hentai icon indicating copy to clipboard operation
hentai copied to clipboard

Services using QueryRepository in CQRS

Open macg20 opened this issue 1 year ago • 2 comments

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?

macg20 avatar Jul 13 '22 15:07 macg20

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)

corlaez avatar Aug 08 '22 22:08 corlaez

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:

  • create a separate module called filmreporter responsible for exporting to csv/xlsx/xml
  • either send events from that module from wherever we create film (most likely another module) and build a read model (repository) with that data (this means data duplication, but in the video linked above I explain why this is not a problem)
  • or query the current film module to ask for film details

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.)

jakubnabrdalik avatar Aug 12 '22 09:08 jakubnabrdalik