[Creation] Use instantiate from objectService

Use instantiate from DomainObjectProvider, to remove redundant
code around DomainObject instantiation.
This commit is contained in:
Victor Woeltjen 2015-11-06 14:23:34 -08:00
parent 81b136eab1
commit cca1928b82
3 changed files with 27 additions and 53 deletions

View File

@ -38,7 +38,7 @@
"provides": "objectService",
"type": "provider",
"implementation": "objects/DomainObjectProvider.js",
"depends": [ "modelService", "capabilityService", "$q" ]
"depends": [ "modelService", "instantiate" ]
},
{
"provides": "capabilityService",

View File

@ -27,8 +27,8 @@
* @namespace platform/core
*/
define(
["./DomainObjectImpl"],
function (DomainObjectImpl) {
[],
function () {
"use strict";
/**
@ -57,62 +57,36 @@ define(
*
* @param {ModelService} modelService the service which shall
* provide models (persistent state) for domain objects
* @param {CapabilityService} capabilityService the service
* which provides capabilities (dynamic behavior)
* for domain objects.
* @param {Function} instantiate a service to instantiate new
* domain object instances
* @param $q Angular's $q, for promise consolidation
* @memberof platform/core
* @constructor
*/
function DomainObjectProvider(modelService, capabilityService, $q) {
function DomainObjectProvider(modelService, instantiate, $q) {
this.modelService = modelService;
this.capabilityService = capabilityService;
this.$q = $q;
this.instantiate = instantiate;
}
DomainObjectProvider.prototype.getObjects = function getObjects(ids) {
var modelService = this.modelService,
capabilityService = this.capabilityService,
$q = this.$q;
// Given a models object (containing key-value id-model pairs)
// create a function that will look up from the capability
// service based on id; for handy mapping below.
function capabilityResolver(models) {
return function (id) {
var model = models[id];
return model ?
capabilityService.getCapabilities(model) :
undefined;
};
}
instantiate = this.instantiate;
// Assemble the results from the model service and the
// capability service into one value, suitable to return
// from this service. Note that ids are matched to capabilities
// by index.
function assembleResult(ids, models, capabilities) {
// from this service.
function assembleResult(models) {
var result = {};
ids.forEach(function (id, index) {
if (models[id]) {
// Create the domain object
result[id] = new DomainObjectImpl(
id,
models[id],
capabilities[index]
);
result[id] = instantiate(models[id], id);
}
});
return result;
}
return modelService.getModels(ids).then(function (models) {
return $q.all(
ids.map(capabilityResolver(models))
).then(function (capabilities) {
return assembleResult(ids, models, capabilities);
});
});
return modelService.getModels(ids).then(assembleResult);
};
return DomainObjectProvider;

View File

@ -25,14 +25,16 @@
* DomainObjectProviderSpec. Created by vwoeltje on 11/6/14.
*/
define(
["../../src/objects/DomainObjectProvider"],
function (DomainObjectProvider) {
[
"../../src/objects/DomainObjectProvider",
"../../src/objects/DomainObjectImpl"
],
function (DomainObjectProvider, DomainObjectImpl) {
"use strict";
describe("The domain object provider", function () {
var mockModelService,
mockCapabilityService,
mockQ,
mockInstantiate,
provider;
function mockPromise(value) {
@ -57,18 +59,15 @@ define(
"modelService",
[ "getModels" ]
);
mockCapabilityService = jasmine.createSpyObj(
"capabilityService",
[ "getCapabilities" ]
);
mockQ = {
when: mockPromise,
all: mockAll
};
mockInstantiate = jasmine.createSpy("instantiate");
mockInstantiate.andCallFake(function (model, id) {
return new DomainObjectImpl(id, model, {});
});
provider = new DomainObjectProvider(
mockModelService,
mockCapabilityService,
mockQ
mockInstantiate
);
});
@ -86,10 +85,11 @@ define(
result;
mockModelService.getModels.andReturn(mockPromise({ a: model }));
result = provider.getObjects(ids).testValue;
expect(mockInstantiate).toHaveBeenCalledWith(model, 'a');
expect(result.a.getId()).toEqual("a");
expect(result.a.getModel()).toEqual(model);
});
});
}
);
);