Skip to content

README

Loading...

Live system. This version passed every check before it shipped.

Blog

Steven StoutBlogNov 17, 2026 · 9 min read

Onion architecture for beginners: what does the middle know?

My auction's bidding rules sit in a project with one reference, and it isn't a database.

Cover: Onion architecture for beginners, what does the middle know?

01The full post

Steven Stout

Staff .NET Engineer | AI-Assisted Development with Quality Gates | 12 Years Full Stack .NET, 7 Fully Remote

· first posted on LinkedIn · 9 min read

My auction's bidding rules sit in a project with one reference, and it isn't a database.

The site is TheYard, a used-car auction I build in C# and .NET as my portfolio. Claude writes most of its code, and I decide what goes live.

Onion architecture is a way to arrange code in rings. The business rules sit in the middle. The code that carries out what a visitor asks for wraps them. The database code and the web endpoints sit on the outside. One rule holds it together: code may use the rings inside it and never the ones outside it. Jeffrey Palermo named the pattern on July 29, 2008, with that rule at its center, and Robert C. Martin's The Clean Architecture, August 13, 2012, calls it the Dependency Rule and calls the database and the web details.

A few words this post uses:

  • Ring: one layer of the code. In TheYard each inner ring is its own .NET project.
  • Domain: the business rules, here the auction's: who may bid, how much and when.
  • Port: an interface the inner code declares for something it needs from outside, such as a place to keep bids.
  • Adapter: a class in an outer ring that implements a port against one real database.

Slide 2 in the slides below draws TheYard's onion: Data in the middle, then Domain, then Application, with the web endpoints and the database adapters in the outside ring, every arrow pointing inward, and Azure SQL and Cosmos DB outside the onion altogether. The full drawing, with every project named, is on the site's rings diagram page.

