Ninject and Provider Model with Parametric Constructor

I use my own RoleProvider and would like to use Ninject, but I ran into a constructor problem with no parameters. Any thoughts on how to enter 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);

    }
}
+3
source share
1 answer

You cannot enter something solid. I'm sorry. No DI infrastructure supports this. In your constructor, you hardcoded the instance, so this is no longer an inverse of control. To perform a control inversion, you need to define your layers as clearly as possible:

public class EFRoleProvider: RoleProvider
{
    private readonly IContextFactory _contextFactory;
    public EFRoleProvider(IContextFactory contextFactory)
    {
        _contextFactory = contextFactory;
    }
}

Now go and configure your DI infrastructure.

+1
source

All Articles