Print Page | Close Window

Question about entity manager cache

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=1863
Printed Date: 21-Apr-2026 at 12:19pm


Topic: Question about entity manager cache
Posted By: ehsan
Subject: Question about entity manager cache
Date Posted: 06-Jun-2010 at 10:12pm
Hi,
I want to know if I create many instances of devforce entitymanager (in silverlight 4), each instace will own its cache or all of them will use a shared cache (& may other resources)???
which one is the best approach ?
1 - creating only one  instance of entitymanager and share it between all classes (it causes coupling !)
2 - create only one  instance of entitymanager and pass it as parameter as any class or method need it (it also causes coupling !)
3 - create an instance of entitymanager where we need it ?



Replies:
Posted By: davidklitzke
Date Posted: 07-Jun-2010 at 11:04am
You are correct.  Each EntityManager has its own separate cache.
 
It is more common and simpler to use a single EntityManager, but this is not necessarily the "best" approach.  A lot depends upon your own personal preference.  Normally, I would start with a single EntityManager.


Posted By: GregD
Date Posted: 07-Jun-2010 at 4:20pm
Originally posted by ehsan

Hi,
I want to know if I create many instances of devforce entitymanager (in silverlight 4), each instace will own its cache or all of them will use a shared cache (& may other resources)???
which one is the best approach ?
1 - creating only one  instance of entitymanager and share it between all classes (it causes coupling !)
2 - create only one  instance of entitymanager and pass it as parameter as any class or method need it (it also causes coupling !)
3 - create an instance of entitymanager where we need it ?



You can share an EntityManager across your entire application in the same way, and with the same benefits, as when you share a database across an entire application (or multiple applications). Namely, there is one and only one instance of any entity represented in the cache (or database)

Some developers like to create workspaces (e.g., Customer with associated Orders, OrderDetails, and Products) wherein an end user can make changes and then save or discard only the changes made within that workspace. This is a nice feature, but does complicate development, as you must now figure out how and when you want to reconcile differences between the state of a particular object as represented in Workspace #1 versus Workspace #2.

For example, the Employee workspace might also have Orders (the ones the Employee wrote), OrderDetails, and Products; so that a given Order, OrderDetail, or Product instance might appear in both the Customer and Employee workspaces. If you make changes to Product X in Workspace #2, should the change be reflected in Product X
  1. as it appears in Workspace #1 immediately (probably not)?
  2. when the change is saved in Workspace #2 (probably so)? or just
  3. when Product X in Workspace #1 is next updated from the database as a byproduct of some other operation?
If your choice is #2 (that it should be updated in Workspace #1 when saved in Workspace #2), you have to write the code to make that happen.

If you can live without separate workspaces, life is a lot easier. The EntityManager has a static DefaultManager property on it that always returns the same instance of the EntityManager, so you just initialize instances of the EntityManager anywhere in your code by setting them to EntityManager.DefaultManager, and you always get the same EntityManager instance, (and the same cache).



Posted By: ting
Date Posted: 07-Jun-2010 at 8:35pm
A few other thoughts:
 
For larger applications, we recommend creating a centralized service that returns you an appropriate EntityManager when you ask for one.  EntityManager.DefaultManager is an easy way to start off and can take you quite far, but that property doesn't know anything about the business context in which the EntityManager is being used or when it's settings may need to be changed.
 
We don't typically find that passing the EntityManager around to every method to be worthwhile as this just tends to add clutter.
 



Print Page | Close Window