Skip to content

Commit

Permalink
🌟 feat: add loader in factory configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor MENDELE committed May 7, 2020
1 parent ebcad46 commit d08b2bd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/factories/IServiceContainerFactoryOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* Represents options of IServiceContainerFactory.
*/
import {IServiceLoader} from '../loaders/IServiceLoader'

export interface IServiceContainerFactoryOptions {
/**
* If true the ServiceContainerFactory will returns a service container
Expand All @@ -14,4 +16,10 @@ export interface IServiceContainerFactoryOptions {
* `container.add()` multiple times with the same service name.
*/
allowServiceOverride?: boolean

/**
* Specify service you want to load by default.
* If you are using Reflection you can add new service with `@Service` and `createReflectServiceLoader`
*/
serviceLoader?: IServiceLoader
}
11 changes: 9 additions & 2 deletions src/factories/ServiceContainerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {OptionMapper} from '../mappers/OptionMapper'
*/
export const defaultOptions: IServiceContainerFactoryOptions = {
useReflection: false,
allowServiceOverride: false
allowServiceOverride: false,
serviceLoader: undefined
}

/**
Expand Down Expand Up @@ -43,9 +44,15 @@ export class ServiceContainerFactory implements IServiceContainerFactory {
create(): IServiceContainer {
const containerOptions = this._optionMapper.toContainerOptions(this._options)

return this._options.useReflection
const container = this._options.useReflection
? new ReflectServiceContainer(containerOptions)
: new ServiceContainer(containerOptions)

if (this._options.serviceLoader) {
this._options.serviceLoader.load(container)
}

return container
}

}
9 changes: 9 additions & 0 deletions tests/sandbox.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {createServiceContainer} from '../src'
import {createPlainServiceLoader} from '../src/factories/createPlainServiceLoader'

it('should work', function () {
const container = createServiceContainer({
useReflection: false,
serviceLoader: createPlainServiceLoader([], [])
})
})

0 comments on commit d08b2bd

Please sign in to comment.