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