3

I have a service that receives a

 public ServiceManagement(List<IWorker> workers)

And I have the workers registered:

this.Container.RegisterType<IWorker, Worker>

I would like to tell unity when resolving that list to create exactly N instances of Worker.

Anyone can help?

I tried the following:

Receiving an array instead of a List and registering like so:

for (int i = 0; i < N; i++)
{
    this.Container.RegisterType<IWorker, Worker>(i.ToString());
}

But this only works for different implementations of IWorker not the same.

Thanks in advance!

4 Answers 4

1

You should't register the same thing more then once. What you should do is to create factory of your Worker. Pass the number of instances to your factory. Then in service constructor pass the factory that will create the list, not the list itself.

In the container, register the factory, so whenever you will like to pass diffrent object type to the service you only swap the factory.

Some example code:

public class WorkerFactory :IFactory<IWorker>
{
public IEnumerable<IWorker> Create(int n)
{
var workers = new List<IWorker>();
for(int i = 0; i<n;i++)
workers.Add(new Worker());
return workers;
}
}

 public ServiceManagement(IFactory<IWorker> workersFactory)
{
Workers = workersFactory.Create(5);
}
1
  • Still kind of sucks since I was trying to avoid the "new Worker()" since the worker has dependencies which I wanted to be resolved by Unity
    – Hugo Rocha
    Aug 18, 2017 at 13:50
1

I ended up doing this way,

Creating a factory doesn't solve the dependencies of the worker class unless you pass them to the factory itself, which would mean have a factory for each type of worker which adds too much overhead.

Could be done by resolving N times the Worker when registering the type, and registering the List with those instances, but that will make the resolving dependent on the order of the RegisterType, which is also bad. It would also resolve the instances before you actually needed them.

Eventually I did an InjectionFactory:

this.Container.RegisterType<List<IWorker>>(new InjectionFactory(c =>
        {
            var workers = new List<IWorker>();

            for (int i = 0; i < N; i++)
            {
                workers.Add(c.Resolve<IWorker>());
            }

            return workers;
        }));

This way it will only resolve the instances when needed and Unity will take care of resolving IWorker dependencies on its own.

1
  • 1
    I think that's the best approach. See my answer. :)
    – Randy Levy
    Aug 18, 2017 at 15:43
0

I favor Crekate answer, but if you still want to go the trivial way, this should work:

        const int N = 5;
        UnityContainer container = new UnityContainer();

        container.RegisterType<IService, ServiceManagement>();
        for (int i = 0; i < N; i++)
        {
            container.RegisterType<IWorker, Worker>(i.ToString());
        }

        container.RegisterType<IList<IWorker>, IWorker[]>();

        var service = container.Resolve<IService>();
        Console.WriteLine("count:"+service.RefCount());

and some more code not so important for the answer:

    public interface IService
    {
        int RefCount();
    }
    public interface IWorker
    {
    }

    public class ServiceManagement:IService
    {
        IList<IWorker> _workers;
        public ServiceManagement(IList<IWorker> workers)
        {
            _workers = workers ?? new List<IWorker>(0);
        }
        public int RefCount()
        {
            return _workers.Count;
        }
    }

    public class Worker : IWorker
    {
    }
1
  • Good tip, this was actually what I was aiming for with the for but like I said in the question doesn't work unless you specify different implementations
    – Hugo Rocha
    Aug 18, 2017 at 15:16
0

You don't need to create multiple named registrations. Named registrations are usually used when the named registration is different. e.g. different implementation type, or different dependencies, etc.

A factory implementation will work but, instead of creating a separate factory class, why not use Unity's InjectionFactory? This saves creating a separate factory class and the service parameters can be the actual dependencies instead of an object that will be able to supply the dependencies. Another benefit is that the factory class is not dependent on Unity (assuming you want the factory class to use Resolve() to construct the object graph of the objects the factory is returning).

In the example below then we can properly resolve an IWorker without creating a factory class or passing an IUnityContainer instance to a custom factory:

var container = new UnityContainer();

container.RegisterType<IWorker, Worker>();
container.RegisterType<List<IWorker>>(new InjectionFactory(c =>
{
    var workers = new List<IWorker>();
    for (var i = 0; i < 5; i++)
    {
        workers.Add(c.Resolve<IWorker>());
    }

    return workers;
}));

var service = container.Resolve<ServiceManagement>();

If different List<IWorker> are required (e.g. one list for ServiceManagement and a different list for OrderManagement) then we could register different implementations for each and then wire them up by name to the correct Service:

var container = new UnityContainer();

container.RegisterType<IWorker, Worker>();

container.RegisterType<List<IWorker>>(typeof(ServiceManagement).Name,
    new InjectionFactory(c =>
    {
        var workers = new List<IWorker>();
        for (var i = 0; i < 5; i++)
        {
            workers.Add(c.Resolve<IWorker>());
        }

        return workers;
    }));

container.RegisterType<List<IWorker>>(typeof(OrderManagement).Name,
    new InjectionFactory(c =>
    {
        var workers = new List<IWorker>();
        for (var i = 0; i < 20; i++)
        {
            workers.Add(c.Resolve<IWorker>());
        }

        return workers;
    }));

container.RegisterType<ServiceManagement>(
    new InjectionFactory(c => new ServiceManagement(c.Resolve<List<IWorker>>(typeof(ServiceManagement).Name))));
container.RegisterType<OrderManagement>(
    new InjectionFactory(c => new OrderManagement(c.Resolve<List<IWorker>>(typeof(OrderManagement).Name))));

var service = container.Resolve<ServiceManagement>();
var order = container.Resolve<OrderManagement>();
1
  • Yeah thats exactly what I did, wish you had answered sooner, would saved me a bunch of time :)
    – Hugo Rocha
    Aug 18, 2017 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.