Skip to content

Commit

Permalink
removed autowiring into inject methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sakren committed Dec 12, 2013
1 parent 96c4f6b commit 94bdd60
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 52 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,6 @@ constructor. This also means that you can not register new service with name "di
di.get('di');
```

## Inject methods

If your services using multiple inheritance and you want to inject some other services but it's parent need some different
services, then it is quite uncomfortable to set your services via constructor.

If DI find some methods with "inject" word in the beginning, it will automatically call and autowire these methods.

## Without configuration

Maybe it will be better for someone to use this DI without configuration, so here is example of application, translator
Expand Down Expand Up @@ -325,8 +318,7 @@ then relative to class of DI!
## Create instance

If you have got some other object which you want to use with other services, but can not use configuration or DI for this,
you can use `createInstance` method and DI will create new instance of your object with dependencies defined in constructor
or with inject methods.
you can use `createInstance` method and DI will create new instance of your object with dependencies defined in constructor.

```
var SuperClass = require('./mySuperClass');
Expand All @@ -351,6 +343,9 @@ $ npm test

## Changelog

* 2.0.0
+ Removed autowiring into `inject` methods (BC break!)

* 1.8.0
+ Better tests (mocha does not need to be installed globally)
+ Updated dependencies
Expand Down
8 changes: 0 additions & 8 deletions lib/DI.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
"test": "npm run test-node && npm run test-browser",
"test-build": "cd ./test/browser; simq build;",
"test-node": "mocha ./test/node/index.js --reporter spec",
"test-browser": "mocha-phantomjs ./test/browser/index.html"
"test-browser": "npm run test-build; mocha-phantomjs ./test/browser/index.html"
}
}
5 changes: 0 additions & 5 deletions src/DI.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ class DI
else
service = Helpers.createInstance(service, args, @)

if Object.prototype.toString.call(service) == '[object Object]' && injectMethods
for method of service
if method.match(/^inject/) != null
@inject(service[method], service)

return service


Expand Down
24 changes: 9 additions & 15 deletions test/browser/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,6 @@
};

DI.prototype.createInstance = function(service, args, instantiate, injectMethods) {
var method;
if (args == null) {
args = [];
}
Expand All @@ -537,13 +536,6 @@
service = Helpers.createInstance(service, args, this);
}
}
if (Object.prototype.toString.call(service) === '[object Object]' && injectMethods) {
for (method in service) {
if (method.match(/^inject/) !== null) {
this.inject(service[method], service);
}
}
}
return service;
};

