Skip to main content

Posts

Showing posts with the label Programming

Clean Architecture in C#: Build Maintainable .NET Apps

Learn clean architecture in C# with a practical ASP.NET Core example. Layers, dependency rule, code samples, and best practices. Start building better apps today. If you have ever opened a five-year-old .NET solution and found Entity Framework queries inside a controller, business rules buried in a Razor view, and a "Utilities" project that every other project references, you already understand why clean architecture in C# matters. Clean architecture is a way of organizing a .NET solution so that your business rules sit at the center, completely independent of frameworks, databases, and user interfaces. The result is an application that is easier to test, easier to change, and far less likely to collapse under its own weight as it grows. This guide walks through the principles behind clean architecture, shows a complete project structure for an ASP.NET Core application, and provides runnable C# code for each layer. Along the way we cover the common mistakes teams make ...

Event-Driven Architecture in C#: Loosely Coupled Systems

Learn event-driven architecture in C# with runnable examples: domain events, MediatR, message brokers, and best practices. Build loosely coupled .NET systems today. Event-driven architecture in C# is one of the most effective ways to build loosely coupled systems that scale, evolve, and stay maintainable as your codebase grows. Instead of components calling each other directly, they announce that something happened, and any interested component reacts. In this tutorial you will learn what event-driven architecture is, why it matters for .NET developers, and how to implement it step by step: from native C# events, to in-process domain events with MediatR, to cross-service messaging with a message broker like RabbitMQ or Azure Service Bus. Every example is runnable on .NET 8 or later. What Is Event-Driven Architecture in C#? Event-driven architecture (EDA) is a design style where the flow of the program is determined by events : immutable facts that something happened in the past. ...

TDD in C# with xUnit and Moq: Complete Tutorial

Learn test-driven development in C# with xUnit and Moq. Step-by-step TDD tutorial with runnable code, mocking, best practices. Start writing tests first! What Is Test-Driven Development in C#? Test-driven development (TDD) in C# flips the traditional workflow on its head: you write a failing unit test first , then write just enough production code to make it pass, and finally refactor with confidence. Combined with xUnit (the most popular .NET testing framework) and Moq (the go-to mocking library), test-driven development in C# gives you a fast feedback loop, a living specification of your code's behavior, and a safety net that makes refactoring painless instead of terrifying. In this tutorial you'll build a small but realistic feature — an order service with a payment gateway dependency — entirely test-first. Along the way we'll cover the red-green-refactor cycle, mocking with Moq, best practices, and the pitfalls that trip up most teams when they adopt TDD. Why ...

.NET Performance Profiling: dotTrace & PerfView Guide

Learn .NET performance profiling with dotTrace and PerfView. Find CPU, memory, and async bottlenecks in C# apps — step-by-step tutorial with code. Why .NET Performance Profiling Beats Guessing Every Time Every developer has been there: the app is slow, someone on the team says "it's probably the database," someone else blames the ORM, and an afternoon disappears into changing code that was never the problem. .NET performance profiling replaces that guesswork with measurement. Instead of asking "what do we think is slow?", a profiler answers "here is exactly where your CPU time, memory allocations, and wall-clock time went." In this guide, you'll learn how to find and fix real bottlenecks in C# applications using two of the best tools in the ecosystem: JetBrains dotTrace and Microsoft's free PerfView. The reason profiling matters so much is a hard statistical truth: performance problems are almost never evenly distributed. In most applica...

Infrastructure as Code with C#: Pulumi .NET Tutorial

Learn Infrastructure as Code with C# using Pulumi. Step-by-step .NET tutorial with runnable examples, best practices, and pitfalls. Start deploying today. Infrastructure as Code (IaC) lets you define cloud resources—virtual machines, storage, databases, Kubernetes clusters—in files you can version, review, and test like any other software. For years, .NET teams had to leave C# behind to do this, writing Terraform HCL or ARM/Bicep JSON. Pulumi changes that: it lets you write infrastructure as code in C# using the same language, IDE, NuGet packages, and testing tools you already use for your applications. In this tutorial you'll learn how Pulumi works, deploy real Azure and AWS resources from a .NET project, and pick up the best practices and pitfalls that matter in production. What Is Infrastructure as Code, and Why C#? Infrastructure as Code means your cloud environment is described declaratively in source files, and a tool reconciles the real world with that description. ...

C# Design Patterns: Factory, Singleton, Observer, Strategy

Learn C# design patterns every developer must know—Factory, Singleton, Observer, and Strategy—with runnable .NET code examples. Start writing cleaner C# today. If you have been writing C# for more than a few months, you have almost certainly hit the same wall every developer hits: a class that grows to 800 lines, a giant switch statement that needs editing every time a new feature ships, or a service that quietly creates its own dependencies so nothing can be unit tested. C# design patterns exist to solve exactly these problems. They are not academic theory—they are battle-tested solutions to recurring design problems, and the .NET ecosystem uses them everywhere, from HttpClientFactory to ASP.NET Core dependency injection to IObservable<T> . In this tutorial we will cover the four design patterns in C# that every developer must know: Factory , Singleton , Observer , and Strategy . For each one you will get a runnable example targeting modern .NET (C# 12/13), an explanati...