Handling application settings with ninject and convention based binding

荒凉一梦 提交于 2019-12-11 06:24:03

问题


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

回答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



来源:https://stackoverflow.com/questions/11907306/handling-application-settings-with-ninject-and-convention-based-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!