Controller Base Dependency Injection

Asked

Viewed 655 times

4

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

I’m using Unity to inject addiction.

Below follows the parent controller code:

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

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

Contoller daughter, here is generated the error as it is necessary to pass the Ifiltroservice due to the parent class builder:

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

I want to know the best way to do that and how to pass the builder from here.

3 answers

2


When you inherit a class that has a constructor with parameters it is necessary to pass it to the base of him, that is, you need to pass the IFiltroServico in the base

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

    }
}

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

EDIT

Here’s an example I usually use when I use the pattern Repository

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)
     { }       
}
  • Ah yes I understood, I had even started to pass the builder to the child classes, but I still have a doubt beyond which I have already healed. For the performance of the Dependency Injection this would be a good practice, in case to pass the constructor to the child classes?

  • I had not yet seen this approach in Controllers, but I use it when I develop with standard repository and service, in the case of Pository, I usually create a Generic class that injects the Context, soon I need to pass this Context in the classes she inherits.

  • I understand, this controller standard is also a little new to me, but I really liked how to work with it. But regarding the injection of dependency by what I saw I will use a protected readonly in the parent class and call only the builder in daughters. This as far as I have seen solves my problem and is still a good practice. Thanks

  • Yes, yes, it is so, as I said, when the parent class has a parameterized constructor, it must be passed on base of the child classes.

  • If the answer has solved your problem, do not forget to give OK, so that the question is finalized.

  • All right, I’ll close the topic here thanks man.

Show 1 more comment

1

In dependency injection, you can inject a builder.

Ex:

container.RegisterType<IDbGerenciador, DbGerenciador>(new InjectionConstructor(Provedor.PostgreSql, _conexaoString));
  • Interestingly, you would have some article showing more of the use of D.I so?

1

Just to complement the answer, after a few researches and gives reply of the above Manole I solved my problem as follows:

When performing the Ifiltro instance I create it as protected and readonly, that in the parent controller base, follows below:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.