views:

274

answers:

2

Let's start with this basic scenario:

I have a bunch of Tables that are essentially rarely changed Enums (e.g. GeoLocations, Category, etc.) I want to load these into my EF ObjectContext so that I can assign them to entities that reference them as FK. These objects are also used to populate all sorts of dropdown controls. Pretty standard scenarios so far.

Since a new controller is created for each page request in MVC, a new entity context is created and these "enum" objects are loaded repeatedly. I thought about using a static context object across all instances of controllers (or repository object).

But will this require too much locking and therefore actually worsen perf?

Alternatively, I'm thinking of using a static context only for read-only tables. But since entities that reference them must be in the same context anyway, this isn't any different from the above.

I also don't want to get into the business of attaching/detaching these enum objects. Since I believe once I attach a static enum object to an entity, I can't attach it again to another entity??

Please help, I'm quite new to EF + MVC, so am wondering what is the best approach.

A: 

I've implemented something similar using Linq2SQL by retrieving these 'lookup tables' as lists on app startup and storing them in ASP's caching mechanism. By using the ASP cache, I don't have to worry about threading/locking etc. Not sure why you'd need to attach them to a context, something like that could easily be retrieved if necessary via the table PK id.

Codewerks
+1  A: 

Personally, I never have any static Context stuff, etc. For me, when i call the database (CRUD) I use that context for that single transaction/unit of work.

So in this case, what you're suggesting is that you wish to retrieve some data from the databse .. and this data is .. more or less .. read only and doesn't change / static.

Lookup data is a great example of this.

So your Categories never change. Your GeoLocations never change, also.

I would not worry about this concept on the database/persistence level, but on the application level. So, just forget that this data is static/readonly etc.. and just get it. Then, when you're in your application (ie. ASP.NET web MVC controller method or in the global.asax code) THEN you should cache this ... on the UI layer.

If you're doing a nice n-tiered MVC app, which contains

  • UI layer
  • Services / Business Logic Layer
  • Persistence / Database data layer

Then I would cache this in the Middle Tier .. which is called by the UI Layer (ie. the MVC Controller Action .. eg. public void Index())

I think it's important to know how to seperate your concerns .. and the database stuff is should just be that -> CRUD'ish stuff and some unique stored procs when required. Don't worry about caching data, etc. Keep this layer as light as possible and as simple as possible.

Then, your middle Tier (if it exists) or your top tier should worry about what to do with this data -> in this case, cache it because it's very static.

Pure.Krome
Thx for answering, Pure.Krome.That is a good solution IF I can use the cached Categories EntitySet as references in my other EF context calls. But I can't. If EF worked like Linq2SQL (or EF 4.0 with FK Association), then I can simply use the PK IDs and that's end of story. But unfortunately I can't do that at this point. I have to assign a valid Category to parent entities for creation/update calls within the relevant EF context. This sucks, but is the way you are "supposed" to use EF... So if I were to use EF like this, how best to "cache" these data like Categories?
Xerion
If i read you right, what you're trying to do is either 1) eager load some static lookup data and/or 2) lazy load some static lookup data. In my opinion, i turn _OFF_ lazy loading. I personally hate that. Why? It's means u haven't thought about your application and have not loaded all the data with the initial hit ... which is bad IMO. Less db hits the better. So, if u need some object which contains a child property .. which is some lookup data .. then maybe we can figure this out. So, can u post some code please, so I can then try and manipulate it. I can't promise anything .. but lets see..
Pure.Krome