Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Clean Architecture for Backend Services: When to Use It and How

Many engineering teams fall into one of two architectural traps: they prematurely over-engineer simple microservices with complex abstraction layers, or they neglect architecture entirely until their core backend becomes an untestable monolith where business rules are entangled with database queries. When you mix your framework, database, and business logic into a single unlayered codebase,…

In the realm of backend service development, engineering teams often encounter two common architectural pitfalls: premature over-engineering of straightforward microservices or neglecting architecture until the core backend becomes an unmanageable monolith. This article explores when to utilize Clean Architecture and how to implement it effectively without introducing unnecessary complexity.

Before incorporating multiple layers of abstractions, engineering teams should assess their domain against three critical questions: Does the domain contain intricate business rules that frequently evolve? Will multiple teams interact with and extend this service over an extended period? Is unit testability without external dependencies (such as databases or external APIs) mandatory?

If the answer to these questions is affirmative, then implementing Clean Architecture proves beneficial. By isolating the core domain at the center, the service remains unaffected by database migrations, framework updates, or changes in external vendors. Conversely, projects involving simple CRUD applications, temporary prototypes, or narrowly scoped microservices may benefit from adopting a simpler approach, such as a single-file or basic module layout, for faster deployment and easier management.

The implementation process begins with defining core entities and interfaces, which should have no dependencies on web frameworks, ORMs, or database drivers. For instance, in Python, a domain entity and an abstract repository interface can be defined using Protocol and dataclasses. This ensures that business rules, such as validating payment amounts, are enforced within the domain entity independently of the storage mechanism.

The next step involves implementing pure application use cases, which orchestrate the domain entities and abstract interfaces to execute specific business operations. These use cases can be tested using mock implementations of the repository interface, enabling testing without reliance on a live database. Finally, the outermost layer implements infrastructure adapters, including concrete components such as database repositories, external API clients, and HTTP controllers.

This layer adheres to the principles of dependency inversion, allowing for easy swapping of infrastructure components without impacting the core domain or application logic. By following this structured approach, engineering teams can effectively leverage Clean Architecture to create scalable, maintainable, and easily testable backend services.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Building a reading speed test that can't be gamed by skimming

Our book review site needed a feature readers would actually use, and that other sites would want to link to. We settled on a reading speed test, then found that most existing tests share a flaw: skim…

  • New reading speed test developed for book review site
  • Prevents skimming by labeling scores above 900 wpm as "Too fast to measure"
  • Uses four stages: intro, reading, quiz, and result with performance.now() timing

how my 1.17 mb player renders webvtt subtitles on a canvas (no amoled burn-in, no dom churn)

i'm the dev behind RKMovies , a 1.17 mb native 4k player for android, with a 29.5 mb windows build on the side. one constraint i gave myself early: subtitles must not burn in amoled screens, and total…

  • RKMovies player renders webVTT subtitles using canvas overlay
  • Avoids AMOLED burn-in and DOM churn with canvas approach
  • Parses and renders subtitles in memory, keeping APK under 1.17 MB

Sereni Locust(does it exist?)

You pasted the dog Bash script from earlier in the thread (with a capitalized Mkdir on the first line and leftover [span_...] markers in the comments/strings).

  • "Sereni locust" is a fictional term
  • "dog" is a software tool
  • "πEve" is the platform it works with

More from Monday 23 November →