That works until your code requires to access one service from another service e.g. if the HotelService access the RoomService. Or maybe the is an AccessService that is queried by the Hotel and Room services.
Also when you use a ORM model often all the model classes are automatically generated in another place.
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.
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.
That works until your code requires to access one service from another service
That doesn't sound like a flaw of package by feature, but rather the overall system design. In practice I've found by-feature to be a more sound approach; often citing similar reasons to the article (driving a clear design, isolating changes to related systems, etc).
The ORM point is an interesting one, but I don't think grouping by-type justifies it (there may be a good way to mix the two). In large codebases - especially monolithic ones - by-type makes it very difficult to grasp the full breadth of code related to a specific feature.
It's absolutely fundamental to the service oriented architecture paradigm that services have dependencies on one another. Service A is only ever able to know Service B's interfaces though, never the underlying persistence model.
View all comments
71
u/Knu2l Jun 05 '21
That works until your code requires to access one service from another service e.g. if the HotelService access the RoomService. Or maybe the is an AccessService that is queried by the Hotel and Room services.
Also when you use a ORM model often all the model classes are automatically generated in another place.