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

Services using QueryRepository in CQRS #10

Open macg20 opened 1 year ago

macg20 commented 1 year ago

Hi, My next question will be about CQRS. I would like create excel/xml/csv report contain all films(or selected films). This service will be based on query repository. Am I right? I would like generate report and next presentation this report over http( for example as rest). First step will create package "query" and "FilmQueryRepository" (like Articles in your presentation https://youtu.be/ma15iBQpmHU?t=1273) How can i implement excel/xml/csv service based on FilmQueryRepository? Should I implement this service in film package or create separate package "report" and call facade/QueryRepository from films? I want to have still package scope. You mentioned that facade should one entry into module. When i implement this service into query package then will be public repository and service(i am going use this service in endpoint). Could you recommended any idea how can i implement this right way and still have package scope?

corlaez commented 1 year ago

If you put the queries in a different folder and package you will have to expose functions publicly.

However, you can keep them package private while being in a different folder. Just declare the package of the module on your files, despite them being in a query subfolder. Your IDE may complain but you should be able to ignore it or configure it and compilation should work without issues (java knows nothing about folders, just declared packages)

jakubnabrdalik commented 1 year ago

Dear @macg20 amd @corlaez I'd do it in a completely different way. In essence: module should have a single responsibility for a processes, not data. For detailed examples see: https://youtu.be/1HJJhGHC2A4?t=878 So for this very case what I'd do is:

This way you have ENCAPSULATION per module, that is no code is being used except for the API and the DTO. That's the point of having modules.

Please also note that the example provided here (film module) is not the best. Attached I provide you with a better diagram, that names the modules so that the name indicates the process a module is responsible for: The drawing

So in essence: xslx reporting is not a good example for a CQRS, because creating an xlsx is a complex process making you problem domain bigger if you put it into existing module. You need to lower the cognitive load when working with modules. If all you wanted is to return a list of DTOs presenting those films based on some criteria, that's a fine example for CQRS. But event that has limitations: full text search should be done in a separate module, because it requires different technologies (Solr, Elasticsearch, etc.)