Bernardo Pacheco
Bernardo Pacheco

Reputation: 1445

How does breezejs's EntityManager work?

I've read the breeze.js's documentation (great documentation, by the way), but I have a doubt about the EntityManager.

Suppose I have the following factory method:

  var createManager = function () {
        return new breeze.EntityManager({
            serviceName: serviceName,
            metadataStore: store
        });
    };

Then I define two instances of Entitymanager:

em1 = createManager();
em2 = createManager();

If a call

em1.saveChanges();

Does em2 save changes too?

Do em1 and em2 share the same cache?

If not, do I have to make a Singleton EntityManager? In my project, I access EntityManager in many places, exchange entities among them, so I am not sure what is the best (or correct) architecture to follow.

Upvotes: 4

Views: 1267

Answers (1)

John Papa
John Papa

Reputation: 22298

I believe the entity manager acts as its own data context on the client. When you create em1 and em2, you are creating 2 different managers, so they save independently of each other. If you want to refer to the same entitymanager in multiple places (which is one of its virtues) you can use the javascript module pattern to reference the instance you create.

My preferred technique is to create a module called datacontext. It owns the entity manager and any other custom features I want surrounding breeze. All other modules reference this datacontext and just ask it for data. The datacontext hides breeze from the rest of the modules. Its not the only way, but I like it because it follows good separation patterns.

Hope this helps.

Upvotes: 7

Related Questions