ioan
ioan

Reputation: 722

breeze merge server entities

Here are the main pillars of my problem:

I tried to find a method somewhere in breeze (entity manager?), where, if I pass a list of entities retrieved from server, these are used to update the local cache, but I did not find any. After studying the source code, I did some changes in the breeze library, where I exposed the MappingContext internal class and did the following (TypeScript code):

public processEntityRequestAsync<TResponse>(request: requestModel.RequestBase): Q.Promise<TResponse> {
  var promise = this.requestProcessor.processRequest<TResponse>(request);
  var processedPromise = promise.then(r => {
    // TODO everything in this method is a hack using internal things from breeze
    // to update the entities after getting them from server
    var updatedEntities = <any[]>(<any>r).UpdatedEntities;
    if (updatedEntities) {
      var dataService = <breeze.DataService>(<any>breeze.DataService).resolve([{
        serviceName: this.manager.dataService.serviceName,
        adapterName: "WebApi",
      }]);
      var mappingContext = new (<any>breeze).MappingContext({
        query: null, // tells visitAndMerge that this is a save instead of a query
        entityManager: this.manager,
        mergeOptions: { mergeStrategy: breeze.MergeStrategy.OverwriteChanges },
        dataService: dataService,
      });

      // Note that the visitAndMerge operation has been optimized so that we do not actually perform a merge if the 
      // the save operation did not actually return the entity - i.e. during OData and Mongo updates and deletes.
      updatedEntities = mappingContext.visitAndMerge(updatedEntities, { nodeType: "root" });
    }
    return r;
  });
  return processedPromise;
}

NOTE: the <any> conversions are used to bypass the Typescript type checking and use breeze internals.

This works, however I do not like that I have to use breeze internals AND that I have to modify the library for this purpose. Is there a better way to achieve my target?

Upvotes: 1

Views: 292

Answers (1)

Jay Traband
Jay Traband

Reputation: 17052

I'm not entirely sure I understand the question but take a look at the discussion of the JsonResultsAdapter: http://www.breezejs.com/documentation/web-service-data-mapping

The idea is that a JsonResultAdapter can be used on the client to coerce any json data into entities that will be merged into the EntityManager.

Upvotes: 0

Related Questions