c# - Castle windsor resolve array with open generic and interface -
i resolve following castle windsor:
ienumerable<definition<ientity>>
at moment i'm getting ienumerable 1 object matches first implementation of ientity.
i array of
{ definition<entity1>, definition<entity2>, ... }
i have feeling sub resolver needed have no idea start.
update
var container = new windsorcontainer(); container.kernel.resolver.addsubresolver( new collectionresolver(container.kernel, true)); container.register(component.for(typeof (definition<>))); var bindir = hostingenvironment.mappath("~/bin"); var assemblyfilter = new assemblyfilter(bindir); container.register(types.fromassemblyindirectory(assemblyfilter) .basedon<ientity>() .unless(t => t.isabstract || t.isinterface) .withserviceallinterfaces() .lifestyletransient()); // doesn't work! var items = container.resolve(typeof(ienumerable<definition<ientity>>));
first, think should improve design bit. don't know actual context, believe intent following:
public interface ientity { } public class entity1 : ientity { } public class entity2 : ientity { } public abstract class definition<tentity> tentity : ientity { } public class entity1definition : definition<entity1> { } public class entity2definition : definition<entity2> { }
with design have problem following code not valid:
definition<ientity> definition = new entity1definition();
in order work should introduce covariant generic interface ientity type. more on covariance , contravariance can find here: covariance , contravariance in generics
so suggest introduce following interface:
public interface idefinition<out tentity> tentity : ientity { }
note out keyword marks interface covariant. , derive definition<tentity> interface:
public abstract class definition<tentity> : idefinition<tentity> tentity : ientity { }
now, when have set design in such way rest easy. can register components this:
windsorcontainer container = new windsorcontainer(); container.kernel.resolver.addsubresolver( new collectionresolver(container.kernel, true)); container.register(types.fromthisassembly() .basedon(typeof(idefinition<>)) .unless(t => t.isabstract || t.isinterface) .withservices(typeof(idefinition<ientity>)) .lifestyletransient());
and resolve them:
var items = container.resolveall(typeof(idefinition<ientity>));
note, resolve instances registered service in windsor should invoke resolveall method.
Comments
Post a Comment