Expand Down Expand Up @@ -1159,12 +1151,14 @@
app = di.createInstance(Application);
expect(app).to.be.an["instanceof"](Application);
expect(app.array).to.be.an["instanceof"](Array);
return expect(app.http).to.be.an["instanceof"](Http);
return expect(app.http).to.not.exists;
});
return it('should throw an error when service to inject does not exists', function() {
var app;
delete di.services.http;
app = di.createInstance(Application);
return expect(function() {
return di.createInstance(Application);
return di.inject(app.setHttp, app);
}).to["throw"](Error, "DI: Service 'http' was not found.");
});
});
Expand Down Expand Up @@ -1194,7 +1188,7 @@
expect(app).to.be.an["instanceof"](Application);
expect(app.namespace).to.be.equal('simq');
expect(app.array).to.be.eql([]);
return expect(app.http).to.be.an["instanceof"](Http);
return expect(app.http).to.not.exists;
});
it('should return always the same instance of Application', function() {
return expect(di.get('application')).to.be.equal(di.get('application'));
Expand Down Expand Up @@ -1336,7 +1330,7 @@
it('should return array with services for inject method', function() {
var args;
di.addService('http', Http);
args = Helpers.autowireArguments((new Application([])).injectHttp, [], di);
args = Helpers.autowireArguments((new Application([])).setHttp, [], di);
expect(args).to.have.length(1);
return expect(args[0]).to.be.an["instanceof"](Http);
});
Expand Down Expand Up @@ -1527,7 +1521,7 @@
this.array = array;
}

Application.prototype.injectHttp = function(http) {
Application.prototype.setHttp = function(http) {
this.http = http;
};

Expand Down Expand Up @@ -1608,7 +1602,7 @@
return {
"name": "dependency-injection",
"description": "Dependency injection with configuration and autowire for node js and browser",
"version": "1.7.3",
"version": "1.8.0",
"author": {
"name": "David Kudera",
"email": "sakren@gmail.com"
Expand Down Expand Up @@ -1716,7 +1710,7 @@
, 'recursive-merge': function(exports, module) { module.exports = window.require('recursive-merge/lib/Merge.js'); }

});
require.__setStats({"/lib/Service.js":{"atime":1386834783000,"mtime":1386834781000,"ctime":1386834781000},"/lib/Helpers.js":{"atime":1386834783000,"mtime":1386834781000,"ctime":1386834781000},"/lib/DI.js":{"atime":1386834783000,"mtime":1386834781000,"ctime":1386834781000},"easy-configuration/lib/EasyConfiguration.js":{"atime":1386835303000,"mtime":1385411214000,"ctime":1385450928000},"recursive-merge/lib/Merge.js":{"atime":1386835303000,"mtime":1385409966000,"ctime":1385450932000},"easy-configuration/lib/Extension.js":{"atime":1386835304000,"mtime":1385411214000,"ctime":1385450928000},"easy-configuration/lib/Helpers.js":{"atime":1386835304000,"mtime":1385411214000,"ctime":1385450928000},"/test/browser/tests/DI.coffee":{"atime":1386835304000,"mtime":1385452984000,"ctime":1385452984000},"/test/browser/tests/Helpers.coffee":{"atime":1386835304000,"mtime":1385450793000,"ctime":1385450793000},"/lib/DIConfigurator.js":{"atime":1386834783000,"mtime":1386834781000,"ctime":1386834781000},"/test/data/Application.coffee":{"atime":1386835304000,"mtime":1385450793000,"ctime":1385450793000},"/test/data/Http.coffee":{"atime":1386835304000,"mtime":1384940373000,"ctime":1384940373000},"/package.json":{"atime":1386835281000,"mtime":1386835278000,"ctime":1386835278000},"easy-configuration/package.json":{"atime":1386835040000,"mtime":1385450929000,"ctime":1385450929000}});
require.__setStats({"/lib/Service.js":{"atime":1386857223000,"mtime":1386857221000,"ctime":1386857221000},"/lib/Helpers.js":{"atime":1386856820000,"mtime":1386856806000,"ctime":1386856806000},"/lib/DI.js":{"atime":1386857268000,"mtime":1386857265000,"ctime":1386857265000},"easy-configuration/lib/EasyConfiguration.js":{"atime":1386835303000,"mtime":1385411214000,"ctime":1385450928000},"recursive-merge/lib/Merge.js":{"atime":1386835303000,"mtime":1385409966000,"ctime":1385450932000},"easy-configuration/lib/Extension.js":{"atime":1386835304000,"mtime":1385411214000,"ctime":1385450928000},"easy-configuration/lib/Helpers.js":{"atime":1386835304000,"mtime":1385411214000,"ctime":1385450928000},"/test/browser/tests/DI.coffee":{"atime":1386857629000,"mtime":1386857625000,"ctime":1386857625000},"/test/browser/tests/Helpers.coffee":{"atime":1386857560000,"mtime":1386857556000,"ctime":1386857556000},"/lib/DIConfigurator.js":{"atime":1386856820000,"mtime":1386856806000,"ctime":1386856806000},"/test/data/Application.coffee":{"atime":1386857310000,"mtime":1386857310000,"ctime":1386857310000},"/test/data/Http.coffee":{"atime":1386835304000,"mtime":1384940373000,"ctime":1384940373000},"/package.json":{"atime":1386857523000,"mtime":1386857522000,"ctime":1386857522000},"easy-configuration/package.json":{"atime":1386835040000,"mtime":1385450929000,"ctime":1385450929000}});
require.version = '5.5.1';

/** run section **/
Expand Down
7 changes: 4 additions & 3 deletions test/browser/tests/DI.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ describe 'DI', ->
app = di.createInstance(Application)
expect(app).to.be.an.instanceof(Application)
expect(app.array).to.be.an.instanceof(Array)
expect(app.http).to.be.an.instanceof(Http)
expect(app.http).to.not.exists

it 'should throw an error when service to inject does not exists', ->
delete di.services.http
expect( -> di.createInstance(Application)).to.throw(Error, "DI: Service 'http' was not found.")
app = di.createInstance(Application)
expect( -> di.inject(app.setHttp, app)).to.throw(Error, "DI: Service 'http' was not found.")

describe '#findDefinitionByName()', ->

Expand Down Expand Up @@ -77,7 +78,7 @@ describe 'DI', ->
expect(app).to.be.an.instanceof(Application)
expect(app.namespace).to.be.equal('simq')
expect(app.array).to.be.eql([])
expect(app.http).to.be.an.instanceof(Http)
expect(app.http).to.not.exists

it 'should return always the same instance of Application', ->
expect(di.get('application')).to.be.equal(di.get('application'))
Expand Down
2 changes: 1 addition & 1 deletion test/browser/tests/Helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe 'Helpers', ->

it 'should return array with services for inject method', ->
di.addService('http', Http)
args = Helpers.autowireArguments((new Application([])).injectHttp, [], di)
args = Helpers.autowireArguments((new Application([])).setHttp, [], di)
expect(args).to.have.length(1)
expect(args[0]).to.be.an.instanceof(Http)

Expand Down
2 changes: 1 addition & 1 deletion test/data/Application.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Application
constructor: (@array) ->


injectHttp: (@http) ->
setHttp: (@http) ->


prepare: (@namespace, @info) ->
Expand Down
2 changes: 1 addition & 1 deletion test/data/Application.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions test/node/DI.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ describe 'DI', ->
app = di.createInstance(Application)
expect(app).to.be.an.instanceof(Application)
expect(app.array).to.be.an.instanceof(Array)
expect(app.http).to.be.an.instanceof(Http)
expect(app.http).to.not.exists

it 'should throw an error when service to inject does not exists', ->
delete di.services.http
expect( -> di.createInstance(Application)).to.throw(Error, "DI: Service 'http' was not found.")
app = di.createInstance(Application)
expect( -> di.inject(app.setHttp, app)).to.throw(Error, "DI: Service 'http' was not found.")

describe '#findDefinitionByName()', ->

Expand Down Expand Up @@ -80,7 +81,7 @@ describe 'DI', ->
expect(app).to.be.an.instanceof(Application)
expect(app.namespace).to.be.equal('simq')
expect(app.array).to.be.eql([])
expect(app.http).to.be.an.instanceof(Http)
expect(app.http).to.not.exists

it 'should return always the same instance of Application', ->
expect(di.get('application')).to.be.equal(di.get('application'))
Expand Down
8 changes: 5 additions & 3 deletions test/node/DI.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/node/Helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe 'Helpers', ->

it 'should return array with services for inject method', ->
di.addService('http', Http)
args = Helpers.autowireArguments((new Application([])).injectHttp, [], di)
args = Helpers.autowireArguments((new Application([])).setHttp, [], di)
expect(args).to.have.length(1)
expect(args[0]).to.be.an.instanceof(Http)

Expand Down
2 changes: 1 addition & 1 deletion test/node/Helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 94bdd60

Please sign in to comment.