asp.net mvc - Ninject - binding constructors with arguments / Entity Framework connection string -
please forgive ignorance, new ioc , ninject. have searched high , low understandable solutions far have eluded me.
so far have following , works expected:
private class standardmodule : ninjectmodule { public override void load() { bind<ilog>().to<nloglogger>(); // use nlog bind<imyentityframeworkrepository().to<myentityframeworkrepository>(); } }
myentityframeworkrepository creates own ef dbcontext via connection string declared in app/web.config:
public class mydbcontext : dbcontext { public mydbcontext() : base("myappconfig") { } ........ }
however!! goal - realise syntax "nonsense" (and think may have ioc mydbconext too) , hope "pseudo-code" conveys desire:
private class standardmodule : ninjectmodule { public override void load() { bind<ilog>().to<nloglogger>(); // use nlog string mysqlconnectionstring = myapp.getcommandlineargument("sqlconn"); // "data source=..." bind<imyentityframeworkrepository().to<myentityframeworkrepository>(mysqlconnectionstring); } } ................. public class mydbcontext : dbcontext { public mydbcontext( string sqlconnectionstring) : base(sqlconnectionstring) // accept standard sql connection string { } ........ }
i appreciate feedback ioc / ninject experts, since sure "pattern" can useful in other scenarios.
you can use .withconstructorargument()
method specify constructor arguments. first argument should name of constructor parameter.
public class standardmodule : ninjectmodule { public override void load() { string connectionstring = "..."; bind<imyentityframeworkrepository().to<myentityframeworkrepository>() .withconstructorargument("sqlconnectionstring", connectionstring); }
}
Comments
Post a Comment