关于 Linq 中数据库上下文重用的意见
我有一个使用 linq 访问数据库的类。有些方法调用其他方法。例如:
class UserManager
{
public User[] getList()
{
using(var db = new MyContext())
{
return db.Users.Where(item => item.Active == false);
}
}
public User[] addUser(string name)
{
using(var db = new MyContext())
{
db.Users.InsertOnSubmit(new User() { id = Guid.NewId(), name = name, active = false ...});
}
return getList();
}
...
在对 addUser 的调用中,我需要返回新列表。 (我知道就目前情况而言,这不是一个很好的设计,但为了简单起见,我删除了细节。)但是,对 getList 的调用创建了第二个数据上下文。
我可以用额外的方法来填充它,即:
public getList()
{
using(var db = new MyContext())
return getList(db);
}
public getList(MyContext db)
{
...
}
然后替换 addUser 中的调用,以保持相同的数据上下文。
我似乎在我的代码中经常看到这种类型的事情,并且我担心创建和释放所有这些数据上下文的成本。有谁认为是否值得投入额外的工作来消除这些上下文的创建和删除?
I have a class that uses linq to access the database. Some methods call others. For example:
class UserManager
{
public User[] getList()
{
using(var db = new MyContext())
{
return db.Users.Where(item => item.Active == false);
}
}
public User[] addUser(string name)
{
using(var db = new MyContext())
{
db.Users.InsertOnSubmit(new User() { id = Guid.NewId(), name = name, active = false ...});
}
return getList();
}
...
In the call to addUser I am required to return the new list. (I know as it stands it isn't a great design, but I have eliminated detail for simplicity.) However, the call to getList creates a second data context.
I could pad this out with extra methods, viz:
public getList()
{
using(var db = new MyContext())
return getList(db);
}
public getList(MyContext db)
{
...
}
Then replace my call in addUser so as to keep the same data context.
I seem to see this type of thing a lot in my code, and I am concerned with the cost of creating and releasing all these data contexts. Does anyone have an opinion as to whether it is worthwhile putting in the extra work to eliminate the creation and deletion of these contexts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Microsoft 提供了以下建议,以防止重复使用 DataContext 实例 http://msdn.microsoft .com/en-us/library/bb386929.aspx
常见问题 (LINQ to SQL)
Microsoft provides the following advice/recommendation to not reuse DataContext instances http://msdn.microsoft.com/en-us/library/bb386929.aspx
Frequently Asked Questions (LINQ to SQL)
可以重用同一逻辑操作的不同部分(也许通过将数据上下文作为参数传递),但您不应该重用更多的东西:
,重用等等是非常不明智的。所以:原子操作很好;长寿命的应用程序上下文;坏的。
It is ok to reuse for different parts of the same logical operation (perhaps by passing the data-context in as an argument), hut you shouldn't reuse much beyond that:
Etc. So: atomic operations fine; a long-life app context; bad.
我通常做的是创建一个类,您可以将其称为 DataManager 之类的类,并将所有数据函数作为成员。此类在其构造函数上创建 MyContext 的实例。
每当您执行一组操作时,都会创建此类的实例。例如,在控制器上,您可以将此类作为成员。只是不要用它创建一个全局变量,当你完成它时实例化并处置它。
What I usually do is create a class the you could call something like DataManager with all data functions as members. This class creates an instance of MyContext on its constructor.
You create an instance of this class whenever you are doing a set of operations. On a Controller, for instance, you could have this class as a member. Just don't make a global var out of it, instantiate and dispose when you are done with it.