I'm struggling in how to represent and reuse some "operations" that are commonly done by the Applications Services but aren't part of the Domain. A bit of context:

I implement CQRS for the Application Services and follow this structure (Example):

    /Client (FunctionalModule)

All the commands have its request, reponse and handler and I'm using a "mediator" https://github.com/jbogard/MediatR to comunicate from the distribute services layer to the application layer (send a command and the mediator searches the handlers associated).

Now I have many of this Command and Queries (UseCases) that shares some particular operations such some calculations that involves data retrieve. Our approach always be to "duplicate" this code in the uses cases (call repository, call domain services), but now we found this hard to mantain. Our current solution is the following:


This interfaces only have one method and I want to use it within the DI container, as components. Is this a correct approach? Is there any concept or solution regarding this problem?

EDIT: Some pseudocode to clarify the question:

public void MyCommand(MyCommandRequest req)
    // Some code

    var data1 = repository.GetMyValueBy(id);
    var data2 = repository.GetMaxValueByType(type.Id);
    var data3 = repository1.GetCorrectionByDevice(device.Id);

    var myCalcValue = myDomainCalculatorSevice.Calc(data1, data2, data3);

    // More code

The actual code is a bit complex in can change. That changes will be valid for all the UseCases/Commands that use this type of calculations. It needs some more operations to get the data needed.

And the solutions we are using looks like this:

public void MyCommand(MyCommandRequest req)
    // Some code

    // mySharedComponent comes from DI.
    var myCalcValue = mySharedComponent.Calc(req.SomeValue);

    // More code

The SharedComponents are only for internal Application use.

We have some less experienced programmers so we are trying to get a pragmatic and straigthforward solution not the most purist one. Also, are there a better name instead of SharedComponent?

Thanks in advance! Óscar

  • Is it possible that you are missing some Supportive or Generic subdomain, which can be used in a framework fashion to group together commonalities in the application services? It's a bit hard to understand it from your question, so I used some imagination. Of course, it may be commonalities are not even worth to group together. As always, the main idea is to find sweet spot, which minimizes complexity. – Roman Susi Jun 6 '18 at 17:26
  • I'll try to make a better example and upload it – Oscar Vicente Perez Jun 6 '18 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.