Instantiate a class that inherits an interface class [closed]

0

I have a class that inherits many classes interfaces, and I want to instantiate it to use only one method, how should I do it? Is it possible to do it 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);
    }
}
    
asked by anonymous 10.10.2017 / 15:38

1 answer

1

Class and interface are very distinct things, either it's class or it's interface. Classes and interfaces have in common what types, just this.

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's just like any case. In this case the problem is the constructor that requires objects to be instantiated to create the object. It's even necessary because of the implemented interfaces, but this is collateral.

After editing you notice an extra complex class and you may wonder if you need it all. You get the impression that you are getting something ready (it has the face of being DDD) and that you do not understand what you are doing. This is a problem. In complex things you need a great domain of programming, object orientation, architecture, etc. Do not use methodologies that do not dominate, does not work. Any methodology, good or bad, only works when the person dominates. As much as a methodology can be wonderful, and many are more marketing than reality, if you do not fully understand how to use it is worse than using a more basic thing. And this seems to be quite complex, so much so that it may not even be advantageous for 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 as such in isolation or should it be static or should it be part of another class. But it looks like 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 say. To consume something very simple you need a very complex code. I'm sorry, but this code is an atrocity. It requires so much to use it that it is better not to use and do anything else, which would kill the reason for having created it.

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

Without changing the entire architecture I do not have a better solution, instantiate the object with everything it requires and use the method you need next. And yes, you will have 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 somewhere else, or maybe you should not call this method you want, but something else. You might not call this method. The architecture can have this class for internal use and not for consumption.

If I knew the problem as a whole I might want to help more, but this problem is solved by lowering the artificial complexity of the application.

    
10.10.2017 / 16:33