我有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,Album ],autoSchemaSync: true,}); } } export { DBConnectionManager };
在我创建连接后,我想将连接绑定到我的容器中:
kernel.bind<Connection>('DefaultConnection').toConstantValue(getConnectionManager().get());
然后我想把它注入另一个类:
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; }
因此,在我的容器配置中,如何绑定需要等待CreateConnection的DefaultConnection
我可以做异步并等待,但我想知道是否有更清洁的方法来实现这一点在inversify
解决方法
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); }); }); }; });
然后您可以注入并使用提供程序.唯一的问题是它需要两步初始化:构造函数注入和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 }); } }
我们正在研究new feature以简化异步值的注入.此功能将包含在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({}); } }