Note that there are some explanatory texts on larger screens.

plurals
  1. POEF ObjectContext, Service and Repository - Managing context lifetime.
    text
    copied!<p>I am fairly new to the MVP and the Entity Framework world so bear with me.</p> <p>I currently have a View + Presenter combination, the view has two events Edit and Delete and the presenter just listens for these events. I also have a service object and repositories set up. The service layer takes some repository implementations which take an ObjectContext so the order of construction is (passing in the top object to the one below it:</p> <pre><code>ObjectContext | V Repositries | V Service Object | V Presenter </code></pre> <p>Now the problem is that when I create object context at the top, It's alive for the whole time the presenter is alive meaning that Edit and Delete use the same context instance from the service.</p> <p>So calling ServiceObject.Delete and ServiceObject.Edit use the same context which makes it hard to manage change tracking. From what I understand the context should really only be short lived and only for a unit of work, to me Edit and Delete are both different lots of work. </p> <p>How do you do DI with the entity framework and still manage context life time?</p> <p>I have seen people just new up the object context in side the repository, is this a good pattern.</p> <p>Or should I do that in the service object, something like:</p> <pre><code>ServiceObject{ public void Edit(// some args) { Using(var context = new MyObjectContext) { var repo = new MyRepo(context); var entity = repo.GetForID(12); // Do some stuff for edit context.SaveChanges(); } } } </code></pre> <p>But If I do it like that I am no longer passing my repository into the constructor of ServiceObject and not doing DI :(. </p> <p>What can I do in this situation?</p> <p>Does anyone know any open source projects that I can look at that may help me with this problem.</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload