c# - Best way to bind an interface to the result of another binding -
let's have following interface , class:
public interface imyservice { void dosomething(); } public class magicinterfaceimplementor<t> { public t getimplementation() { // not relevant. } }
the way instance of imyservice
through magicinterfaceimplementor<imyservice>
. can setup injection pretty magicinterfaceimplementor<imyservice>
:
bind<magicinterfaceimplementor<imyservice>().toself();
[strictly, isn't necessary in particular instance, i'm doing bit more binding in actual case.]
my question - how bind imyservice
?
i think can this, i'm not sure it's best way, since i'm explicitly calling kernel, typically frowned upon:
bind<imyservice>().tomethod(context => { return ((magicinterfaceimplementor<imyservice>) context.kernel.getservice(typeof(magicinterfaceimplementor<imyservice>))) .getimplementation(); });
any suggestions of more proper way appreciated.
maybe interesting magicimplementor does. more of guess, maybe provider might you.
https://github.com/ninject/ninject/wiki/providers,-factory-methods-and-the-activation-context
otherwise can write
bind<imyservice>().tomethod(ctx => ctx.kernel.get<magicinterfaceimplementor<imyservice>>().getimplementation());
Comments
Post a Comment