Skip to content

Latest commit

 

History

History
39 lines (31 loc) · 1.25 KB

async_factory_resolver.md

File metadata and controls

39 lines (31 loc) · 1.25 KB

Async factory resolver

RSDI intentionally does not provide the ability to resolve asynchronous dependencies. The container works with resources. All resources will be used sooner or later. The lazy initialization feature won't be of much benefit in this case. At the same time, mixing synchronous and asynchronous resolution will cause confusion primarily for the consumers.

The following approach will work in most scenarios.

// UserRepository.ts
class UserRepository {
  public constructor(private readonly dbConnection: any) {} // some ORM that requires opened connection

  async findUser() {
    return await this.dbConnection.find(/*...params...*/);
  }
}

// configureDI.ts
import { createConnections } from "my-orm-library";
import DIContainer, { factory, use } from "rsdi";

async function configureDI() {
  // initialize async factories before DI container initialisation
  const dbConnection = await createConnections();

  const container = new DIContainer();
  container.add({
    DbConnection: dbConnection,
    UserRepository: object(UserRepository).construct(use("DbConnection")),
  });
  return container;
}

// main.ts
const diContainer = await configureDI();
const userRepository = diContainer.get<UserRepository>("UserRepository");