Is there any real purpose to calling Dispose() on a Linq to SQL query? I want my site to be scalable so I’d like to control the datacontext but if it’s not necessary I don’t see a point.
Not sure of the benefits of calling Dispose(), but to be safe, put it into a “using” statement. It will close/dispose the connection when the statement block is finished.
using(MyDBContext dbc = new MYDBContext())
{
// linq code goes here.
}
If you already knew this, just ignore.
http://www.sitepoint.com/forums/showthread.php?t=599230
Using() just doesn’t seem practical when using a repository and trying to employ strict separation of concerns. At least there doesn’t seem to be a way to make it work.
What about creating an IHttpModule that disposes of the datacontext (if it exists) at .PostRequestHandlerExecute? Is that a useful idea?
I guess I should mention that I’m using Windsor.Castle IoC container. Does Castle have any control over Dispose()? I think I need to understand more about IoC containers.
@imagineKitty: http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/01/27/transparently-releasing-components-in-windsor.aspx
So it says that Windsor does (or is supposed to) specifically dispose of what it instantiates but gives a suggestion on how to help it along. Am I too concerned about something that may not ever cause an issue? I’m just not keen on leaving it to chance.
Perhaps I should post some code and suggestions could be made.
@dhtmlgold
The post in the link deals with releasing “components” from Windsor when they are disposed.
imaginekitty question was on how to dispose of LinqToSql DataContexts and avoid the repetitive use of using statements.
@imagineKitty
Using Windsor you can let windsor manage the life of the objects it creates… one of the life styles that you can use ties an instance with a web request, disposing the instances when the request terminates. This is done using a HttpModule. You might want to take a look at that.
cheers,
Rui
Thank you. I’m using the PerWebRequest lifeStyle option. I was hoping this was the case.
Just to be clear though, since I’m using this IoC container to avoid using hard coded dependencies, when I’m letting Windsor instantiate then I can also let it dispose? Is that safe to say?
with PerWebRequest Lifestyle, disposed will be called at the end of the request.
Excellent news! Thank you.