乐闻世界logo
搜索文章和话题

How to inject an asynchronous dependency in inversify?

1个答案

1

In Inversify, to inject asynchronous dependencies, we typically need to wrap these dependencies within a synchronous factory function that returns a Promise. Inversify itself does not directly support asynchronous dependency injection because its design goal is for a synchronous dependency injection container. However, we can indirectly achieve asynchronous dependency injection through certain design patterns. Here is one way to implement this functionality:

Using Factory Methods

  1. Define the Asynchronous Dependency: First, define your asynchronous dependency. This is typically a function returning a Promise or a class that asynchronously fetches resources.

    typescript
    class AsyncDependency { async getData() { const data = await fetchSomeData(); return data; } }
  2. Create a Factory Function: Next, create a factory function responsible for instantiating the asynchronous dependency. While the factory function itself is synchronous, it returns a Promise resolving to an instance of the asynchronous dependency.

    typescript
    const asyncDependencyFactory = () => { const asyncDependency = new AsyncDependency(); return asyncDependency; };
  3. Register the Factory in the Inversify Container: Bind the factory function to the Inversify container using the toFactory method.

    typescript
    import { Container, interfaces } from "inversify"; const container = new Container(); container.bind<AsyncDependency>("AsyncDependency").toFactory<AsyncDependency>((context: interfaces.Context) => asyncDependencyFactory);
  4. Inject and Use the Factory: In the class requiring the asynchronous dependency, inject this factory and use it to create the dependency instance.

    typescript
    import { inject, injectable } from "inversify"; @injectable() class SomeService { constructor(@inject("AsyncDependency") private asyncDependencyFactory: () => Promise<AsyncDependency>) {} async useDependency() { const asyncDependency = await this.asyncDependencyFactory(); const data = await asyncDependency.getData(); console.log(data); } }

Summary

Although Inversify does not directly support asynchronous dependency injection, using factory methods effectively encapsulates asynchronous behavior within a manageable synchronous API. This approach preserves the synchronous and predictable nature of the Inversify container while enabling flexibility for asynchronous operations.

Through the above example, we can see how Inversify manages dependencies requiring asynchronous initialization, ensuring the overall architecture remains clear and consistent.

2024年8月3日 16:46 回复

你的答案