New Posts New Posts RSS Feed: Managing Entity Managers and the cache for WinForm apps
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Managing Entity Managers and the cache for WinForm apps

 Post Reply Post Reply
Author
Randar View Drop Down
Newbie
Newbie
Avatar

Joined: 13-May-2011
Location: Toronto
Posts: 23
Post Options Post Options   Quote Randar Quote  Post ReplyReply Direct Link To This Post Topic: Managing Entity Managers and the cache for WinForm apps
    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?


Edited by Randar - 21-Jun-2011 at 2:47pm
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post 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.



Edited by ting - 23-Jun-2011 at 11:28am
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down