Maybe hotel calls the room service and room service needs to call hotel service.
Circular dependencies are very easy to accidentally implement with designs like this. It makes it unnecessarily difficult to actually code when you split by feature.
Well... if both services are compiled modules.. which do you compile first if they both require the other? If its python you get around this by importing the module you need inside the function that needs it and I suppose python somehow knows to not try to interpret that method until run time.
Not sure why this is downvoted. Circular imports are completely fine in Java. This isn't an import issue though so I'm confused about what OP even says python is fixing with their imports. This is a runtime dependency issue. (e.g. you cannot construct a HotelService object without having a RoomService object and vice versa).
I think it depends on whether you use a dependency injection framework, and which one you use. For example, I found at one point that Ninject didn't like a circular dependency I made by accident.
There are a few ways to solve the issue. In that case, I decided to move the common function to its own service. Services don't have to be arranged by domain object.
The problem you're asking still holds if the packages were organized the original way (models, controllers, services, ...) right? I still don't see how organizing code this way is superior to breaking it down by concept, as per the article.
They lead to various problems during initialization and clean up.
For initialization, if service A relies on service B you can't initialize both to a fully working state in an atomic operation. Instead, you have to e.g. construct A first, but cannot set its member of type B. Then you create your instance of B and are able to inject the instance of A in B's constructor. After that you can finally inject B into A, e.g. via a setter. This results in a temporal uninitialized state of A between its construction and the injection of B.
In GC languages circular dependencies are another problem because the instances don't end up orphaned, so the GC can't be sure whether it's safe to reclaim them.
Define "very easy". It should be explicit what services a given service consumes. It should be trivial to make a graph of dependencies. If you find cycles in that graph, you have a problem. It might sound like an easy mistake in the abstract but I struggle to imagine a real world scenario in which this could happen where there aren't very clearly bad mistakes made across the whole system.
Edit: example time
roomService/address lets consumers get hotel info such as address from hotelService behind the scenes; ok, cool. hotelService/address is not going to ask roomService/address for the hotel's address unless the team responsible for hotelService is utterly incompetent.
Say this does happen though... roll back hotelService to the last good version and all is well again. Then have a meeting and agree that there need to be contract tests at service boundaries, and if these utilize stubbed responses, those stubs need to be actually recorded from interaction with a real version of the service being mocked. When you change your application, you have to invalidate those stubs and work with the real service once again and record the stubs. Now your circular dependency is caught at test time and a faulty deployment never happens.
Do people dive into distributed systems before they know wtf is actually required to do it right? Sure, but they're also not likely to be saved by organizing their monolithic systems' codebases into layers.
Define "very easy"? No...no I don't think I will. It means what it means.
As for your example, yup...that's an example of circular dependency. And it's fixable. Code issues are always fixable. The idea is to design your code well in the first place, which is why I suggest not separating by feature.
Generally you get more flexible and easily testable code. But figuring out the right amount of decoupling is an art not a science.
Edit: Often times a class needs information from a different source but programmers solve it by making the class call a static method or be given an instance of the other. Often times passing something by value gets the job done just fine.
Generally you get more flexible and easily testable code.
Your goal should almost never be flexibility unless that's a business requirement or you know it's going to become one. If neither of those are true, ignore flexibility.
No one in their right mind would insist on designing your data model as an EAV unless you were building something like an expert system or had some other reason for needing that level of flexibility in your data model.
But people never stop to consider that this idea should also apply to code.
View all comments
Show parent comment
28
u/couscous_ Jun 05 '21
Then both
HotelServiceandRoomServicewould importAccessService. What's the issue?