Using Ninject with Membership.Provider

前端 未结 4 1092
刺人心
刺人心 2021-01-11 14:19

I\'m new to Ninject and I\'m having problems using it with a custom membership provider.

My membership provider has a repository interface passed in. It looks like:<

相关标签:
4条回答
  • 2021-01-11 14:28

    Since the membership collection and the Membership.Provider instance are created before Ninject can instantiate them, you need to perform post creation activation on the object. If you mark your dependencies with [Inject] for your properties in your provider class, you can call kernel.Inject(Membership.Provider) - this will assign all dependencies to your properties.

    0 讨论(0)
  • 2021-01-11 14:47

    I haven't used Ninject ever. but in StructureMap i set this dependency:

    expression.For<MembershipProvider>().Add(System.Web.Security.Membership.Provider);
    

    and it works fine.

    0 讨论(0)
  • 2021-01-11 14:49

    The best solution I found was the following:

    private IRepository _repository;
    
    [Inject]
    public IRepository Repository
    {
        get { return _repository; }
        set { _repository= value; }
    }
    
    public CustomMembershipProvider()
    {
        NinjectHelper.Kernel.Inject(this);
    }
    

    Where NinjectHelper is a static helper class to get the Kernal from.

    0 讨论(0)
  • 2021-01-11 14:51

    This is how it should be done today with new versions of both MVC and Ninject (version 3):

    You have access to the DependencyResolver instance and Ninject sets itself as the current DependencyResolver. That way you don't need hacks to get access to the static Ninject kernel. Please note, my example uses my own IUserService repository for Membership...

    IUserService _userService = DependencyResolver.Current.GetService<IUserService>();
    
    0 讨论(0)
提交回复
热议问题