0.2.5 • Published 6 years ago

@fun-coder/ioc v0.2.5

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

IOC Container

Build Status

Define classes

import { Context, injected } from '@fun-coder/ioc';

abstract class BaseService {
  abstract getName(): string;
}

class ServiceA extends BaseService {
  getName(): string {
    return 'Hello, I come from A';
  };
}

class ServiceB extends BaseService {
 
  @injected()
  serviceA: ServiceA;
  
  getName(): string {
    return 'Hello, I come from B';
  };
}

Create context

const context = Context.create(ServiceA, ServiceB);

Get instance from context

const serviceA = context.get(ServiceA);
serviceA.getName(); // Hello, I come from A;

const serviceB = context.get(ServiceB);
serviceB.getName(); // Hello, I come from B;

console.log(serviceB.serviceA === serviceA); // => true

Get instances by parent class

context.getAll(BaseService); // [serviceA, serviceB]
0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago