New Posts New Posts RSS Feed: Question about entity manager cache
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Question about entity manager cache

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

Joined: 02-Jun-2010
Posts: 15
Post Options Post Options   Quote ehsan Quote  Post ReplyReply Direct Link To This Post Topic: Question about entity manager cache
    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 ?
Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post 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).

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: 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.
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down