How to follow and control the dependency rules in Clean-Architecture
By Maik Schmidt
We, here at claimsforce, are building our applications following the Clean-Architecture pattern advocated by our beloved Uncle Bob.
For everyone who never heard of this pattern, here is a very short introduction.
[…] Nothing in an inner circle can know anything at all about something in an outer circle. In particular, the name of something declared in an outer circle must not be mentioned by the code in the an inner circle. […]
In concrete
- The blue layer is allowed to access the green, red and yellow layer
- The green layer is allowed to access the red and yellow layer
- The red layer is allowed to access the yellow layer
- The yellow layer is not allowed to access any other layer
For further readings we highly recommend the following article: https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
Back to the topic
We are using a slightly different layer structure than it was described in Uncle Bob’s Clean Architecture article but the core idea behind is the same.
- Domain (yellow layer)
- Application (red layer)
- Infrastructure (blue layer)
With this in mind a typical application folder structure at claimsforce looks as the following:
Based on the Dependency-Rule defined in the Clean-Architecture we need to follow and control these dependency rules:
domain
is not allowed to depend on othersapplication
is allowed to depend ondomain
infrastructure
is allowed to depend ondomain
andapplication
Easy peasy… but…
…our team is growing and we realize that these dependency rules are hard to remember especially for newcomers who never experienced working inside a Clean-Architecture. And for this we need a solution.
Solution
Since we are already using tslint in all our projects and we are developers who are trusting machines more than humans we just created a custom tslint rule to help newcomers and prevent human mistakes ;-)
You can find the tslint rule here: tslint-import-control-rule. Together with our tslint rule configuration:
{
"rules": {
"import-control": [
true,
{
"rootDir": "src",
"overrides": [
{
"rootDir": "src/application",
"whitelist": [
"src/domain"
]
},
{
"rootDir": "src/infrastructure",
"whitelist": [
"src/application",
"src/domain"
]
}
]
}
]
},
"rulesDirectory": [
"node_modules/tslint-import-control-rule/src"
]
}
Conclusion
Experience is good. Automation and Guidance are better. We extended the tslint rules in all our projects which prevents dependency rules violations already before they hit our source-control. Latest in our CI/CD in case someone deactivated (accidentally) the git hooks, but this is a different topic.
About claimsforce
The insurance industry does not currently offer its customers a sufficient loss experience, as the loss value chain is highly fragmented and digital.
claimsforce is the first fully digital SaaS+ (Software as a Service PLUS) platform for claims services in the insurance industry. To provide insurers, insurance customers, involved experts and other stakeholders with the technology to create a unique claims experience.
If our work sounds exciting to you, join our team!