惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
美团技术团队
博客园_首页
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
B
Blog
D
DataBreaches.Net
腾讯CDC
C
Check Point Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
月光博客
月光博客
V
V2EX
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
The Cloudflare Blog
博客园 - 叶小钗
Y
Y Combinator Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Building software in C#: part 2 - code architecture
Miroslav Thompson · 2026-05-30 · via DEV Community

In my previous post, I described how historical trends shaped our (or at least my) perception of software architecture.

However, with experience and seniority, you learn that code architecture always depends on a variety of context-dependent factors. This is something AI cannot help you with. A human must always establish the direction, depending on human factors such as company culture, team dynamics, formalization of work, responsibilities, and the software life cycle.

The best code architecture I have encountered so far doesn't have a name yet. It is a mix of various concepts taken from different architectural patterns.

Domain as a Flat List of Functions

When you use the mediator pattern, you are basically using a service locator. Writing CreateUserCommand and CreateUserCommandHandler is fundamentally the same thing as writing ICreateUserService and/or CreateUserService with a single CreateUser method.

Ultimately, the specific approach doesn't matter. The important thing is to always write your domain as a flat list of functions, each having a single responsibility and handling a single process. Time and again, this has proven to be the best strategy to guarantee long-term stability and, all else being equal, the avoidance of technical debt.

In this article, I will use the term function, but what I mean is a mediator command/handler or a service with a single public method; the specific shape is secondary.

CQRS, But Read is Part of the Domain

Originally, CQRS was meant to separate reads and writes at the infrastructure level because, for most business applications, 90% of operations are reads and 10% are writes. Domain writes are focused on one system, and changes eventually become visible eventually in a separate system optimized for reading.

However, I have never worked in an environment where traffic was high enough to justify such infrastructure-level optimizations. One reason is that the Czech Republic is a relatively small country with a population of around 10.5 million, and not everyone works at a high-traffic site like seznam.cz.

From a code architecture perspective, accounting for a separate read-only database is difficult. You need to produce and capture events, and you must maintain a manual or automated synchronization mechanism in case event delivery fails. Chances are, you don't need to do this at all—I certainly haven't.

BUT: Separating your domain functions into writing and reading functions is still an excellent idea.

Why?

If your users interact with a UI that displays data, then reading data is part of your domain. The DDD purists who believe the domain is strictly for writing are mistaken. The domain is supposed to represent what the business needs. If the business needs to read and display data, a function that reads and returns that data belongs in the domain.

Higher-Order Functions

Once you start writing your domain as a list of read or write functions, if the domain is complex enough, you will inevitably need a higher-order function: a function that calls (reuses) other functions in your domain. This is a saga pattern of sorts. Your first higher-order function will be a second-level function reusing your foundational, flat list of functions.

The critical architectural constraint here is that any higher-order function must strictly read from or write to the domain using the functions directly below them. A second-level function can use the first-level (bottom) list of functions. A third-level function can only use second-level functions, and so on.

Another constraint is that functions on the same level must NEVER invoke each other.

I have never needed anything higher than a second-level function, even in highly complex financial domains. However, in my first job, I worked on a monolithic application for travel agencies, which was probably the most complex domain I have ever seen. If that domain were written using this architecture, it would definitely require third- or even fourth-level functions.

Monolithic Domain

The domain is a monolith that should not be modularized within the scope of a single application or API. You should never spread domain functions across different .csproj files. The only justification for moving functions elsewhere is a deliberate decision to extract a new application that takes over specific responsibilities from the original one.

Validation, Transformation, Dependency

These are the only three actions allowed inside your domain functions, executed in any order:

  • Validation: Verify whether, at any point inside the function, your in-memory data is valid.
  • Transformation: Transform or manipulate data in memory.
  • Dependency: Invoke a request to an external service (including your repository) to pass data, request data, or both.

Direct Use of EF Core

Most applications I have written in my career use MSSQL as their primary data source, or at least some relational database.

Time for a hard-to-swallow pill: switching from one RDBMS to another never happens. Unless the database schema is cleanly architected from the beginning, moving from MSSQL to PostgreSQL or MySQL (or vice versa) requires an immense amount of work that is rarely justified.

For those reasons, I don't see a point in abstracting the DbContext. It already is the abstraction I need. The argument that you must abstract DbContext into a repository interface just for testing is outdated; the ability to mock or substitute it has been solved for years. If you think EF Core is inherently slow, you probably haven't heard of .AsNoTracking().

This is also my critique of the Unit of Work pattern in Clean Architecture—it is often completely useless. There is no point in abstracting DbSet<User>.Add into IUserRepository.Add only to invoke IUnitOfWork.Commit instead of DbContext.SaveChanges(). YAGNI!

Dependencies as Modules & Maturity Levels

The domain needs to handle operations other than just reading and writing to the database. You invoke external APIs, write PDFs to file systems, or open sockets using legacy, third-party binary protocols.

Some operations are simple enough to write directly inside the function. However, complex operations should be abstracted away into an interface (like IInvoicePdfWriter) with the actual implementation moved elsewhere.

Where exactly? It depends on how much architectural overhead you are willing to accept:

  • Maturity Level 5: InvoicePdfWriter is packaged inside a NuGet package, making it completely reusable across other projects.
  • Maturity Level 4B: InvoicePdfWriter resides in a dedicated .csproj file created solely for this specific functionality.
  • Maturity Level 4A: InvoicePdfWriter resides in a shared .csproj for all external dependencies, organized in a dedicated folder. A custom analyzer is configured to allow or forbid dependencies the same way you would use project references.
  • Maturity Level 3: InvoicePdfWriter is in the same .csproj as the domain functions, but the functions inject the IInvoicePdfWriter interface.
  • Maturity Level 2: You skip the interface entirely and inject the concrete InvoicePdfWriter class directly.
  • Maturity level 1: The dependency speficic code is written directly inside of your function. You should at least move all of that to an internal service (maturity level 2).

The difference between 4A and 4B is practical: with 4B, you can easily end up with hundreds of .csproj files even for a relatively small project. This slows down your Visual Studio instance considerably and is rarely worth the overhead.