简体   繁体   中英

breezejs: dynamically switching between OData and WebAPI

I'm facing a bit of a tricky situation here. I have two services that my application needs to access to. One is a pure ODATA service and the other is a WebAPI (with Breeze controller) service.

My application is designed around the AngularJS modules and breeze is injected into two differerent single-instance services :

angular.module('domiciliations.services').
   factory('domiciliationService', function (breeze) {

     breeze.config.initializeAdapterInstance("modelLibrary", "backingStore", true);
     //more initialization goes here

     //then query methods go here
}

and

angular.module('person.services').
   factory('personService', function (breeze) {

    breeze.config.initializeAdapterInstances({ "dataService": "OData" });

     //more initialization goes here

     //then query methods go here
}

Now obviously the problem is that once the person service has been instanciated, the domiciliations service then uses OData because the config was overwritten.

So, what is the general approach for tackling this issue ? Is there a way to isolate the config ? So far the only way I can think of, is to call the initializeAdapterinstances method each time a query method is called, which is not really desirable.

EDIT

As per Jay's recommandation I'm now using DataService. I'm having an error though in ctor.resolve at the line:

ds.jsonResultsAdapter = ds.jsonResultsAdapter || ds.adapterInstance.jsonResultsAdapter;

ds.adapterInstance is null, therefore this throws an exception. But I don't understand why it's null.

Here's what I've done:

var service = new breeze.DataService({
            serviceName: 'http://localhost:16361/api/mandates',
            adapterName: 'WebApi'
        });
var manager = new breeze.EntityManager({ dataService: service });

//this is the line causing the later exception:

manager.fetchMetadata().then(function () { ... }

Did I forget to do something ?

Good question!

The initializeAdapterInstance method is really intended to setup the 'default' adapters.

If you need to have multiple adapters and apply them on a per query basis then see the DataService documentation especially the 'adapterName' property in the ctor. You can have two DataServices, one for OData and one for WebApi. You can then use either for any query via the EntityQuery.using method.

 var odataDataService = new DataService({
       serviceName: "Foo",
       adapterName: "OData"
 });
 var webApiDataService = new DataService({
       serviceName: "Bar",
       adapterName: "WebApi"
 });

 var query1 = EntityQuery.from(...).where(...);
 var query2 = EntityQuery.from(...).where(...);

 entityManager.executeQuery(query1.using(odataDataService)).then(...)
 entityManager.executeQuery(query2.using(webApiDataService)).then(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM