0

I'm building an application based on DDD and Hexagonal Architecture. Hexagonal architecture suggests that you shouldn't, let's say, pollute your domain with logic of elsewhere, your Domain layer should be "pure" and only containing your business logic.

But what if I have a valueobject where I want to generate an ID from an external library? UUID for example.

This is not contaminating my domain, but still, I'm importing a third party in my Domain layer and it's supposed to be placed in the Infrastructure layer.

However, do this every time I want to do this kind of things for time formats, converters... it can become kinda tedious.

Are there exceptions on third party imports in the Domain layer? If so, on what depends?

What you guys think?

Many thanks

1 Answers1

1

DDD states that what is business matter goes in the domain layer, what is not does not. If the ID generation is a business logic, then there is no problem in importing that library in your domain layer. It is actually part of your domain layer, although you are not maintaining that part yourself.

When we say the domain layer should remain "pure", it means that your business logic should not be contaminated with infrastructure or presentation concerns. If the ID generation is only for persistence concerns (surrogate key), then this is an infrastructure concern, and this generation should either be done by the persistence store, or the infrastructure adapter.

ArwynFr
  • 1,383
  • 7
  • 13
  • Always thought that to connect with third parties was a infrastructure responsability, but in DDD makes sense what you are saying, thanks! – Roger González Hermosa Apr 01 '22 at 21:49
  • Infrastructure connects to *external* third parties (think UML actors), like a database, another program, or a control-command device. A library is embedded as a part of your application ; you are delegating implementation, not the use case. – ArwynFr Apr 01 '22 at 22:53