Search code examples
c#asp.netlinqusingbltoolkit

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.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?


Solution

  • 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.