I am implementing a custom RoleProvider and would like to use Ninject however I am faced with a parameterless constructor issue. Any thoughts on how to inject for this??
public class EFRoleProvider:RoleProvider
{
private readonly IRepository _repository;
// I want to INJECT this GOO here!
public EFRoleProvider()
{
IContextFactory contextFactory = new DbContextFactory<myEntities>();
_repository = new RepositoryBase(contextFactory);
}
}
You cannot inject something that is hardcoded. Sorry. No DI framework supports this. In your constructor you have hardcoded the instance, so this is no longer inversion of control. In order to perform inversion of control you need to define your layers as loosely coupled as possible:
public class EFRoleProvider: RoleProvider
{
private readonly IContextFactory _contextFactory;
public EFRoleProvider(IContextFactory contextFactory)
{
_contextFactory = contextFactory;
}
}
Now go ahead and configure your DI framework.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With