IOC DI Multi-Threaded Lifecycle Scoping in Background Tasks

荒凉一梦 提交于 2019-12-01 05:26:45
Mark Seemann

Some DI Containers (e.g. (IIIRC) StructureMap) actually support Per Thread lifetime styles, but that's probably not going to help you, because it would inject IService into Controller on one thread, and then use it on a number of other threads.

What I would suggest is to leave the Controller implementations as is, because the fact that a particular implementation of IService isn't thread-safe, is an implementation detail.

Instead, create a thread-safe Adapter/Decorator of IService and inject that into Controller. Something like this:

public ThreadSafeService : IService
{
    private readonly IServiceFactory factory;

    public ThreadSafeService(IServiceFactory factory)
    {
        this.factory = factory;
    }

    public void DoSomething()
    {
        this.factory.Create().DoSomething();
    }
}

IServiceFactory could be declared like this:

public interface IServiceFactory
{
    IService Create();
}

If you implement IServiceFactory so that it creates a new instance of IService for every call to Create, you have a thread-safe implementation on hand, because there's no shared state.

This approach is container-agnostic and prevents Leaky Abstractions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!