Wednesday 15 September 2010

c# - Structuremap 3 constructor that accepts list of all registered types/instances -



c# - Structuremap 3 constructor that accepts list of all registered types/instances -

i have object expects ienumerable<iplugintype> parameter it's constructor. have line in container configuration adds implementers of iplugintype:

x.scan(s => { ... s.addalltypesof<iplugintype>(); });

i've confirmed via container.whatdoihave() expected implementers registered, ienumerable not beingness populated.

i guess i'm beingness little optimistic thinking structuremap know mean, how can tell it?

if iplugintypes registered in container say, structuremap resolve correctly , pass 1 of each registered type ienumerable. found, need utilize interfaces, not abstract types.

here finish working illustration (or as dotnetfiddle):

using system; using system.collections.generic; using structuremap; namespace structuremaptest { public class programme { public static void main(string[] args) { var container = new container(); container.configure(x => { x.scan(s => { s.assemblycontainingtype<iplugintype>(); s.addalltypesof<iplugintype>(); }); x.for<imytype>().use<mytype>(); }); var mytype = container.getinstance<imytype>(); mytype.printplugins(); } } public interface imytype { void printplugins(); } public class mytype : imytype { private readonly ienumerable<iplugintype> plugins; public mytype(ienumerable<iplugintype> plugins) { this.plugins = plugins; } public void printplugins() { foreach (var item in plugins) { item.dosomething(); } } } public interface iplugintype { void dosomething(); } public class plugin1 : iplugintype { public void dosomething() { console.writeline("plugin1"); } } public class plugin2 : iplugintype { public void dosomething() { console.writeline("plugin2"); } } }

c# .net structuremap constructor-injection structuremap3

No comments:

Post a Comment