Course · Training · Workshop

Clean Code with C#

Two days hands-on in C# and .NET: sharpen names and methods, apply SOLID to an existing application, develop business rules test-first and build APIs with validation and integration tests.

In many .NET projects the business logic sits in service classes that grow with every feature: one method validates, calculates, saves and sends emails, there are few tests, and every change takes courage. This course shows C# teams how to improve such code in small, secured steps and how to build new code cleanly from the start. Participants begin with names, comments and the basic principles DRY, KISS, Single Level of Abstraction and the Integration Operation Segregation Principle. They then apply the five SOLID principles in exercises and to the services of an existing application for medical clinics. In the second part they rebuild modules of this application as a modular monolith: domain model, business rules as classes of their own with unit tests in xUnit, endpoints with FluentValidation and integration tests against PostgreSQL with Testcontainers. Analysers, nullable reference types and an .editorconfig lock the agreed rules into the build. The course is part of the letsboot clean code family with one shared quality bar: code the team can read, change safely and test. For teams with several languages there is Clean Code Hands-On, for Java Clean Code with Java, for the frontend Clean Code with TypeScript. At the end participants have a refactored application and two tested modules they can use as a template for their own projects.

What participants say

These customers booked courses in the same topic cluster.More customers →

Content

The course is for C# teams that maintain and evolve .NET applications. A sample application for medical clinics runs through both days: it is refactored first and then rebuilt in modules.

The course consists of the following topics and can be adapted to the audience.

Clean code in everyday .NET work

  • Why code is read more often than written, and what that costs
  • Clean code in the age of AI: what becomes more important with generated code
  • Pragmatism: how much quality makes sense where
  • Settling style questions such as var with .editorconfig
  • Hands-On: collect and classify good and problematic spots in your own code

Effective naming and comments

  • Names for variables, methods and classes that carry the intent
  • Comments for intent, warnings and public contracts (XML docs)
  • Spotting outdated and redundant comments
  • Hands-On: improve badly named C# code with the rename refactorings of the IDE

Basic principles

  • DRY and the price of the wrong abstraction
  • KISS, YAGNI and composition over inheritance
  • Integration Operation Segregation Principle (IOSP)
  • Single Level of Abstraction and separation of concerns
  • Hands-On: merge duplicated discount logic and split an OrderService following IOSP

SOLID in C#

  • Single responsibility, using a UserService as the example
  • Open-closed with Strategy, using a DiscountCalculator as the example
  • Liskov substitution in inheritance hierarchies
  • Interface segregation and dependency inversion with the DI container
  • Hands-On: fix one violation of each of the five principles

Refactoring existing code

  • Recognising code smells in grown service classes
  • Unit tests as a safety net before the change
  • Small steps with the automated refactorings of Rider and Visual Studio
  • Boy Scout Rule and root cause analysis
  • Hands-On: refactor the billing, invoice and notification services of the clinic application

Unit testing and test-driven development

  • Structure of a test: arrange, act, assert and telling test names
  • TDD in micro-iterations: test, minimal implementation, refactoring
  • xUnit with Shouldly, test data with Bogus, controlling time with TimeProvider
  • Telling stubs and mocks apart
  • Hands-On: develop business rules such as «adults only» and «no registration on weekends» test-first

Domain model and business rules

  • Rich and anaemic domain model
  • Business rules as classes of their own
  • Tell, Don’t Ask and the Law of Demeter
  • Hands-On: build the domain model for registration and appointment scheduling with the methods Register, Discharge, Schedule and Cancel

Clean APIs

  • One endpoint per use case with minimal APIs
  • Request validation with FluentValidation
  • Consistent error responses with a global exception handler
  • Integration tests against PostgreSQL with Testcontainers
  • Hands-On: implement endpoints with validation, unit tests and integration tests

Modularisation

  • Modular monolith: the modules Patient Registration and Appointment Scheduling
  • Public interfaces of a module through a contracts project
  • From folders to a project of its own per module
  • Placing clean architecture and vertical slices
  • Hands-On: separate two modules and connect them through a public interface

Securing quality in the build

  • .NET analysers, warnings as errors and nullable reference types
  • .editorconfig and Directory.Build.props for the whole solution
  • Central package management with Directory.Packages.props
  • Placing static analysis and continuous integration
  • Hands-On: switch on build rules for a solution and fix the violations

At the end participants have a refactored application and two tested modules with domain model, validation and integration tests that serve as a template for their own projects. Does your team work in several languages? Then Clean Code Hands-On is the fit. For TypeScript in the frontend there is Clean Code with TypeScript, for Java Clean Code with Java.

The actual course content may differ from the above depending on the trainer, delivery, duration and the composition of participants.

Request this course in-house

By submitting you accept our Privacy Policy.

Request a public date

No suitable public date? Register without obligation — once there is enough interest we schedule a new public date and let you know first.

Number of participants (approx.)

More than 3 participants? Best to request a dedicated in-house date directly.

By submitting you accept our Privacy Policy.

More about Clean Code with C#

Clean code in C# combines the principles from the book by Robert C. Martin (2008, second edition 2025) with the means of the language and the platform. Records, pattern matching and nullable reference types support immutable data and expressive code, and the .NET analysers check conventions in the build. In German-speaking countries the Clean Code Developer initiative with its grades, principles and practices is also widely used.

Further resources:

Related courses