Skip to content

Commit

Permalink
Merge pull request #25 from Spy-Seth/feature/singleton-by-default
Browse files Browse the repository at this point in the history
Service are now singleton by default
  • Loading branch information
armandabric committed Feb 25, 2016
2 parents 9fbac3f + 14f966e commit f99a361
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ContainerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var buildServiceDefinitionCollection = function buildServiceDefinitionCollection
value.name,
value.service,
new FunctionArgumentCollection(functionArgumentList),
value.singleton || undefined,
value.singleton,
new CallCollection(callList)
));
});
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var ServiceDefinition = function ServiceDefinition(name, serviceConstructor, fun
this.name = name;
this.serviceConstructor = serviceConstructor;
this.functionArgumentCollection = functionArgumentCollection;
this.isSingletonService = !!isSingletonService;
this.isSingletonService = (isSingletonService !== undefined ? !!isSingletonService : true);
this.callCollection = callCollection;
};

Expand Down
11 changes: 11 additions & 0 deletions tests/fixture/valid/ServiceE.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

/**
* A service with no dependency.
*
* @constructor
*/
var ServiceE = function ServiceE() {
};

module.exports = ServiceE;
4 changes: 4 additions & 0 deletions tests/fixture/valid/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ module.exports = {
51
]
}
},
{
'name': 'foo.serviceE',
'service': require('./ServiceE')
}
]
};
9 changes: 9 additions & 0 deletions tests/functionals/ContainerFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ describe('ContainerFactory', function () {
expect(firstCall).to.not.be.equal(secondCall);
});

it('should concider a service as singleton if it have no singleton configuration', function () {
var container = ContainerFactory.create(servicesConfigurationValid.services, servicesConfigurationValid.parameters);

var firstCall = container.getService('foo.serviceE');
var secondCall = container.getService('foo.serviceE');

expect(firstCall).to.equals(secondCall);
});

it('should throw an exception on undefined parameter', function () {
var container = ContainerFactory.create(servicesConfigurationValid.services, servicesConfigurationValid.parameters);

Expand Down

0 comments on commit f99a361

Please sign in to comment.