Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject and Provider Model with Parameterless Constructor

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);

    }
}
like image 486
CrazyCoderz Avatar asked Apr 19 '26 20:04

CrazyCoderz


1 Answers

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.

like image 76
Darin Dimitrov Avatar answered Apr 23 '26 23:04

Darin Dimitrov