i have following factory:
public class myfactory : imyfactory { private readonly ienumerable<imyservice> myservices public myfactory(ienumerable<imyservice> myservices) { this.myservices = myservices; } }
i registering ienumerable<imyservice>
this:
container.register<imyfactory, myfactory>(); container.registerall<imyservice>( s in appdomain.currentdomain.getassemblies() type in s.getexportedtypes() !type.isabstract typeof(imyservice).isassignablefrom(type) select type); container.verify();
then following results
// correctly resolves count of 4 implementations // of imyservice var myservices = container.getallinstances<imyservice>(); // incorrectly resolves ienumerable<imyservice> count // of 0 imyservices. var myfactory = container.getinstance<imyfactory>();
why factory cannot resolve collection of services?
i created following console application:
using system; using system.collections.generic; using system.linq; using simpleinjector; public interface imymanager { } public interface imyfactory { } public interface imyservice { } public class mymanager : imymanager { public mymanager(imyfactory factory) { } } public class myfactory : imyfactory { public myfactory( ienumerable<imyservice> services) { console.writeline("myfactory(count: {0})", services.count()); } } public class service1 : imyservice { } public class service2 : imyservice { } public class service3 : imyservice { } public class service4 : imyservice { } class program { static void main(string[] args) { var container = new container(); container.register<imyfactory, myfactory>(); container.register<imymanager, mymanager>(); container.registerall<imyservice>( nd in appdomain.currentdomain.getassemblies() type in nd.getexportedtypes() !type.isabstract typeof(imyservice).isassignablefrom(type) select type); var mymanager = container.getinstance<imymanager>(); console.writeline("imyservice count: " + container.getallinstances<imyservice>().count()); } }
when run it, outputs following:
myfactory(count: 4) imyservice count: 4
Comments
Post a Comment