简体   繁体   中英

How to Inject dependencies inside a class

I have 2 services. CarsController and CarsFetcherService

I also have a class called Car .

The CarsController has a method createCar() that creates an instance of Car everytime it is called. Each Car instance needs to be provided with the CarsController service.

With the code below, each Car instances gets undefined instead of the CarsController dependency. I use the library typedi to illustrate the problem I want to know in general how this is done. An answer with other library link inversify or Nestjs or even Angular would also be helpful.

export class Car {
    @Inject() // this is not working here
    private carsController: CarsController; // this becomes undefined
    private carRecipe: string;

    constructor(carRecipe: string) {
        this.carRecipe = carRecipe;
        this.carsController.notifyNetwork('HelloWorld, I am created!');
    }
}

@Service()
export class CarsController {

    @Inject()
    private carsNetworkService: CarsFetcherService;
    private cars: Car[] = [];

    constructor() {
    }

    createCar() {
        const carRecipe = this.carsNetworkService.getRequestSomething();
        const car = new Car(carRecipe);
        this.cars.push(car);
    }

    notifyNetwork(msg: string) {
        this.carsNetworkService.postRequestSomething(msg);
    }
}

@Service()
export class CarsFetcherService {
    getRequestSomething() {
        return 'this is how to make a car';
    }

    postRequestSomething(msg:string) {
        console.log(msg)
    }
}

const carsController = Container.get(CarsController)
// Creating 4 cars
carsController.createCar()
carsController.createCar()
carsController.createCar()
carsController.createCar()

I think, that when using typedi , and other similar DI frameworks, you should separate your classes into two categories. First categories would be services: typedi would guarantee that there's only one instance of each, typedi would create those instances and inject everything. Second category would be ordinary objects, that you create and manage on your own.

So, if you want to create as many instances of Car as you like, don't use DI for it - you already create it in a service that has direct references to everything Car needs:

export class Car {
    // deleted inject decorator

    constructor(private carsController, private carRecipe: string) {
        this.carsController.notifyNetwork('HelloWorld, I am created!');
    }
}

@Service()
export class CarsController {

    @Inject()
    private carsNetworkService: CarsFetcherService;
    private cars: Car[] = [];

    constructor() {
    }

    createCar() {
        const carRecipe = this.carsNetworkService.getRequestSomething();
        const car = new Car(this, carRecipe);
        this.cars.push(car);
    }

    notifyNetwork(msg: string) {
        this.carsNetworkService.postRequestSomething(msg);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM