How to pass parameter to a class whose dependencies are wired through IoC container?

无人久伴 提交于 2019-12-13 02:38:28

问题


I have a class that works with a network via multiple resources. Its constructor receives arguments that are resolved at runtime by IoC container (StructureMap):

public NetworkWorker(IRetryService retryService, ILog log)
    { ... }

What I need is to control the number of resources this class uses on a use-case level - for example, client A need NetworkWorker instance that allows only one operation at a time, while client B need 10 ops at a time.
Currently this number is hardcoded in the constructor. The only way I see is to add a method void Configure(int resourceCount) that each client of NetworkWorker would call with a different value.
Or may be there's a better way I don't see?
This class can do different things, but number of resources is required for every method call (Get/Send/etc methods).
P.S. is this a known technique (with a Configure method)? If it is, what's the name for it? smth like 'two-step initialization'?


回答1:


I'll presume this NetworkWorker has multiple methods (otherwise you could just add a parameter to that single method).

You could use a factory pattern:

public interface INetworkWorkerFactory
{
    NetworkWorker Create(int numberOfResources);
}

public class NetworkWorkerFactory : INetworkWorkerFactory
{
    private readonly IContainer _container;
    public NetworkWorkerFactory(IContainer container)
    {
        _container = container;
    }

    public NewtorkWorker Create(int numberOfResources)
    {
        var retryService = _container.GetInstance<IRetryService>();
        var log = _container.GetInstance<ILog>();

        return new NewtorkWorker(retryService, log, numberOfResources);
    }
}

(or simply inject the required dependencies instead of the container) and then simply

private readonly INetworkWorkerFactory _networkWorkerFactory;

public C(INetworkWorkerFactory networkWorkerFactory)
{
    _networkWorkerFactory = networkWorkerFactory;
}

public void M()
{
    var networkWorker = _networkWorkerFactory.Create(10);
}



回答2:


Autofac uses delegate factories for this.

Other containers have equivalent mechanisms of providing auto-generated Abstract Factories - @ploeh has an awesome SO post aggregating examples.

The code in your question would look like so:

public delegate NetworkWorker Factory(IRetryService retryService);
public NetworkWorker(IRetryService retryService, ILog log)
{ ... }

A consumer would look like so (stolen from Alex's answer)

readonly NetworkWorker.Factory _networkWorkerFactory;

public C(NetworkWorker.Factory networkWorkerFactory)
{
    _networkWorkerFactory = networkWorkerFactory;
}

public void M()
{
    var networkWorker = _networkWorkerFactory(10);
    ...
}

The key difference is that you do not declare either an interface or an impl for the Abstract Factory - the delegate is the interface and Autofac 'just knows' to make the rest happen.

Another benefit of this approach (other than the code reduction) is that there's a natural path to transitioning the code to Pure DI)



来源:https://stackoverflow.com/questions/37829360/how-to-pass-parameter-to-a-class-whose-dependencies-are-wired-through-ioc-contai

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