2

I am trying to learn entity framework and related patterns. While searching I came across the site: http://www.asp.net/mvc...

I checked the patterns, but I could not understand one point. According to my investigations, dbcontex lifetime should be very little because it has in-memory object model and these changes should be persisted to database as fast as possible. If not, there will be conflicts in multi-user scenarios.

When I look at the above tutorial, I see that for every controller there is only one uow defined. I wonder if this means as long as I am on a one page of the site doing CRUD operations I am using the same dbcontext. But shouldn't its lifetime shorter? For example for every action one uow could be defined.

Could somebody please explain the lifetime of uow?

Community
  • 1
  • 1
Omer
  • 8,194
  • 13
  • 74
  • 92

3 Answers3

2

Each action being called is a new instance of the controller. Just set a breakpoint in your controller constructor and you will see that it is called everytime you make a request to any action on the controller.

Generally a DBContext is scoped per web request in a web application. So if you inject a DBContext into your controller that generally will give you what you need.

GraemeMiller
  • 11,973
  • 8
  • 57
  • 111
  • thanks, so if I develop client-wcf service-server? Per service request I should create one dbcontext. I am right? – Omer Jul 04 '15 at 23:32
  • Correct - one dbcontext. Always try to keep DB Context as short lived as possible but as scoped as relevant. It will manage the transaction - generally each of my actions represents a command and either I want everything in that command to succeed or everything to fail. I wrap a filter around my actions that commits my unit of work at the end of an action assuming there no errors. – GraemeMiller Jul 05 '15 at 00:53
2

Defining a DbContext as a private class variable vs. defining it as a local variable shouldn't make any difference.

Every time an HTTP request is created, the controller is initialized (as well as any of it's class variables) and the action is called. Instances of ontrollers will not persist between different requests, nor will any instances of DbContext.

Check out this article about why you don't have to worry about the lifetime of a DbContext.

EDIT

I realized a caveat to this answer a few days after I posted it, and I would have felt guilty had I not updated it.

The above statement is true if every action uses your DbContext. If only a few of your actions use it, however, you might be better off using a locally-scoped DbContext instead. This will prevent unnecessary creation of a DbContext class variable any time you call an action that doesn't require usage of it. Will this make your code more efficient? Yes - but insignificantly so - and you'll have to instantiate a DbContext every time you want to use it, which will result in slightly messier code than just having one class-variable at the top.

Community
  • 1
  • 1
johnnyRose
  • 7,310
  • 17
  • 40
  • 61
  • thanks, so if I develop client-wcf service-server? Per service request I should create one dbcontext. I am right? – Omer Jul 04 '15 at 23:33
  • Correct - defining one private `DbContext` at the top of your controller vs defining it in every action really makes no difference. The former is certainly cleaner code, though. – johnnyRose Jul 04 '15 at 23:34
1

In the example given, the controller is taking the responsibility of creating the instance of the DbContext and doing the dispose. A better practice is to let IoC container to take the responsibility of the lifetime control for the instance of DbContext, and implement constructor injection to inject the DbContext into the MVC/WebApi controller.

As for WCF service, my preference is to indicate the below attribute [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] for the service, and also specify the lifetime of the DbContext so that only one DbContext instance would be create per call.

you may need to have some lifetime management if DI will be implemented for WCF service or MVC/WebApi. Ref: https://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx#_Lifetime_Management

This post is also highly recommended for your question. One DbContext per web request... why?

Community
  • 1
  • 1
Lavande
  • 744
  • 8
  • 20