Handling application settings with ninject and convention based binding
Asked Answered
R

1

1

I am using Ninject in an MVC3 application and am trying to switch over to conventions based binding with ninject.extensions.conventions.

Now let's say I have a class that needs access to application settings such as:

public class Foo : IFoo
{
  public Foo(string connectionString)
  { ... }
}

I think I understand how to do normal binding with Ninject like this:

Bind<IFoo>()
  .To<Foo>()
  .WithConstructorArgument(
    "connectionString",
    ConfigurationManager.ConnectionStrings["Default"].ConnectionString);

But how do I do this using conventions instead?


Extra information if needed:

I'm using the nuget Ninject.MVC3 package and in App_Start/NinjectWebCommon.cs's RegisterServices this is all I currently have:

kernel.Bind(x => x
                .FromAssembliesMatching("*")
                .SelectAllClasses()
                .BindDefaultInterface());
Rhinelandpalatinate answered 10/8, 2012 at 18:32 Comment(0)
L
1

Ninject Conventions are there to get you 90% of the work for 10% of the cost. If you think you need to add an additional binding on top it, don't worry about it.

But in your scenario, I wouldn't worry too much about not injecting the connection string via the constructor and just grabbing it manually. Alternatively, you could setup an IDatabaseConfig interface and implementation that could do it for you, and your conventions based binding should just pick it right up. We do the later on our projects

Lag answered 10/8, 2012 at 20:54 Comment(1)
I did something like your IDatabaseConfig idea and it's working pretty well. It's nice to know I'm going in a good direction, thanks!Rhinelandpalatinate

© 2022 - 2024 — McMap. All rights reserved.