1

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'?

chester89
  • 8,328
  • 17
  • 68
  • 113

2 Answers2

2

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);
}
Alexandru Marculescu
  • 5,569
  • 6
  • 34
  • 50
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)

Community
  • 1
  • 1
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249