I have asp.net mvc application. I have created a custom membership provider that is bound in web.config like this:
<membership defaultProvider="MyMembershipProvider">
<providers>
<clear/>
<add name="MyMembershipProvider" type="MyProject.Infrastructure.Security.MyMembershipProvider" />
</providers>
</membership>
I use Castle Windsor for IoC. Usually I use the constructor injection something like:
SomeController(ISomething something)
{
something.do();
...
}
I can not use this for the custom membership class because I get an error from web.config that I don't have a parameterless constructor. But if I used a parameterless constructor the object wouldn't be initialized.
So I used property injection like so:
<castle>
<components>
<component id="usersRepos" service="MyProject.Core.Repositories.IUserRepository, MyProject.Core" type="MyProject.Data.RepositoryImplementations, MyProject.Data"></component>
</components>
</castle>
...
public class MyMembershipProvider : MembershipProvider
{
public IUserRepository UserRepository{get; set;}
public override bool ValidateUser(string username, string password)
{
if (UserRepository.UserExists(username, password))
{
return true;
}
return false;
}
}
And I get object reference not found exception because the object (UserRepository) has not been initialized.