1

i'm migrating a nTier architecture into an hexagonal one. My domain is well defined now, and I have interfaces for all infrastructure dependencies. Looking at database repositories, I have several databases and there is one class implementing each repo, in the Infra side. My question is about what should be the correct approach about DB dependency:

1- To have one interface on domain side to deal with data access and then rely on Infrastructure implementation and have there one class as entry point for all database repositories, like a facade, from where all repository implementations will be called. This approach is the same I already have in nTier application, the facade and the repos are the current Database Layers.

2- Have interfaces for each database I need on Domain side. Each interface will be implemented on Infra side to access the corresponding DB. It keeps layers thin, but this aproach add Data management logic to the Domain. The Domain should not care where the data is, the Infra should deal with this. If some data is moved to another database then the corresponding interface needs to changed on Domain side (to move the method exposing this data to another interface for example)

let me know,

user810917
  • 221
  • 4
  • 11

1 Answers1

0

That's a good question. I had the same dilemma in one application, and I decided going for the first option you expose:

Just one port for access data, the domain doesn't care about the origin of the data, no matter which database the data is stored. And in the adapter I have a facade to forward the request to the correct database. In fact in my case the resulting data maybe a mix from the two databases.

But I think the other option is valid too, if we consider the databases as external systems our app have to talk to. Our app must know which secondary actors must communicate with, and in that case, each one would have a port at the domain.

Anyway, I think 1st option is more "correct", because a port in hexagonal architecture is the purppose of the communication, being the purppose in this case "retrieving data". In this talk Alistair Cockburn explains it. He says the ports must be named by their intention, saying what they are for. A port is for ...ing something.

Talk "Alistair in the Hexagon":

https://www.youtube.com/watch?v=th4AgBcrEHA

https://www.youtube.com/watch?v=iALcE8BPs94

https://www.youtube.com/watch?v=DAe0Bmcyt-4

choquero70
  • 3,332
  • 2
  • 24
  • 40