5

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 ?

Sam
  • 605
  • 9
  • 19

2 Answers2

8

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.

Vasyl Zvarydchuk
  • 3,789
  • 26
  • 37
6

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.

Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54
  • Thank you for replying, I also didn't like the idea of my Domain layer interacting with the file system, as I have this principle of "_My Domain Layer must be in a way that allows me to move it or share it without problems_" . So I decided to take the values and put them in my class instead of the file. – Sam Jun 12 '17 at 15:53