-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(IoC): Add more advanced IoC with componentScan
- Loading branch information
Sander Koenders
committed
Sep 10, 2018
1 parent
355a96d
commit fad55c7
Showing
3 changed files
with
65 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,60 @@ | ||
import { Container, ContainerModule, interfaces } from 'inversify'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import 'reflect-metadata'; | ||
import { Container, inject } from 'inversify'; | ||
import { autoProvide, provide, fluentProvide } from 'inversify-binding-decorators'; | ||
|
||
const container = new Container(); | ||
export default class Ioc { | ||
private static METADATA_PROVIDE_KEY = 'inversify-binding-decorators:provide'; | ||
private static ALLOWED_EXTENSIONS = ['.js']; | ||
|
||
export { container, autoProvide, provide, inject, fluentProvide }; | ||
public constructor( | ||
private projectRoot: string, | ||
private loader: NodeRequire = require, | ||
private container: Container = new Container() | ||
) { } | ||
|
||
public componentScan(contextPaths: string[]) { | ||
contextPaths.forEach(contextPath => this.readDir(path.join(this.projectRoot, contextPath))); | ||
|
||
this.container.load(this.getAnnotatedDependencies()); | ||
} | ||
|
||
public bind<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>) { | ||
return this.container.bind<T>(serviceIdentifier); | ||
} | ||
|
||
public get<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>) { | ||
return this.container.get<T>(serviceIdentifier); | ||
} | ||
|
||
public getNamed<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, named: string | number | symbol) { | ||
return this.container.getNamed<T>(serviceIdentifier, named); | ||
} | ||
|
||
private readDir(dir: string) { | ||
fs.readdirSync(dir).forEach((entity) => { | ||
this.processEntity(path.join(dir, entity)); | ||
}); | ||
} | ||
|
||
private processEntity(entity: string) { | ||
if (fs.statSync(entity).isDirectory()) { | ||
this.readDir(entity); | ||
} else { | ||
this.loadFile(entity); | ||
} | ||
} | ||
|
||
private loadFile(filePath: string) { | ||
if (Ioc.ALLOWED_EXTENSIONS.indexOf(path.extname(filePath)) !== -1) { | ||
this.loader(filePath); | ||
} | ||
} | ||
|
||
private getAnnotatedDependencies() { | ||
return new ContainerModule(bind => { | ||
const provideMetadata: any[] = Reflect.getMetadata(Ioc.METADATA_PROVIDE_KEY, Reflect) || []; | ||
provideMetadata.map(metadata => metadata.constraint(bind, metadata.implementationType)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { fluentProvide, provide } from 'inversify-binding-decorators'; | ||
import { interfaces, inject } from 'inversify'; | ||
|
||
const provideSingleton = (identifier: interfaces.ServiceIdentifier<any>) => { | ||
return fluentProvide(identifier).inSingletonScope().done(); | ||
}; | ||
|
||
export { provideSingleton, fluentProvide, inject, provide }; |
This file was deleted.
Oops, something went wrong.