C# Linq-to-Sql - Should DataContext be disposed using IDisposable -
I have several methods that deal with DB and start by calling them all
FaierDbDataContext DB = new FaierDbDataContext (); Since the Linq2Sql DataContext object implements IDisposable, should it be used with "experiment"? Use it in one way or another What are the effects of doing?
Most of the opposite, which implement idispable, datacontact actually needs a settlement Not - at least not in most cases. I asked Matt Warren about this design decision, and here was his reaction:
There are some reasons we have implemented accidentally:
- If the app Logic is a unit when Datacontext is expected to be used or after valid, you can apply that contract by calling the dispute. The deferred loader in that unit will still refer to the data content and any code will attempt to try to navigate the deferred properties. These efforts will fail, the settlement also forces the datacontax to dump the cache of robust physical sects, so that the same cached unit can not accidentally keep all the living organisms alive through that datacontact, which otherwise would not allow memory leak appears to be.
- A logic that automatically closes the datacontact connect connection, the connection can be tricked to open. Datacentext relies on all the results of a query on the enumeration application code as the connection closes after reaching the end of a result. If the application uses IEnumerable's MoveNext method instead of a foreach statement in C # or VB, you can exit the calculation prematurely if your application does not experience problems with the connection, and you close automatically If you are not sure about behaving, then you can use the settlement pattern as a work.
to
Comments
Post a Comment