Skip to content

#102 simplify syntax #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -4,4 +4,5 @@ src/**/*.js*
*.d.ts
dist
usage.js
CURRENT-CHANGELOG.txt
CURRENT-CHANGELOG.txt
yarn.lock
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -209,6 +209,21 @@ container.register([
{ token: App, useClass: App }
]);
```
In such case it is redundant to use `Inject()` decorator, dependency will be injected automatically.
```typescript
@Injectable()
class AppService {
constructor(private http: HttpService) {} // instead of @Inject(HttpService) private http: HttpService
}

@Injectable()
class HttpService {

}

container.register([HttpService, AppService])
```


## Contribution
Become a contributor to this project. Feel free to submit an [issue](https://github.com/thohoh/container-ioc/issues) or a pull request.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -58,6 +58,7 @@
"gulp-tslint": "8.1.2",
"gulp-typescript": "3.2.2",
"mocha": "3.5.3",
"reflect-metadata": "^0.1.12",
"tslint": "5.7.0",
"tslint-microsoft-contrib": "5.0.1",
"typescript": "2.5.3"
12 changes: 12 additions & 0 deletions src/lib/decorators.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ import { IInjectionMd, ProviderToken } from './interfaces';
import { INJECTABLE_MD_KEY, INJECTIONS_MD_KEY } from './metadata/keys';
import { IMetadataAnnotator } from './metadata/metadata-annotator.interface';
import { AnnotatorProvider } from './metadata/index';
import 'reflect-metadata';

const MetadataAnnotator: IMetadataAnnotator = AnnotatorProvider.get();

@@ -14,6 +15,17 @@ export function Injectable(injections?: ProviderToken[]) {
injections.forEach(token => injectionsMd.push(token));
MetadataAnnotator.defineMetadata(INJECTIONS_MD_KEY, injectionsMd, target);
}

const parameters = Reflect.getMetadata('design:paramtypes', target);
if (parameters && Array.isArray(parameters)) {
const injectionsMd: IInjectionMd[] = MetadataAnnotator.getMetadata(INJECTIONS_MD_KEY, target) || [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! Thank you for your pull request, I really appreciate your input. Do some changes so I can merge it, just simplify this expression so it can be read and understood easily by others. Getting rid of logical "OR" and putting things in self-describing variables might suffice.

parameters.forEach((param, index) => {
if (param['DI:injectable'] && !injectionsMd[index]) {
injectionsMd[index] = param;
}
});
MetadataAnnotator.defineMetadata(INJECTIONS_MD_KEY, injectionsMd, target);
}
};
}

18 changes: 18 additions & 0 deletions src/tests/decorators.spec.ts
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import { Container } from '../lib/index';
import 'mocha';
import { expect } from 'chai';
import { Inject, Injectable } from '../lib/decorators';
import { RegistrationProvider } from '../lib/interfaces';

/* tslint:disable: no-unused-expression max-classes-per-file*/

@@ -34,6 +35,23 @@ describe('Decorators', () => {
expect(instanceB.a).to.be.instanceOf(A);
});

it('should inject dependency in to the constructor of a resolved class wihout @Inject keyword', () => {

@Injectable()
class A { }

@Injectable()
class B {
constructor(private a: A) { }
}

container.register([A, B]);

const instanceB = container.resolve(B);

expect(instanceB.a).to.be.instanceOf(A);
});

it('should throw an error when wrong token was provided not registered token', () => {
@Injectable()
class A {}