c# - StructureMap - Register a singleton for multiple interfaces -
i want register animals singletons, wrote structure map registry following code:
this.for<ilion>.use<lion>().singleton(); this.for<ielephant>.use<elephant>().singleton();
ilion , ielephant derive ianimal , want possibility animals @ once. tried:
this.for<ianimal>.add<lion>().singleton(); this.for<ianimal>.add<elephant>().singleton();
but gives me 2 different lion instances each interface:
public anyconstructor(ilion lion, ienumerable<ianimal> animals) { // lion == animals[0] should true here, false }
how can tell structure map instantiate one lion?
if mean getting 2 different lion instances, can use forward<tfrom, tto>()
method in registry:
this.for<ilion>().use<lion>().singleton(); this.for<ielephant>().use<elephant>().singleton(); this.forward<ilion, ianimal>(); this.forward<ielephant, ianimal>();
then, ianimal instances use getallinstances<t>()
method this:
var lion = objectfactory.getinstance<ilion>(); var elephant = objectfactory.getinstance<ielephant>(); var animal = objectfactory.getallinstances<ianimal>();
Comments
Post a Comment