Ninject映射的无参数实现

时间:2011-04-25 14:48:53

标签: asp.net-mvc-2 ninject membership-provider

我正在使用ninject创建自定义成员资格提供程序以绑定到sql类。

我的班级看起来像

public MyMembershipProvider(IUsersRepository userRepository)
{
   this.userRepository = userRepository;
}

如何从这个无参数构造函数创建?

1 个答案:

答案 0 :(得分:2)

kernel.Bind<IUsersRepository>().To<UsersRepository>();
kernel.Bind<MembershipProvider>().To<MyMembershipProvider>();

然后:

public class AccountController : Controller
{
    private readonly MembershipProvider _membershipProvider;
    public AccountController(MembershipProvider membershipProvider)
    {
        _membershipProvider = membershipProvider;
    }

    public ActionResult Foo()
    {
        // TODO: Use the membership provider to do some processing
        return View();
    }
}