c# - Binding one class to several interfaces as singleton -
i have instance 2 interfases iinterface1
, iinterface2
,
public interface iinterface1 {...} public interface iinterface2 {...}
and 1 implementation of these interfaces implclass
.
public class implclass : iinterface1, iinterface2 {...}
i have sure application has 1 instance of implclass, used iinterface1 , iinterface2. i'm using ninject dependency injection. qustion is: code below meet requirements?
... bind<iinterface1>().to<implclass>().using<singletonbehavior>(); bind<iinterface2>().to<implclass>().using<singletonbehavior>(); ...
or code create 2 instances of implclass, eash interface?
with ninject can this:
var impl = new impl(); container.bind<iint1>().tomethod(c => impl); container.bind<iint2>().tomethod(c => impl);
when impl
class has dependencies can't ninject inject, can this:
container.bind<impl>().toself().insingletonscope(); container.bind<iint1>().tomethod(c => c.kernel.get<impl>()); container.bind<iint2>().tomethod(c => c.kernel.get<impl>());
nice , clean.
Comments
Post a Comment