Dependency Injection in Controller Base

4

Well I'm learning to work with dependency injection now and would like to apply it to my project. However I came across the following difficulty, I have a base controller where it is inherited by three other base controllers that perform the overload and thus I can perform all the necessary functions in my system. However when trying to apply the dependency injunction the child classes ask me to pass the constructor object, then I would like to know how to handle it.

I'm using unity to do dependency injection.

Below is the parent controller code:

public class BaseController : ApiController
{
    public string[] includes = null;

    private readonly IFiltroServico servico;
    public BaseController(IFiltroServico _servico)
    {
        servico = _servico;
    }
}

Child contoller, here the error is generated because it is necessary to pass the IFiltroService due to the constructor of the parent class:

public abstract class BaseController<R> : BaseController
        where R : class
    {
 //services da controller;
    }

I want to know how best to do this and how to pass the constructor from here .

    
asked by anonymous 20.03.2018 / 21:12

3 answers

2

When you inherit a class that has a constructor with parameters, you have to pass this to the base of it, ie you need to pass IFiltroServico to base

public abstract class BaseController<R> : BaseController
        where R : class
{
    public BaseController(IFiltroServico servico)
        :base(servico)
    {

    }
}

obs underline before the name of the variable is a convention to indicate that the variables are private, in case it "should" be in the private property, since the IFiltroServico passed as parameter in the constructor is only in that scope.

EDIT

Here's an example I use when I use the Repository pattern

public abstract class CrudRepository<TEntity, TKey> : ICrudRepository<TEntity, TKey>
    where TEntity : class
{
    protected DbContext _context;
    public CrudRepository(DbContext context)
    {
        _context = context;
    }

    //Codigo aqui
} 

public class UsuarioRepository : CrudRepository<Usuario, int>
{
    public UsuarioRepository(DbContext context) : base(context)
     { }       
}
    
21.03.2018 / 02:21
1

Just to complement the answer, after some research and answer the manolo above I solved my problem as follows:

When I run the IFiltro instance I create it as protected and readonly, this in the parent controller base, below:

 public class BaseController : ApiController
    {
        public string[] includes = null;
        protected readonly IFiltroServico _servico;
        public BaseController(IFiltroServico servico)
        {
            _servico = servico;
        }
    }

In child classes, just follow the example of the answer constructor above.

    
21.03.2018 / 16:00
1

In dependency injection, you can inject a constructor.

Ex:

container.RegisterType<IDbGerenciador, DbGerenciador>(new InjectionConstructor(Provedor.PostgreSql, _conexaoString));
    
27.03.2018 / 22:31