1.6.1 • Published 3 years ago

@botflx/dependency-injection-container v1.6.1

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

Build Status npm version Coverage Status Lightweight GitHub issues GitHub license

Installation

npm i --save @botflx/dependency-injection-container
# or using yarn
yarn add @botflx/dependency-injection-container

API

Container creation

  • createServiceContainer(options: IServiceContainerFactoryOptions): IServiceContainer

Container usage

  • IServiceContainer.get<TService>(serviceName: string): TService
  • IServiceContainer.add(serviceName: string, constructor: Function): IServiceContainer
  • IServiceContainer.addService(serviceName: string, factory: Function): IServiceContainer
  • IServiceContainer.resolve<TService>(constructor: Function): TService
  • IServiceContainer.resolveFacory<TService>(factory: Function): TService
  • IServiceContainer.delete(serviceName: string): IServiceContainer

Decorators

  • @Inject(string)
  • @Service(string)

Usage

Container with decorators

import 'reflect-metadata'
import {createServiceContainer, Inject, Service, createReflectServiceLoader} from '@botflx/dependency-injection-container'

// Add metadata to this class, the service loader will
// be able to load those metadata and it will populate 
// the service container.
@Service('Logger')
class Logger {}

@Service('UserDao')
class UserDao {
    // You can inject services using the @Inject decorator.
    constructor(@Inject('Logger') logger: Logger) {}
}

// The service loader role is to collect services that use @Service decorators
const serviceLoader = createReflectServiceLoader([Logger, UserDao], [])
const container = createServiceContainer({ useReflection: true, serviceLoader: serviceLoader })

const logger = container.get<Logger>('Logger')
const userDao = container.get<UserDao>('UserDao')

More examples

Plain container examples

import {createServiceContainer} from '@botflx/dependency-injection-container'

// It creates a plain container without decorators supports.
// You can plain container when you don't want to import 'npm i reflect-metadata'
const plainContainer = createServiceContainer({ useReflection: false })

import 'reflect-metadata'
// It creates a container that supports decorators.
// You must import the reflect-metadata package in order to use this container type.
const container = createServiceContainer({ useReflection: true })

// By default this factory creates plain container
const anotherPlainContainer = createServiceContainer()

Plain container usage example

import {createServiceContainer, IServiceContainer} from '@botflx/dependency-injection-container'

const plainContainer = createServiceContainer({ useReflection: false })

class Logger {}
class UserDao {
    // When you use plain containers services will
    // receive the container as first parameter.
    constructor(container: IServiceContainer) {
        container.get<Logger>('Logger')
    }
}

// Add services using the `IServiceContainer.add()`
plainContainer
    .add('Logger', Logger)
    .add('UserDao', UserDao)

// Get user dao using `IServiceContainer.get()`
const userDao = plainContainer.get<UserDao>('UserDao')

function createUserFactory (logger: Logger) {
    return function (userName: string, password: string) { ... }
}

// Sometimes you can't use the class constructor, so you can use
// `IServiceContainer.addFactory()` as a fallback.
plainContainer
    .addFactory('createUser', container => createUserFactory(container.get('Logger')))
2.0.3

3 years ago

2.1.1

3 years ago

2.0.2

3 years ago

2.0.4

3 years ago

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago