Print Page | Close Window

Managing Entity Managers and the cache for WinForm apps

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2786
Printed Date: 20-Jun-2026 at 1:08pm


Topic: Managing Entity Managers and the cache for WinForm apps
Posted By: Randar
Subject: Managing Entity Managers and the cache for WinForm apps
Date Posted: 21-Jun-2011 at 1:31pm

I have a WinForm application that I am trying to port to use DevForce.  I originally assumed that entity managers shared their caches.  Probably because in ASP.Net, the Cache object is shared by the entire application, so I assumed it worked the same way.  My current implementation creates and destroys Entity Managers quite frequently.  For example, I have a method which goes and gets an entity and a bunch of related entities.  I assumed that was a “unit of work”, so it creates an Entity Manager, gets all the entities and then closes itself.  Same thing with a Save, I get some entities, make changes and then call SaveChanges().

The problem is, if I now go back and re-retrieve the same entities, by re-opening the same form 10 seconds later, I can see using SQL Profiler it is still getting it from the database.  Which I can only assume means that the cache is local to the Entity Manager and not the application.

Questions

  • Am I correct and the cache is not shared across Entity Managers by default?  If yes, can you change that?
  • Should I be solving this by keeping Shared instances (static in C#) of the Entity Managers?  Is this a recommended best practice?  Or should I be using a Singelton to manage all my managers?
  • Are there any general best practices or patterns when it comes to managing EntityManagers for WinForms?



Replies:
Posted By: ting
Date Posted: 23-Jun-2011 at 11:25am
Hi Randar,

Yes, each EntityManager maintains its own cache. Most applications should start off using a single EntityManager, and should only use multiple EntityManagers when you want to isolate one work flow from another. If you want to share data with another EntityManager, you can call ImportEntities to move entities across managers. (You may also find FindEntityGraph to be useful to get entities that you want to import.)

Putting the EntityManager, in a singleton works, but will be rather awkward if the application later needs to isolate any workflows.

My suggestion is that you create a main EntityManager and hold it in the main form of the application. All of the child forms in the application should take an EntityManager as a parameter in their constructor. When a child form is launched, the parent form can decide either to pass the parent's EntityManager to the child, or to create a new EntityManager context and pass it to the child.

Some architectural purists will use a dependency injection framework to assign the EntityManager to the child, but this is probably more than you need.




Print Page | Close Window