开发者

BLToolkit with Linq -- Why is a `using` statement required?

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.Las开发者_开发问答tName, 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?


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.


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. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