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());
有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top