Slide 3 defines each ring, from the middle out:

  • Data (textbook: the domain model's records): plain records with no behavior and no dependencies, which every ring may read. In TheYard: Vehicle and PhotoEntry.
  • Domain (textbook: the domain model's rules): the business rules as pure functions. The same inputs give the same answer, and the clock is handed in. In TheYard: BidRules and AuctionSchedule.
  • Application (textbook: use cases): the use cases, which run the rules in the right order, and the ports, which are interfaces for what the middle needs from outside. In TheYard: PlaceBidHandler and IBidStore.
  • Outside ring (textbook: infrastructure and UI): the web endpoints read a request and ask Application, and the adapters implement the ports against one real database each. In TheYard: EfBidStore and CosmosBidStore.

The databases themselves, Azure SQL and Cosmos DB, sit outside the onion. Palermo's 2008 post says it directly: the database is not the center, it is external.

In October I walked through TheYard's rings one at a time. This post is about the point under all of them: the rules in the middle know nothing about the database or the web. Here are five lessons on keeping it that way, written so you can use them in your own project tomorrow. Every count was read from the code at release 1.0.5.55.

1. Point every dependency inward

The auction's rules are the part of TheYard that must not break. A bid that goes through when it should be refused costs a real buyer money, and I didn't want those rules tied to a database driver or a web framework.

So the rules live in their own project, Domain, in the middle. Its project file has one reference, to the Data project that holds the plain vehicle shapes, and no NuGet package at all:

<ProjectReference Include="..\TheYard.Data\TheYard.Data.csproj" />

That is the whole trick, and the compiler enforces it for free. Code in Domain that reaches for Entity Framework, the Cosmos DB SDK or ASP.NET doesn't compile, because the project can't see them. Domain holds 24 C# files today, and the rule for a bid is one static method that takes a vehicle, an amount, a clock and whether the car is sold, and answers with an outcome.

2. Hand the rules the clock

Every auction on TheYard opens and closes at set times. A rule that reads DateTime.UtcNow for itself gives a different answer every time it runs, so a test of it can only ask what happens "now".

I needed tests that could name an exact moment: one millisecond before an auction opens, the moment it opens, the last millisecond before it closes and the moment it closes.

So the rules never read the clock. `BidRules` and `AuctionSchedule` take the time as an argument, and the web layer reads the server's clock and hands it in. The build backs it up: one of the architecture tests reads every method in Domain for a call to the system clock and fails on the first one it finds.

The result is a test that checks those four exact milliseconds with no database, no web server and no waiting. With a rule that read the clock for itself, that test could not be written.

3. Put the database behind a port

TheYard runs on two very different databases: Azure SQL, through Entity Framework Core, and Cosmos DB, through its own SDK. The same rules have to save a bid to either one without knowing which.

The answer is dependency inversion, the D in SOLID. The inner ring declares the interface it needs, in its own words, and the outer ring implements it. Application declares `IBidStore`. `EfBidStore` implements it for Azure SQL, `CosmosBidStore` for Cosmos DB, and MemoryBidStore for the tests. The one place that names a real adapter is the wiring at startup. Name the port for what the middle needs, IBidStore, and keep the technology out of the name. ISqlBidRepository would leak it.

To be plain about what this did and did not do: when Cosmos DB arrived, the port itself had to change once. Every method became a Task, because the Cosmos DB SDK has no way to answer without waiting on the network. That touched every caller one time. After it, the Cosmos DB adapters arrived as new classes and no bid rule changed. Microsoft's .NET architecture guide, updated March 7, 2023, describes the same move and the same payoff: because the core doesn't depend on infrastructure, it is easy to unit test.

4. Let a test hold the rings, then test it

A rule that only code review holds slips the first busy week, and I didn't want to be the person who has to remember it. So I gave the rule to the build.

`OnionTests` uses NetArchTest, a library that opens the compiled .NET code and checks which types use which. Reading compiled code matters, because it sees inside method bodies and lambdas, so a dependency hidden in a handler fails as surely as a using line would.

It started with ten rules in early October, and I watched each one fail before I trusted it. Four failed on their first run against my own code: the endpoint rules found handlers reaching into the service container and composing the bid services themselves, the Cosmos DB rule found that adapter borrowing a type from the SQL one, and the database rule found three queries written in the web project, one hidden in a lambda. I fixed each, then proved the other six by planting a forbidden reference and watching the build go red.

Then the tests had a gap of their own, and it was mine. On October 21 a review planted six violations, one per ring: a vehicle shape with a rule and a file read inside it, a Domain class starting a process, an adapter with a second buy-now rule, and more. Every architecture test passed with all six in. The rules allowed the whole System namespace in every ring, so they only caught what they named. In release 1.0.4.24 I tightened them until each planted file failed by name. The file holds 19 rules today.

I know I'm not alone here. Anyone who has trusted a green test that checked less than its name says hits this one. The fix is the same as for any test: make it fail on purpose before you believe it.

5. Slice the onion by feature

The rings held, but a feature like bidding was spread across folders named for kinds of code: the endpoint in one, the wiring in another, the handler in a third, and about 90 files loose at the root of the web project. To change bidding you opened four folders and guessed which files mattered.

On October 20, in release 1.0.4.0, every feature became one folder with one name, used in every ring that holds its code. Features/Bids in the web project holds the endpoint and a Module that wires it; Features/Bids in Application holds the handlers. The rules and the database code that bidding shares with the vehicle pages sit in a Shared folder in each ring, and a test fails the build when one feature reaches into another. The web project has ten feature folders and 13 modules today, and Program.cs finds the modules itself, so it stays at 56 lines and a new feature adds no line to it.

Jimmy Bogard's Vertical Slice Architecture, April 19, 2018, is where the idea comes from: couple along a slice, not across a layer. Bogard builds slices instead of layers. TheYard keeps the rings and cuts the slices through them, because the rings are what let the database change without touching the rules. The full decision is ADR-107: The onion cut into vertical slices.

When not to build it this way

The rings cost projects, interfaces and tests, and they pay that back only when there is something to protect. TheYard has two databases behind the same rules and rules worth testing on their own, so the cost is worth it here.

A small service over one table is clearer as one project, with Entity Framework called from the endpoint. A spike written to learn something fast is one file. A script that reads, changes and writes needs no layers at all. Add a ring the day a second database or a rule worth testing alone shows up. The middle road is folders: The Shed, a file browser that lives beside TheYard, is one project with its rings as folders, held by the same kind of tests by namespace.

More from the same rings

  • CQRS sits inside them. My post CQRS for beginners splits every request into a query or a command, and its handlers sit in the same feature folders.
  • The full record. ADR-088: Onion and SOLID lists every ring, what is kept on purpose and every addendum since October 3.
  • The practices page. Onion architecture, the practices follows one bid through the rings with the live code, and shows how to make a rule go red on your own repository in five steps.

A simple way to remember:

  • Dependencies → point inward
  • Clock → handed in
  • Database → behind a port
  • Rings → held by a test
  • Feature → one folder per ring

What would have to change before you split a small service into rings?

References

The slides (10)

5 reads on TheYard0 this week

Steven Stout

Steven Stout

Staff .NET engineer · Remote

I have spent twelve years building full stack software in C#, SQL Server, React and Azure.

About StevenCheck my work

02What has to work, where is it today, and what does success look like?

Book 20 minutes opens an email to me with those three questions in it, and I answer with a time.

Book 20 minutes

Your email app should have opened with this message. If it did not, copy it and send it from any mailbox.

Or message me on LinkedIn