Reusable ObjectContext or new ObjectContext for each set of operations?

I'm new to the Entities Framework, and I'm just starting to play with it in my free time. One of the main questions I have is regarding the handling of ObjectContexts.

What is usually recommended / recommended of them:

it

public class DataAccess{ MyDbContext m_Context; public DataAccess(){ m_Context = new MyDbContext(); } public IEnumerable<SomeItem> GetSomeItems(){ return m_Context.SomeItems; } public void DeleteSomeItem(SomeItem item){ m_Context.DeleteObject(item); m_Context.SaveChanges(); } } 

Or that?

 public class DataAccess{ public DataAccess(){ } public IEnumerable<SomeItem> GetSomeItems(){ MyDbContext context = new DbContext(); return context.SomeItems; } public void DeleteSomeItem(SomeItem item){ MyDbContext context = new DbContext(); context.DeleteObject(item); context.SaveChanges(); } } 
+3
c # linq-to-entities data-access-layer objectcontext
source share
4 answers

ObjectContext is for the unit of work.

In essence, this means that for each “operation” (for example: every request to a web page) there must be a new instance of ObjectContext. As part of this operation, you must reuse the same ObjectContext.

This makes sense when you think about it, because transactions and change propagation are tied to an ObjectContext instance.

If you are not writing a web application and instead writing a WPF application or Windows forms, it becomes a little more complicated, since you do not have a narrow request area, downloading gives you, but you get an idea.

PS: In any of your examples, the lifetime of an ObjectContext will be either global or temporary. In both situations, it should not be inside the DataAccess class - it should be passed as a dependency

+6
source share

If you keep the same context for a long-term process executing lots of queries against it, linq-to-sql (I have not tested linq for objects, but I think the same problem) becomes VERY slow (1 query in a second after 1000 simple queries). Updating the context on a regular basis fixes this problem and does not cost so much.

What happens is that the context keeps track of every request you make on it, so if it is not reset in some way, it becomes really fat ... Another problem is the memory it occupies.

Thus, it depends mainly on how your application works, and if you update the DataAccess instance again or keep it the same. Hope this helps.

Stéphane

+1
source share

A simple note - the two parts of the code are roughly the same in their main problem. This is what I looked at because you do not want to open and close the context (see the second example), at the same time you are not sure whether you can trust Microsoft to properly manage the context for you.

One of the things I did was to create a common base class that lazily loads Context and implements a base class destructor to get rid of things. This works well for something like an MVC framework, but unfortunately leads to the problem of passing context to different layers, so business objects can share the call.

In the end, I went with something using Ninject to embed this dependency in each level and track its usage.

0
source share

While I am not a proponent of creating what should be complex objects every time I need them, I also found that DataContexts in Linq to Sql and ObjectContexts in EF are best created when necessary.

Both of them do a lot of static initialization based on the model with which you start them, which is cached for subsequent calls, so you will find that the initial start for the context will be longer than all subsequent instances.

The biggest obstacle that you encounter is that, as soon as you get the entity out of context, you cannot just pass it back to another to perform update operations or add related objects back. In EF, you can connect again, the object returns to a new context. In L2S, this process is almost impossible.

0
source share

All Articles