BLToolkit with Linq —— 为什么需要 `using` 语句?
鉴于 SubSonic 3 最近的(极端)性能问题,我们正在寻求迁移 ORM,最好重写尽可能少的代码(主要是 Linq)。
所以我正在寻找 BLToolkit。我发现 SubSonic 和 BLToolkit 之间的主要区别之一是 BLToolkit 始终需要 using
语句。例如:
static void SingleTableTest()
{
using (var db = new NorthwindDB()) //This
{
var query =
from e in db.Employee
where e.EmployeeID > 5
orderby e.LastName, e.FirstName
select e;
foreach (var employee in query)
{
Console.WriteLine("{0} {1}, {2}", employee.EmployeeID, employee.LastName, employee.FirstName);
}
}
}
这到底是做什么的?当您创建数据库的新实例时,它会创建新连接吗?将其“包装”到静态类中是否合理,以便我可以从 Database.Employee ... 中的任何 var q=from e ...
进行操作?这会对 Web 应用程序产生什么影响?
I light of recent (extreme) performance issues with SubSonic 3, we are looking to migrate ORMs, preferably rewriting as little code as possible(which is mostly Linq).
So I am looking at BLToolkit. One of the major differences I see between SubSonic and BLToolkit though is that BLToolkit always requires a using
statement. For instance:
static void SingleTableTest()
{
using (var db = new NorthwindDB()) //This
{
var query =
from e in db.Employee
where e.EmployeeID > 5
orderby e.LastName, e.FirstName
select e;
foreach (var employee in query)
{
Console.WriteLine("{0} {1}, {2}", employee.EmployeeID, employee.LastName, employee.FirstName);
}
}
}
What exactly does this do? When you create a new instance of the database, does it create a new connection? Would it be reasonable to "wrap" this into a static class so that I could do from anywhere var q=from e in Database.Employee ...
? What repercussions would this have in the context of a web application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜你的例子中的 NorthwindDB 类是基于 DbManager 的。 DbManager 是 Connection 的包装器,其行为类似于连接。您应该尝试另一个类 - DataContext。它是专门为您的场景而设计的。
I guess the NorthwindDB class in your example is based on DbManager. DbManager is a wrapper around Connection and behaves like a connection. You should try another class - DataContext. It's designed exactly for your scenario.
我不知道 BLToolkit,但从您的评论来看,您想知道是否可以在每个 HTTP 请求中仅使用一个对象,而可以使用实体框架之类的东西。您可以在 global.asax 的 Application_BeginRequest 事件中创建一个 db 对象,而不是使用 using 语句。您可以在 Application_EndRequest 中处理它。您可以将该对象存储在 HttpContext.Current.Items 中,这是一个方便的每个请求集合。
正如我所说,我不知道这是否适用于 BLToolkit,因为我对此一无所知,但希望它足以为您指明正确的方向。 :)
I don't know BLToolkit, but from your comment that said you wanted to know if it was possible to just use one object per HTTP request, with something like Entity Framework you can. Instead of a using statement, you create a db object in global.asax's Application_BeginRequest event. You dispose of it in Application_EndRequest. You can store the object in HttpContext.Current.Items, which is a handy per-request collection.
As I said I don't know if that applies to BLToolkit specifically because I don't know anything about it, but hopefully its enough to point you in the right direction. :)