views:

50

answers:

2

Hello,

Currently my website is based around MVC and the Entity Framework running against a SQL Server 2005 database. So far, it has all been running very smoothly, and I really enjoy MVC and its slimmer more concise code (and no huge viewstates or soul destroying postbacks ;))

Recently I was working on upgrading the site to use a simple forum system, and this is where I started running into problems. When I was testing the site using two different browsers, if I created or replied to a post in one browser, the other browser couldn't see the post.

At the moment, each visitor to the site gets their own copy of the entity model, which I store in their session data. Obviously this is the problem as updates to one model aren't getting carried to the other.

As a test, I tried storing a single copy of the model which all visitors would access by assigning the model to a static variable. This worked, and both browsers could see each others modifications. However, it had its side effects. For example, if I fired up both browsers at the same time and the model was initialized, one browser would crash, and the other would work fine, despite me using a locking object so in theory one of them should have been delayed until the model was ready (of course I could have implemented this wrong ;)). Also, originally this site did use one model for all visitors and when it was live, it frequently shut down - killing the IIS application pool while it did. Now I'm not sure if this was related, but I don't really want to reintroduce whatever bug I had that caused this shut down.

So, my question is a simple one really - what is the best way of either using the same model for all website users so they all see updates, or if they do have separate copies (which I imagine will have a performance impact in time) how can the models detect changes in the database and update themselves according.

Thanks in advance for any advice!

Regards;
Richard Moss

+1  A: 

Get required data from the database on each request. Let web and/or ASP.Net caching do its job instead of putting data in session state.

Cache invalidation, on timeout or by directly invalidation through code, can be done whenever new data are submitted to the database.

Some more clarification: when I say "get required data...on each request" it is based in the fact that from a resource management perspective it is more cost-effective to get data from the database when you need it. Storing a copy of the forum data in each users session state will quickly drain your server of memory (depending on the number of users and size of the data of course). Also, as you have experienced yourself, trying to keep those copies synchronized with new changes becomes very hard. Again, going to the database whenever you need data solves these synchronization issues too.

So to answer your "what is the best way..." question: don't store the model at all. Datacontext and model should only live as long as needed for one page request.

Now, I mentioned caching because that would probably be why you put data in session state in the first place. EF does not do caching. What I suggest is look at the ASP.Net caching features. This is caching at the page and html level which is much more resource efficient.

Peter Lillevold
Do entity models support caching? I thought they did "clever stuff" like generating SQL statements to only return the data that was requested - how you are supposed to cache that I honestly don't know. I never deal directly with the database, everything goes though service/repository classes to the entity model which itself provides all the data access.
Richard Moss
@richardmoss: added some clarification. hope it actually clarifies :)
Peter Lillevold
Peter,Thanks for the information, sounds like I'm doing it completely the wrong way so time for some code juggling and making use of caching.Thanks again!;Richard Moss
Richard Moss
@Richard: glad to be of assistance. Best of luck to you!
Peter Lillevold
A: 

"each visitor to the site gets their own copy of the entity model, which I store in their session data"

and

"When I was testing the site using two different browsers, if I created or replied to a post in one browser, the other browser couldn't see the post."

Wait a second...

Are you putting an ObjectContext into a Session variable? That is a pretty odd thing to do. Are you sure your flushing your changes to the database?

jfar