I have a workspace with many microservices and many times I want to perform conversions between the DTOs of different services (e.g. convert the response of one service into a request for another). I’m having a hard time deciding where to put these conversions due to cargo rejecting cyclic dependencies. Some solutions I’ve thought of:
- Decide on a convention such that conversions from crate
A
to crateB
(A
->B
) belong in crateA
(always the ‘from’ side). This means crateA
depends on crateB
but this precludes crateB
from depending onA
if it ever callsA
directly (which actually happens in our codebase) - Create a separate
convert
crate where we store conversions, gated by both features for both involved crates. This meansconvert
depends on bothA
andB
so neither can use these conversions directly or otherwise that would be a cylic dependency!
I’ll take any ideas to solve this cleanly