When should I get rid of the data context

I am currently writing a data access level for an application. The access level makes extensive use of the linq classes to return data. Currently, to reflect the data back into the database, I have added a personal data context element and a public save method. The code looks something like this:

private DataContext myDb; public static MyClass GetMyClassById(int id) { DataContext db = new DataContext(); MyClass result = (from item in db.MyClasss where item.id == id select item).Single(); result.myDb = db; return result; } public void Save() { db.SubmitChanges(); } 

This is crude compared to simplification, but it gives a general idea. Is there a better way to handle this pattern? Should I create an instance of a new data context every time I want to visit db?

+53
c # linq linq-to-sql
Dec 23 '08 at 19:25
source share
3 answers

In fact, it does not really matter. I asked Matt Warren from LINQ to SQL a command about this a while ago, and here is the answer:

There are several reasons why we implemented IDisposable:

If application logic needs to be stored on an object outside when it is assumed that a DataContext or an existing one will be used, you can enforce the Dispose call to execute this contract. Deferred loaders in this object will still be referenced by the DataContext and try to use it if any code tries deferred properties. These attempts will fail. Dispose also causes the DataContext to flush the cache of materialized objects so that a single cached object will not accidentally support all materialized subjects through this DataContext, which otherwise causes what appears to be a memory leak.

The logic that automatically closes the DataContext connection can be tricked into leaving the connection open. The DataContext relies on application code that lists all the query results from the moment it receives the end of the result set and starts a connection to close. If the application uses the IEnumerable MoveNext Method instead of foreach in C # or VB, you can exit the listing prematurely. If your application is having problems with connections not closing, and you suspect automatic closing does not work, you can use Dispose as a work around.

But in most cases, you do not need to get rid of them in most cases - and this is by design. I personally prefer to do this anyway, since it’s easier to follow the “dispose of everything that implements IDisposable” rule than remember to load exceptions from it — but you’ll hardly miss a resource if you forget to get rid of it.

+64
Dec 23 '08 at 19:42
source share

View your datacontext resource as a resource. And the resource usage rule says

"getting the resource is still possible, release it as soon as its safe"

+16
Dec 23 '08 at 19:38
source share

The DataContext is quite lightweight and designed to be used in a production application when you use it. However, I don’t think I saved the DataContext in my object. You might want to take a look at the repository templates if you are not going to use the code created by the designer to manage your business objects. The repository template will allow you to work with your objects removed from the data context, and then reconnect them before performing updates, etc.

Personally, I can mostly live with the created DBML code developer, with partial class implementations for my business logic and validation. I also create an abstract abstract data construct and inherit it to allow me to intercept things like stored procedure methods and table functions, which are added directly to the data context and apply business logic there.

The sample I used in ASP.NET MVC is to introduce a factory class that creates the appropriate data contexts needed for units of work. Using a factory allows me to easily outline a data context using (1) a wrapper around an existing data context class to mock (mock the wrapper because the DataContext is not easy to mock) and (2) create a Fake / Layout of contexts and factories to create them. Being able to create them at will with the factory makes it so that I don’t need to maintain it for long periods of time.

+4
Dec 23 '08 at 19:31
source share



All Articles