The ObjectContext is meant to be the "Unit of Work Essentially what this means is that for each "Operation" (eg: each web-page request) there should be a new ObjectContext instance. Within that operation, the same ObjectContext should be re-used This makes sense when you think about it, as transactions and change submission are all tied to the ObjectContext instance If you're not writing a web-app, and are instead writing a WPF or windows forms application, it gets a bit more complex, as you don't have the tight "request" scope that a web-page-load gives you, but you get the idea PS: In either of your examples, the lifetime of the ObjectContext will either be global, or transient. In both situations, it should NOT live inside the DataAccess class - it should be passed in as a dependency.
The ObjectContext is meant to be the "Unit of Work". Essentially what this means is that for each "Operation" (eg: each web-page request) there should be a new ObjectContext instance. Within that operation, the same ObjectContext should be re-used.
This makes sense when you think about it, as transactions and change submission are all tied to the ObjectContext instance. If you're not writing a web-app, and are instead writing a WPF or windows forms application, it gets a bit more complex, as you don't have the tight "request" scope that a web-page-load gives you, but you get the idea. PS: In either of your examples, the lifetime of the ObjectContext will either be global, or transient.In both situations, it should NOT live inside the DataAccess class - it should be passed in as a dependency.
If you keep the same context for a long-running process running lots queries against it, linq-to-sql (I didn't test against linq to entities, but I guess that's the same problem) gets VERY slow (1 query a second after some 1000 simple queries). Renewing the context on a regular basis fixes this issue, and doesn't cost so much. What happens is that the context keeps track of every query you do on it, so if it's not reset in a way, it gets really fat... Other issue is then the memory it takes.So it mainly depends on the way your application is working, and if you new up a DataAccess instance regularly or if you keep it the same all along.
Hope this helps. Stéphane.
Just a quick note - the two code pieces are roughly the same in their underlying problem. This is something that I have been looking at, because you don't want to keep opening and closing the context (see second example) at the same time you are not sure if you can trust Microsoft to properly dispose of the context for you. One of the things I did was create a common base class that lazy loads the Context in and implement the base class destruct-er to dispose of things.
This works well for something like the MVC framework, but unfortunately leads to the problem of having to pass the context around to the various layers so the business objects can share the call. In the end I went with something using Ninject to inject this dependency into each layer and had it track usage.
Microsoft recommends not having long-running DataContexts or ObjectContexts. It kills performance and complicates object tracking. – Andras Zoltan Jan 9 '10 at 1:31.
While I'm not in favour of always creating, what must be, complicated objects each time I need them - I too have found that the DataContexts in Linq to Sql and the ObjectContexts in EF are best created when required. Both of these perform a lot of static initialisation based on the model that you run them against, which is cached for subsequent calls, so you'll find that the initial startup for a context will be longer than all subsequent instantiations. The biggest hurdle you face with this is the fact that once you obtain an entity from the context, you can't simply pass it back into another to perform update operations, or add related entities back in.In EF you can reattach an entity back to a new context.
In L2S this process is nigh-on impossible.
As a result of this - I always simply have a using statement around my datacontext/objectcontext, and push all data manipulation inside it; My contexts are always as short-lived as possible. – Andras Zoltan Jan 9 '10 at 1:33.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.