views:

140

answers:

2

Hi, i've been creating a web application in mvc asp.net. I have three different project/solutions:

  • One solution contains the model in EF (DAL) and all the methods to add, update, delete and query the objects in the model, the objectcontext is managed here in a per request basis.
  • Other solution contains a content management system in wich authorized users insert, delete, update and access objects through the DAL mentioned before.
  • And the last solution contains the web page that is accessed by all users (thousands of users per day) and where the only operations executed are selects, no update, inserts or deletes here.

All the selects are executed against the DAL mentioned before (the first solution). The problem here is that i'm not sure whether an HttpContext lifespan ObjectContext is the best solution.

I have a lot of ajax calls in my web app and i'm not sure if an httpcontext could interfere with the performance of the application. I've been noticed that in some cases, specially when someone is working in the content manager inserting, updating or deleting, when you try to click on any link of the user web application (the web app that is accessed by any user - the third one that i mentioned before) the web page freezes and it remains stucked transferring data. In order to stop that behaviour you have to stop and refresh or click several times on the link. Excuse me for my bad english. I hope you could understand and could help me to solve this issue. Thanx in advance.

A: 

You need a new ObjectContext for every operation, because it's designed to be used for a unit of work. You want to dispose it after calling SaveChanges, because if you would use it to select from it after saving changes, you would get unexpected results.

Sander Rijken
Sander, thank you for taking the time to answer. I have implemented a repository pattern in wich the ObjectContext is created for every HttpRequest. When you says that I must create an ObjectContext for every operation what you mean is that for every select, update, insert i need to create an ObjectContext? Note that in the same page i have multiple requests. e.g the home page performs multiple selects in the form of ajax request: GetArticles, GetEvents, GetPreview and so on all of these methods are call in one request. Then with your approach i have to create the objectContext for each one?
fabianadipolo
When it's read only access it won't hurt to keep it around longer. There's no win in keeping it around longer either. The DB connection will be closed after each query and opened again for each successive query. When doing updates to the database you really need to dispose the context after saving changes, because the results from subsequent selects can't be trusted
Sander Rijken
A: 

I have implemented a repository pattern in wich the ObjectContext is created for every HttpRequest. If i create the ObjectContext for every operation, then if i have multiple queries in my home page, how many ObjectContext i have to create?

Note that in the same page i have multiple requests in the form of ajax requests. e.g the home page performs this methods: GetArticles, GetEvents, GetPreview, GetPopularArticles, GetLatestVideos and so on. All of these methods are call in one page.

I really don't understand very well how this ObjectContext works and i am concerned about the performance issues of creating the objectContext many times or reused it in a way that is not the correct one.

fabianadipolo
This is another question, not an answer
Sander Rijken
Sorry,im new here
fabianadipolo