c# - How do I use StructureMap with generic unclosed types using Scan with a "greedy" constructor -
c# - How do I use StructureMap with generic unclosed types using Scan with a "greedy" constructor -
between various stack overflow questions , blog posts there pretty reasonable amount of documentation on topic of open generics , structuremap. unfortunately, must missing attempts @ using scan perform configuration along class implementation has "greedy" constructor have yet work.
i want structuremap grab instance of below class via references implemented interface. tocsvservice exists in unreferenced assembly called infrastructure. itocsvservice exists in referenced assembly called core. can see tocsvservice has "greedy" constructor.
public class tocsvservice<tsource> : itocsvservice<tsource> { public tocsvservice(icollection<tsource> collection) { } } i allow structuremap know tocsvservice via connectimplementationstotypesclosing method.
objectfactory.initialize(cfg => { cfg.scan(scan => { scan.assembly("infrastructure"); scan.withdefaultconventions(); // phone call structuremap cannot utilize tocsvservice // instance of itocsvservice (though wouldn't expect to) scan.connectimplementationstotypesclosing (typeof(itocsvservice<>)); }); }); from objectfactory.whatdoihave() method appears structuremap aware of tocsvservice.
however when specify itocsvservice<customerviewmodel> in controller constructor throws exception
structuremap exception code: 202 no default instance defined pluginfamily core.services.itocsvservice`1[[ui.models.machineform, ui, version=1.0.0.0, culture=neutral, publickeytoken=null]], core, version=1.0.0.0, culture=neutral, publickeytoken=null
i imagine because structuremap has no thought hand "greedy" tocsvservice constructor. there someway can create structuremap able play nice constructor? need switch constructor , set collection property after instantiation?
the question structuremap , generic types details trying not utilize scan so. answer provided joshua flanagan utilizes for(x).use(y) configuration, might work if wasn't scanning assembly because don't have reference tocsvservice.
edit
i wanted see if using different mechanism allow structuremap identify instances of tocsvservice<> have effect. changes what's reported in objectfactory.whatdoihave() , throws different exception. here's illustration of using addalltypesof.
objectfactory.initialize(cfg => { cfg.scan(scan => { scan.assembly("infrastructure"); scan.withdefaultconventions(); scan.addalltypesof(typeof(itocsvservice<>)); }); }); after using above dump objectfactory.whatdoihave() is
with configuration throw exception:
structuremap exception code: 202 no default instance defined pluginfamily system.collections.generic.icollection`1[[ui.models.machineform, ui, version=1.0.0.0, culture=neutral, publickeytoken=null]], mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089
to me exception indicates structuremap knows needs icollection<machineform> instantiate tocsvservice not know from. goes jimmy's comment below using structuremap , constructor setter injection. however, doesn't seem possible without adding explicit reference infrastructure assembly.
are there concrete implementations of itocsvservice? or open type tocsvservice?
connectimplementationstotypesclosing connecting concrete customerviewmodeltocsvservice itocsvservice<>. if want connect open classes open interfaces, you'll need:
for(typeof(itocsvservice<>)).use(typeof(tocsvservice<>)); here i'm connecting open interface type open class type.
c# asp.net-mvc generics dependency-injection structuremap
Comments
Post a Comment