c# - How do I retrieve objects in Ninject kernel parameters that need in constructor for initialization -


i'm using ninject ioc.

my question how retrieve object requires parameter in constructor.

below sample code:

//interface connection public interface iconnection {     idbconnection currentconnection { get; } }  //concret connection public class myconnection : iconnection {     public myconnection(idbconnection nativeconnection){  } }  //module ninject class module : ninjectmodule {     public override void load()     {         bind<iconnection>().to<myconnection>().insingletonscope();     } }  //native connection var sqlconn = new sqlceconnection();  //ninject kernel var ker = new standardkernel(new module()); return ker.get<iconnection>(); //how can pass parameters constructor of class "myconnection"?? 

you have add/define binding idbconnection. ninject pass automatically constructor. example

bind<idbconnection>.to<sqlceconnection>(); 

or can have constant in module

private static readonly sqlconnection = new sqlceconnection(); 

and bind interface that

bind<idbconnection>.toconstant(sqlconnection); 

best check this page more information.

update

i don't think it's sophisticated design. if want can directly pass parameter constructor.

ker.get<iconnection>(new constructorargument("nativeconnection",yourconnection)); 

i don't know why need that, , how works in singleton scope though.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -