Ninject - 使用参数/实体框架连接字符串绑定构造函数
请原谅我的无知,但我对 IOC 和 NinJect 很陌生。我一直在寻找易于理解的解决方案,但到目前为止它们还没有找到。
到目前为止,我有以下内容并且所有工作都按预期进行:
private class StandardModule : NinjectModule
{
public override void Load()
{
Bind<ILog>().To<NLogLogger>(); // Use NLog
Bind<IMyEntityFrameWorkRepository().To<MyEntityFrameWorkRepository>();
}
}
MyEntityFrameWorkRepository 然后通过 app/web.config 中声明的连接字符串创建自己的 EF DbContext:
public class MyDbContext : DbContext
{
public MyDbContext() : base("MyAppConfig")
{
}
........
}
但是!我的目标是这样的 - 我意识到这种语法是“废话”(我认为我可能也必须 IOC MyDbConext),但我希望“伪代码”传达了我的愿望:
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) // will accept a standard SQL connection string
{
}
........
}
我会非常感谢 IOC / NinJect 专家的一些反馈,因为我确信任何“模式”在其他场景中都非常有用。
Please forgive my ignorance, but I am very new to IOC and NinJect. I have searched for high and low for easily understandable solutions but so far they have eluded me.
So far I have the following and all works as expected:
private class StandardModule : NinjectModule
{
public override void Load()
{
Bind<ILog>().To<NLogLogger>(); // Use NLog
Bind<IMyEntityFrameWorkRepository().To<MyEntityFrameWorkRepository>();
}
}
MyEntityFrameWorkRepository then creates its own EF DbContext via a connection string declared in app/web.config:
public class MyDbContext : DbContext
{
public MyDbContext() : base("MyAppConfig")
{
}
........
}
HOWEVER!! My goal is something like this - I realise this syntax is "nonsense" (and I think I may have to IOC MyDbConext too) , but I hope the "pseudo-code" conveys my 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) // will accept a standard SQL connection string
{
}
........
}
I would truly appreciate some feedback from IOC / NinJect experts, since I am sure any "pattern" can be very useful in other scenarios.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
.WithConstructorArgument()
方法指定构造函数参数。第一个参数应该是构造函数参数的名称。}
You can use the
.WithConstructorArgument()
method to specify constructor arguments. The first argument should be the name of the constructor parameter.}
新版本的 Ninject 允许摆脱绑定定义中的魔术字符串。像这样的事情:
对于涉及泛型类型的绑定(例如将
ISomeService
绑定到SomeService
并且应该立即对所有可能的类型执行绑定),ToConstructor(需要新的表达式),因此
WithConstructorArgument
仍然是最简单的方法。例如:Newer versions of Ninject allow to get rid of magic strings in the binding definition. Something like this:
For bindings involving generic types (e.g. bind
ISomeService<T>
toSomeService<T>
and binding should be performed for all possible types at once),ToConstructor
cannot be used (a new expression is required), soWithConstructorArgument
remains the simplest approach. E.g.: