DDD Architecture - Where to Place Common Methods / Helpers

In accordance with this issue of stack overflow in the DDD architecture, the β€œhelper” classes can be in different layers depending on their purpose. For example, an assistant who formats something in a friendly way enters the user interface. The database assistant will go to the infrastructure.

But what about helpers that can be used by more than one layer? For example, the calculation of age. Age may be required at the level of the business logic model. It is used by several objects, so it should not be in a specific object. There are also places where age is only required for display in the user interface. Similarly, I have string functions that can be used by more than one layer. For example, my user methods Right and Left can be used for formatting in the user interface, but they can also be used in the model, for example, prefix-based conditional logic.

So where do these general methods go? My setup is this:

  • interface
  • Application
  • Model
  • Infrastructure

The model is basic and does not depend on the infrastructure, so general assistants cannot work in the infrastructure. I am considering two options:

1) You have another layer called Common or similar, which can be used by any layer. This would create a dependency between Model and Common.

2) Duplicate the auxiliary logic in any layer. For example, have an age assistant in the user interface and have an age assistant in the model. This will violate DRY, but will not require the domain to be dependent on a "common" level.

Which option is better? Is it good for the model layer to be dependent on the "general" level?

UPDATE:

For 2.5 years since this question was asked, I concluded:

  • Things like Right, Left, etc., which compensate for limitations in the structure, belong to the "Common" utilities / auxiliary component, which the Model / Domain level depends on.
  • The General Utility / Assistant component does not have to be very large. Having more experience, I found that many of the things that I thought of as assistants really apply to the domain model.
  • Age belongs to its own class in the domain layer. Like things like address, phone number and money, I see these things as objects of value. Understanding value objects was really key to my understanding of creating reusable domain classes that could be included in other classes.
+6
source share
2 answers

The line helpers are a bit different, I think - in this case your custom Right and Left methods actually compensate for the restriction in the built-in string type of your language / platform - this has nothing to do with your application as such, in this case it’s great to make it accessible Worldwide.

The age calculation example is a bit more interesting as it encapsulates behavior / logic, which is a very important part of your application / domain. In this case, it may be advisable to evaluate in each case whether it is worth duplicating the behavior at each level (violating DRY in favor of SRP). This is a tough decision. Although now they can be identical, duplicating the behavior that you give the two methods, the opportunity to diverge from each other in the future. This usually happens if they have different reasons for the change .

+5
source

From my experience, having a CommonInfrastructure (or some such) component / level that contains all the helper classes / methods is the right way. both line helpers and age helpers, as you described, can get there (along with DateTime helpers, a dictionary, dynamics, Linq, whatever).

it should be a library that can be easily transferred to the next project, since it will be so general and without any dependence on the current domain logic and contains useful code for all purposes.

the only cause of DRY violation will be if you need Javascript code for the UI and C # code on your server, in which case you will need to duplicate this common infrastructure code (unless you come up with a more complicated solution to keep it DRY).

+5
source

Source: https://habr.com/ru/post/969895/


All Articles