Ninject - 使用参数/实体框架连接字符串绑定构造函数

发布于 2024-11-25 17:06:31 字数 1344 浏览 0 评论 0原文

请原谅我的无知,但我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神仙妹妹 2024-12-02 17:06:31

您可以使用 .WithConstructorArgument() 方法指定构造函数参数。第一个参数应该是构造函数参数的名称。

public class StandardModule : NinjectModule
{
    public override void Load()
    {
        string connectionString = "...";
        Bind<IMyEntityFrameWorkRepository().To<MyEntityFrameWorkRepository>()
            .WithConstructorArgument("sqlConnectionString", connectionString);
    }

}

You can use the .WithConstructorArgument() method to specify constructor arguments. The first argument should be the name of the constructor parameter.

public class StandardModule : NinjectModule
{
    public override void Load()
    {
        string connectionString = "...";
        Bind<IMyEntityFrameWorkRepository().To<MyEntityFrameWorkRepository>()
            .WithConstructorArgument("sqlConnectionString", connectionString);
    }

}

江南烟雨〆相思醉 2024-12-02 17:06:31

新版本的 Ninject 允许摆脱绑定定义中的魔术字符串。像这样的事情:

public class StandardModule : NinjectModule
{
    public override void Load()
    {
        string connectionString = "...";
        Bind<IMyEntityFrameWorkRepository()
            .ToConstructor(_ => new MyEntityFrameWorkRepository(connectionString);
    }
}

对于涉及泛型类型的绑定(例如将 ISomeService 绑定到 SomeService 并且应该立即对所有可能的类型执行绑定),ToConstructor(需要新的表达式),因此 WithConstructorArgument 仍然是最简单的方法。例如:

Bind(typeof(ISomeService<>))
    .To(typeof(SomeService<>))
    .WithConstructorArgument("someParam", "someValue");

Newer versions of Ninject allow to get rid of magic strings in the binding definition. Something like this:

public class StandardModule : NinjectModule
{
    public override void Load()
    {
        string connectionString = "...";
        Bind<IMyEntityFrameWorkRepository()
            .ToConstructor(_ => new MyEntityFrameWorkRepository(connectionString);
    }
}

For bindings involving generic types (e.g. bind ISomeService<T> to SomeService<T> and binding should be performed for all possible types at once), ToConstructor cannot be used (a new expression is required), so WithConstructorArgument remains the simplest approach. E.g.:

Bind(typeof(ISomeService<>))
    .To(typeof(SomeService<>))
    .WithConstructorArgument("someParam", "someValue");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文