New ObjectContext on every HttpRequest

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 лет назад
I would like to ask that why nopCommerce is creating ObjectContext on each HttpRequest? Could it not be instantiated for the very first request and then be reused for further requests?
13 лет назад
I have also asked a very similar question on StackOverflow. I am coming from the NHibernate background and I want to understand the performance part of managing lifecycle of ObjectContext.

http://dotnetslackers.com/articles/ado_net/Managing-Entity-Framework-ObjectContext-lifespan-and-scope-in-n-layered-ASP-NET-applications.aspx#1390

This is the article written by same author who has contributed to "AspNetObjectContextManager.cs". I understand the points that he has mentioned in article, however if somebody can explain me the repercussions of sharing same ObjectContext across application, that would be of great help.
13 лет назад
The same ObjectContext is shared throughout the request - it is stored in the HttpContext.Items collection (see AspNetObjectContextManager)

This follows the unit of work pattern and is a similar approach to creating your NHibernate ISession on BeginRequest and disposing it on EndRequest.
13 лет назад
Sharing it within same HttpRequest is good only in case when you have to go to data store multiple times. For example - when you have some kind of complex business logic. But if you have to perform simple CRUD(single hit to data store), then sharing it within HttpRequest is as good as creating it every time.

Did nopCommerce face any situation where sharing ObjectContext through out application was creating problem?
13 лет назад
p_gururani wrote:
Sharing it within same HttpRequest is good only in case when you have to go to data store multiple times. For example - when you have some kind of complex business logic. But if you have to perform simple CRUD(single hit to data store), then sharing it within HttpRequest is as good as creating it every time.

Did nopCommerce face any situation where sharing ObjectContext through out application was creating problem?


Think you have just contradicted yourself. Either way, it really makes no difference whether the objectcontext is used once or multiple times throughout the request. The main point is that if we need to hit the database multiple times, we can share the same objectcontext, therefore saving resources.

I don't think this is a good idea, just like you wouldn't use a single ISession in a NHibernate application. Keeping the same objectcontext around is a real drain on resources due to the amount of objects you will end up tracking.
13 лет назад
Creating a data context is cheap. Creating one at the start of a Http request and disposing of it at the end of the request is perfect as far I'm concerned. Keeping them round for longer is just asking for trouble. See e.g. http://community.icburner.com/blogs/vs2010tests/archive/2009/06/20/tip-18-how-to-decide-on-a-lifetime-for-your-objectcontext.aspx
13 лет назад
Above mentioned ICBurner community post seems to answer my question.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.