Ninject构造函数参数

she*_*per 4 dependency-injection ninject

我有这个界面:

public interface IUserProfileService
{
    // stuff
}
Run Code Online (Sandbox Code Playgroud)

实施者:

public class UserProfileService : IUserProfileService
{
    private readonly string m_userName;

    public UserProfileService(string userName)
    {
        m_userName = userName;
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要将这个注入到这样的控制器中:

public class ProfilesController : BaseController
{
    private readonly IUserProfileService m_profileService;

    public ProfilesController(IUserProfileService profileService)
    {
        m_profileService = profileService;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何将此接口及其实现注册到Ninject容器中,以便在Ninject进入此服务的实例时传入userName参数.

我有什么想法可以达到这个目的吗?

ryb*_*ber 5

技术ninject的答案是使用构造函数参数,如下所示:

Bind<IUserProfileService>().To<UserProfileService>().WithConstructorArgument("userName", "karl");
Run Code Online (Sandbox Code Playgroud)

当然,你需要弄清楚"卡尔"的来源.这真的取决于你的应用程序.也许它是一个网络应用程序,它在HttpContex上?我不知道.如果它变得相当复杂,那么你可能想要编写一个IProvider而不是进行常规绑定.