问题
My angular application uses Breeze and encapsulates the breeze calls within a datacontext service. How can I use this service with different EntityManagers? I am thinking that I need to abandon using the datacontext as an angular service in this situation, since it seems like it is going to require maintaining state.
How do others deal with this? I really hate not being able to use the angular service, but I also want to have re-use of the datacontext with a different EntityManager.
回答1:
I don't know enough about your requirements to give a specific answer. But I wouldn't be so quick to abandon the "datacontext" abstraction.
This question appears to be related to your other question here which seems to be the "sandbox" case. I'll pick up the thread there.
Sometimes I need multiple EntityManagers
when I need to talk to multiple services, each with its own entity model. And when that happens I typically have a different workflow for each service in which case I should have separate "datacontexts" for each workflow. Problem "solved" :-)
Sometimes I'll want two EntityManagers
for the same service because I'm "sandboxing". I "sandbox" when I need two copies of the same nominal entity:
- A read-only version representing the state of the entity on the database
- A mutable version holding the user's unsaved changes.
I might implement this scenario with separate "datacontexts" too
- The main, read-only datacontext handling most of the app's data access needs.
- An "editing" datacontext, smaller than the first, focused on the edit/save workflow.
Each datacontext has its own instance of an EntityManager
. Each instance is mostly configured the same way and typically has the same metadata.
I might create a SandboxContextFactory
service to create new instances of the specialized datacontext. Each such SandboxContext
has its own EntityManager
.
I tend to write an EntityManagerProvider
(EMP) service to create EntityManager
s and inject this into the datacontext services. This EMP can create and coordinate multiple instances of a commonly configured "master" EntityManager
.
The EMP might:
- Create a "master"
EntityManager
at the start. - Populate that "master" with metadata (via
fetchMetadata
call) - Populate the "master" with app-wide reference entities (e.g., "lookup" entities).
- Create new "sandbox" managers when asked, perhaps by.
- cloning the master:
var childEm = masterEm.createEmptyCopy();
- populating the child with common data from the master (e.g., reference entities). You can do that easily by exporting the pertinent entities from master and importing them into the child.
- cloning the master:
Hope these architectural speculations inspire a solution appropriate for you. If you need help developing your application along these lines, you might consider engaging IdeaBlade's professional services.
来源:https://stackoverflow.com/questions/30245807/angular-and-breeze-multiple-entitymanagers