views:

176

answers:

1

I have a EF4/ASP.NET web application that is structured to use POCOs and generic repositories, based essentially on this excellent article.

The application is relatively sophisticated with one page that involves selection and linking of multiple entities to build up a complex user profile. This requires access to multiple entity types (20 or so) and associated repositories across multiple posts.

When a repository is first accessed it uses the existing data context if exists, else it creates a new context. The problem is that if the lifetime of the context is only per-request (as suggested in the article) then you have to deal with multiple contexts and the complexity around detaching and attaching entities from contexts.

My solution is to share the context between posts by creating a single View Model that includes all required repositories (initialised to share the same context) plus any associated data and store this model in a Session variable, retrieving from Session on subsequent page requests. Therefore maintaining the same context across all posts until the profile is saved.

This works fine BUT I am concerned that I don't actually know exactly what is stored in the model session variable or more importantly the size of the Session variable.

So two questions I suppose: firstly should I look for a better solution to handle the shared context across posts issue (any suggestions welcome)? And secondly what is actually stored in the Session when it includes a repository plus context?

Any help appreciated!

+1  A: 

Rick Strahl covered a very similar topic using Linq 2 SQL back in early 2008..

http://www.west-wind.com/weblog/posts/246222.aspx

Its quite a long read and i have never implemented it, so cannot recommend it. It looks like a lengthy job to implement.

I am in a similar position to yourself, which is why i came across this post.

I do wonder if just instantiating the repositories where necessary is the way to go, and let sql server manage the connection pooling (assuming that you are using sql server of course)

Baldy

related questions