C# Unity register a list of registered types and resolve by adding N instances -
i have service receives
public servicemanagement(list<iworker> workers)
and have workers registered:
this.container.registertype<iworker, worker>
i tell unity when resolving list create n instances of worker.
anyone can help?
i tried following:
receiving array instead of list , registering so:
for (int = 0; < n; i++) { this.container.registertype<iworker, worker>(i.tostring()); }
but works different implementations of iworker not same.
thanks in advance!
you should't register same thing more once. should create factory of worker. pass number of instances factory. in service constructor pass factory create list, not list itself.
in container, register factory, whenever pass diffrent object type service swap factory.
some example code:
public class workerfactory :ifactory<iworker> { public ienumerable<iworker> create(int n) { var workers = new list<iworker>(); for(int = 0; i<n;i++) workers.add(new worker()); return workers; } } public servicemanagement(ifactory<iworker> workersfactory) { workers = workersfactory.create(5); }
Comments
Post a Comment