This repository was archived by the owner on Jan 12, 2023. It is now read-only.
separate read and write paths #14
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Here is the PR that I "threatened", proposing an approach that approximates Command-Query Responsibility Separation... E.g. making sure the read paths and write paths are separate and distinct from each other.
This pattern also executes a little bit of the Don't Repeat Yourself (DRY) principle as well, if you squint. Pretend that the read/fetch operation somehow gets more complicated, and you update the
FetchDogappropriately, but you forget to update how theCreateDogorUpdateDogoperations build their response, and then the entity that you get back fromFetchDogno longer matches what comes back fromCreateDog/UpdateDog... That could lead to some subtle bugs in the code. By forcing everything through theFetchDogpath, you've effectively DRY'd your code.Another way that this helps is that it is ready to adjust to high read volume and scale later on down the road. Wanna put REDIS or Memcached caching on the read path? Cool, it's already separated out.
(I generally have felt the same thing should apply at the API layer, i.e. POSTs/PUTs should not return the body of the entity itself, but at most a 301 to the canonical entity location... However Kara convinced me that for purposes of reducing client round-trips to the server, returning those entities might be useful depending on the use-case. I am definitely willing to leave that in the "it depends" bucket.)
I definitely think read/write separation at the data layer is a MUST. I also built in the separation at the service layer in the PR just to show how it might affect higher layers.