Ninject - binding constructors with arguments / Entity Framework connection string
Asked Answered
J

2

20

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.

Jehu answered 21/7, 2011 at 12:16 Comment(0)
E
25

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);
    }

}

Ephemerid answered 21/7, 2011 at 13:24 Comment(5)
TY so much! .. I had seen this construct during my searching, but for some reason I was always (mistakenly) concerned about the "scope" of 'connectionString' ... but your concise example clearly illustrates that it is only the value at binding that is important.Jehu
is there a way to do it without the magic string or so that ninject knows which overload but decides itself where to acquire the values?Phosphorate
@Phosphorate I don't think you can do it in Ninject 2.2 (but don't take my word for it). However, you should read this blog post which discusses upcoming changes.Ephemerid
@Ephemerid Is it possible to call WithConstructorArgument multiple times for multiple constructor parameters?Jarvis
@The Muffin Man Yes it is!Pteridology
F
5

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");
Fairleigh answered 23/9, 2016 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.