jakubnabrdalik / hentai

Example of Hexagonal architecture with high cohesion modularization, CQRS and fast BDD tests in Java
Apache License 2.0
212 stars 51 forks source link

How implement repository without Spring Data? #9

Open macg20 opened 1 year ago

macg20 commented 1 year ago

Hi.Could you explain how to implement FilmRepository without SpringDataJpa and InMemory? Should FilmRepository be public then? Should the implementation FilmRepository then be in the infrastructure package? How can I do this while keeping the current package structure and package scope? Could you give an example?

jakubnabrdalik commented 1 year ago

Assuming you want to write a wrapper over some primitive tech (like JdbcTemplate or whatever), I'd just implement it directly in the same file. Just like InMemoryRepository. I know, religious zealots will say right away that this is infrastructure, and therefore should be down in the infra package (thus requiring Repo interface to become public), but I'm agnostic and pragmatic - I give no damn about religion. Everyone understands that Repo is infra, but limiting visibility by package scope is more important (and easy to get wrong). In fact I have implemented my own version of production ready repos exactly this way (for example to handle thread pools in non-reactive driver with subsribeOn). Worked like a charm, nobody made any mistakes with that for the next 3 years in 2 companies (so no problems for 7 years at least). But then again, I'm agnostic and pragmatic. I wouldn't be able to do that in a highly religious environment.

macg20 commented 1 year ago

Thank you for your answer!

corlaez commented 1 year ago

Tangential to the topic but... I have made a compromise in some cases where repositories where already coded and tested in a different module. What I did in that case was to mock the repositories. It worked well for my use case of very loosely coupled microservices that just read events and trigger repo calls/create other events.