Since quite a while back I've been very curious on Clean Architecture. And since the recent meetup on the subject I've been even more so. I threw together some code to play around with it and gain some actual experience. Now I feel I need to jot down some thoughts on it before I forget.
First of my realizations, ID's should not be a part of your domain structures. Unless they're domain specific. For swedish persons for example, the personal identification number is actual part of the domain, so it could be there. The ID you get from mongodb, or whatever, should not. This immediately has an impact on your Repository structure. It should not have any getById
method. Once again, unless that fits the domain. You might be starting to see the relation to Domain Driven Design about here. Please not that the version on github might be breaking this rule.
Second, your Repository structures should reflect the needs of the application. For example, I shouldn't have a generic Repository, I shouldn't have a Repository for Todos, only Persons, because that's the aggregate level you want to work on.
Thirdly, I tried to avoid writing wrapper structs for my domain structs in order to avoid some duplication. I also thought myself quite clever with my SQLable mechanism. One could probably even derive this somehow. What you absolutely do not want, is any invasive declarations inside your domain structs. Which is kind of what happens in Java, or Go. Rust can alleviate this to a great extent by the fact that implementations of traits can be separate from the objects themselves. But it's not a complete solution as you might need access to some getters or some such things. Perhaps this is what pub(crate)
is for. I need to investigate this aspect some more.
One reason I'm pondering this right now is because at work we're kind of meddling with re-implementing some functionality with new interfaces and data types, because the old ones are so entrenched into the code. If we had applied Clean Architecture, the logic could probably be re-used more easily. I hope to get better at this in the future. If you know of any successful examples, please let me know!