Sam
Sam

Reputation: 615

Where to put a helper class in a DDD architecture?

I already read the answers in this similar question, but I believe my needs are different.

I have a class A in my Domain layer, one of its methods getValue needs to read from a file to get a certain value, so I thought about creating a helper class to do the file reading and keep the getValue as clean and minimal as possible.

Since I'm using DDD for the first time, and based on the humble knowledge I have, I think the helper class can be put in the Domain Layer since it's where it is going to be used.

Does putting the helper class in the Domain Layer a wise choice? if No, Is there any better solution that is DDD compliant ?

Upvotes: 5

Views: 3876

Answers (2)

Vasyl Zvarydchuk
Vasyl Zvarydchuk

Reputation: 3839

Reading data from file should be in Infrastructure layer as well as working with database, external services, communication with low-level API and so on. That's technical code, not domain logic. It should be there.

In DDD you can use services to place some helper code if of course it is domain logic and is not factory. You can also use Repository object creating an interface inside Domain Layer and implementation in Infrastructure layer.

So, you can create IMyDataRepository interface in Domain layer and MyDataRepository class in Infrastructure layer resolving dependencies in Application layer.

Upvotes: 8

Constantin Galbenu
Constantin Galbenu

Reputation: 17673

I do not recommend touching the file system inside the Domain/Aggregates, as they should be pure. If however you are using CQRS+Event sourcing then you can do IO in the read side but be carefull about replaying as the application state must be rebuildable at any time.

If you are not using CQRS but some kind of layered architecture and you must do IO then you should invert the dependencies to the Infrastructure (where the IO is done). For this you can define the interface inside the Domain layer and the implementation in the Infrastructure layer.

Upvotes: 6

Related Questions