如何在反转中注入异步依赖?

use*_*086 7 inversion-of-control node.js typescript inversifyjs typeorm

我有TypeScript应用程序,我正在使用Inversify for IoC.

我有一个连接类:

'use strict';
import { injectable } from 'inversify';
import { createConnection, Connection } from "typeorm";
import { Photo, PhotoMetadata, Author, Album } from '../index';

@injectable()
class DBConnectionManager {

    public createPGConnection(): Promise<Connection> {
        return createConnection({
            driver: {
                type: "postgres",
                host: "host",
                port: 5432,
                username: "username",
                password: "password",
                database: "username"
            },
            entities: [
                Photo, PhotoMetadata, Author, Album
            ],
            autoSchemaSync: true,
        });

    }

}

export { DBConnectionManager };
Run Code Online (Sandbox Code Playgroud)

在我创建连接后,我想将连接绑定到我的容器中:

kernel.bind<Connection>('DefaultConnection').toConstantValue(getConnectionManager().get());
Run Code Online (Sandbox Code Playgroud)

然后我想把它注入另一个类:

import { injectable, inject } from 'inversify';
import { Connection, FindOptions } from "typeorm";
import { IGenericRepository, ObjectType } from '../index';


    @injectable()
    class GenericRepository<T> implements IGenericRepository<T> {

        private connection: Connection;
        private type: ObjectType<T>;

        constructor( @inject('DefaultConnection') connection: Connection) {
            this.connection = connection;
        }
Run Code Online (Sandbox Code Playgroud)

所以在我的容器配置中如何绑定需要等待CreateConnection的DefaultConnection我可以用async做等待但是我想知道是否有更简洁的方法来实现这个inversify

Rem*_*sen 13

Inversify 2.0包括对异步工厂的支持(AKA提供商)

提供者允许您按如下方式声明提供者:

container.bind<<DbClient>("DbClient").to(DbClientClass);

container.bind<interfaces.Provider<DbClient>>("Provider<DbClient>")
         .toProvider<DbClient>((context) => {
            return () => {
                return new Promise<DbClient>((resolve, reject) => {

                    // Create instance
                    let dbClient = context.container.get<DbClient>("DbClient");

                    // Open DB connection
                    dbClient.initialize("//connection_string")
                            .then(() => {
                                resolve(dbClient);
                            })
                            .catch((e: Error) => {
                                reject(e);
                            });
                });
            };
        });
Run Code Online (Sandbox Code Playgroud)

然后您可以注入并使用提供程序.唯一的问题是它需要两步初始化:constructor注入和async getDb()方法.

class UserRepository { 

    private _db: DbClient;
    private _dbProvider: Provider<DbClient>;

    // STEP 1
    // Inject a provider of DbClient to the constructor
    public constructor(
        @inject("Provider<DbClient>") provider: Provider<DbClient>
    ) { 
        this._dbProvider = provider;
    }

    // STEP 2
    // Get a DB instance using a provider
    // Returns a cached DB instance if it has already been created
    private async getDb() {
        if (this._db) return this._db;
        this._db = await this._dbProvider();
        return Promise.resolve(this._db);
    }

    public async getUser(): Promise<Users[]>{
        let db = await this.getDb();
        return db.collections.user.get({});
    }

    public async deletetUser(id: number): Promise<boolean>{
        let db = await this.getDb();
        return db.collections.user.delete({ id: id });
    }

}
Run Code Online (Sandbox Code Playgroud)

我们正在开发一种新功能来简化异步值的注入.此功能将包含在inversify 3.0中:

class UserRepository { 

    // STEP 1
    public constructor(
        @inject("Provider<DbClient>") private provider: Provider<DbClient>
    ) {}

    public async getUser(): Promise<Users[]>{
        // STEP 2: (No initialization method is required)
        let db = await this.provider.someFancyNameForProvideValue;
        return db.collections.user.get({});
    }
}
Run Code Online (Sandbox Code Playgroud)