Instantiate a class that inherits an interface class

Asked

Viewed 303 times

0

I have a class that inherits many interface classes, and I want to instantiate it to use only one method, how do I do? It is possible to do without passing all the necessary parameters?

public class PontoSituacaoService : IPontoSituacaoService,
        IMessageHandler<DespachoCreatedEvento, bool>,
        IMessageHandler<OcorrenciaAssociadaEvento, bool>,
        IMessageHandler<OcorrenciaDesassociadaEvento, bool>,
        IMessageHandler<PartilharOcorrenciaEvento, bool>,
        IMessageHandler<GrupoDespachosCreatedEvento, bool>,
        IMessageHandler<AlteracaoEstadoOcorrencia, bool>,
        IMessageHandler<NotificacaoManualEvento, bool>,
        IMessageHandler<OcorrenciaImportanciaModificadaEvento, bool>,
        IMessageHandler<AtualizarPOSITOcorrencia112, bool>,
        IMessageHandler<DanoHabitacionalCriadoEvento, bool>
{
    private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    private readonly IRepository<PontoSituacao> _pontoSituacaoRepository;
    private readonly IRepository<SumarioPontoSituacao> _sumarioPontoSituacaoRepository;

    private readonly IOcorrenciaService _ocorrenciaService;
    private readonly ILookupService<TipoPontoSituacao> _tipoPontoSituacaoService;
    private readonly IModuloInterfacePortal _moduloInterfacePortal;

    private readonly ILookupService<TemplateNotificacaoSINOP> _templateService;

    private readonly IMessageBus _bus;

    public PontoSituacaoService(IRepository<PontoSituacao> pontoSituacaoRepository,
                IOcorrenciaService ocorrenciaService,
                ILookupService<TipoPontoSituacao> tipoPontoSituacaoService,
                IMessageBus bus,
                IModuloInterfacePortal moduloInterfacePortal,
                IRepository<SumarioPontoSituacao> sumarioPontoSituacaoRepository,
                ILookupService<TemplateNotificacaoSINOP> templateService)
    {
        _pontoSituacaoRepository = pontoSituacaoRepository;
        _sumarioPontoSituacaoRepository = sumarioPontoSituacaoRepository;
        _ocorrenciaService = ocorrenciaService;
        _tipoPontoSituacaoService = tipoPontoSituacaoService;
        _bus = bus;
        _moduloInterfacePortal = moduloInterfacePortal;
        _templateService = templateService;
    }

public ActionConfirmation SaveOrUpdate(PontoSituacao pontoSituacao)
    {
        return SaveOrUpdate(pontoSituacao, true, false);
    }
}
  • @ramaral conceptually gives to answer this, at this point the question is conceptual. If he has an example then it becomes more practical and can be better answered.

  • With this alone we cannot understand the whole or the difficulty of instantiation. Give more details of the problem and if possible more code that is part of the problem. Leave out all the code that doesn’t help understand the problem, but leaves out nothing that is important to understand it.

  • The problem is that when I go to instantiate the class Pontosituacaoservice pts = new Pontosituacaoservice(); he wants me to pass all the parameters, I just want to use method qlq that is below that of this constructor, got it?

  • There is no method below the constructor.

  • is a method whatever is down there, I put the Saveorupdate method for you to see, ready I want to instantiate and call it(remembering that this class has "N" other methods implemented).

  • @vc_89 This class seems to me to be made to be used with depletion injection (Ioc container), and not be instantiated in the "hand" with new. I think your difficulty in instantiating this class is because of this.

  • The initial concept is that I need to save in another area(table) of my application called "Situation Point" when saved for example a form called "Population Movement" and in this form "Population Movement" the quantity field is greater than 20, so I need to use the saveorupdate function.

  • 1

    Just adding staff I declared a second empty constructor in the class I wanted to instantiate, so the parameters were optional, and it worked. Thank you all for your availability.

Show 3 more comments

1 answer

1


Class and interface are very different things, either it’s class or it’s interface. Classes and interfaces have in common that are types, that’s all.

The instantiation of a class is independent of the interfaces they implement (note that I did not use the term "inherit" which is not correct). It is the same as any case. In this case the problem is the constructor that requires objects to be instantiated to create the object. It is even necessary because of the implemented interfaces, but this is collateral.

After editing, you notice an extremely complex class that you can ask if you need it all. It gives the impression that you’re getting something ready (it looks like DDD) and you don’t understand what you’re doing. That’s a problem. In complex things like this you need a great domain of programming, object orientation, architecture, etc. Don’t use methodologies that you don’t master, it doesn’t work. Any methodology, good or bad, only works when the person dominates it. As wonderful as a methodology can be, and many are more marketing than reality, if you don’t fully understand how to use it, it’s worse than using something more basic. And this one seems to be quite complex, so much so that maybe it’s not even advantageous to your problem.

I would even say that it is necessary to observe if it is necessary to implement so many interfaces in this class, or even if this method should be part of this class. Whether it should be used so alone, or it should be static, or it should be part of another class. But it seems that this is a matter of architecture.

I’m critical of complex architectures, like this one. This is a very clear example of what I always talk about. To consume something very simple requires a very complex code. I’m sorry, but this code is an atrocity. He demands so much to use it that it’s better not to use it and do something else, which would kill the reason he created it.

Worse, as I understood, the bulk of this object serves more of architecture control than the domain of the problem, and if it is DDD it would be ironic.

Without changing the whole architecture I do not have a better solution, please install the object with everything it requires and use the method you need next. And yes, you will need to create at least 7 class objects that implement these interfaces just to consume a method that may not even be related to what you need.

On the other hand, you may be doing all this so wrong that the problem is elsewhere, or maybe you should not call this method what you want, but something else. It may be that you should not call this method. Architecture may have this class for internal use and not for its consumption.

If I knew the problem as a whole maybe dese to help more, but this problem solves itself by lowering the artificial complexity of the application.

  • Thank you I joined the company now and this is old implementation, I did not want to commit any gaffe of type repeat code, however I tbm had thought of instantiating pass all the parameters he asks and then use only one method (of the dozens of methods that this class has)but that you talked about the architectures helped me a lot, thanks again.

Browser other questions tagged

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