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.
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?
– Guilherme Caixeta
I had not yet seen this approach in
Controllers
, but I use it when I develop with standardrepository
andservice
, in the case of Pository, I usually create a Generic class that injects theContext
, soon I need to pass thisContext
in the classes she inherits.– Barbetta
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
– Guilherme Caixeta
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.– Barbetta
If the answer has solved your problem, do not forget to give OK, so that the question is finalized.
– Barbetta
All right, I’ll close the topic here thanks man.
– Guilherme Caixeta