Skip to content

Commit

Permalink
feat(IoC): Add more advanced IoC with componentScan
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander Koenders committed Sep 10, 2018
1 parent 355a96d commit fad55c7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 45 deletions.
61 changes: 57 additions & 4 deletions src/ioc/ioc.ts
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));
});
}
}
8 changes: 8 additions & 0 deletions src/ioc/iocUtils.ts
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 };
41 changes: 0 additions & 41 deletions src/ioc/loader.ts

This file was deleted.

0 comments on commit fad55c7

Please sign in to comment.