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
- as it appears in Workspace #1 immediately (probably not)?
- when the change is saved in Workspace #2 (probably so)? or just
- 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).