Today's Sponsor: HackerPulse — The Ultimate Dev Profile Don’t send 69 links to share your work - just share one! Bring together your GitHub, Stack Overflow, and LinkedIn profiles into a single HackerPulse dev profile. Perfect for job applications and networking, HackerPulse gives a complete picture of your skills and projects, all in one place.
Hey Reader, Today, I'm going to walk you through setting up a Blazor project using Clean Architecture. If you've ever wondered how to structure your Blazor apps to keep them maintainable, scalable, and, well, clean, you're in the right place. Watch on YouTube 📺Check out the full tutorial on YouTube to see everything in action 👇 Getting StartedFirst, let's fire up Visual Studio 2022 and create a new project. But instead of starting with a Blazor Web App right away, we're going to create a blank solution. Here’s how:
This empty solution will be our playground where we'll build out all the layers of our architecture. Setting Up the Project StructureNext, let’s organize our solution by adding a Source (src) folder. This is where all our code will live. If you were going to add tests (not in this tutorial), you’d also create a test folder. Now, let's dive into the Clean Architecture layers. Understanding Clean ArchitectureClean Architecture is all about organizing your code into different layers:
The idea is that each layer only knows about the layer below it. This keeps things clean and flexible. Creating the Domain LayerLet’s start with the Domain layer:
Here’s an example of what your entity might look like:
This is a basic entity representing a blog article. Creating the Application LayerNext up is the Application layer:
Here, you might start implementing services or CQRS patterns. For now, let's keep it simple and add a Dependency Injection setup.
This method allows us to add services from the Application layer to our Dependency Injection container. Creating the Infrastructure LayerThe Infrastructure layer handles the gritty details like data access:
Again, we’ll set up Dependency Injection here:
Creating the Presentation LayerFinally, let’s set up the Presentation layer using Blazor:
In the
This code adds the Application and Infrastructure services to the Blazor project. ConclusionAnd that’s it! You’ve set up a basic Clean Architecture with Blazor. This structure will make your app easier to maintain and scale over time. If you’re interested in diving deeper into patterns like CQRS or the Mediator pattern, let me know, and I’ll create more tutorials on those topics. Happy coding and have an awesome weekend! Take care, Patrick PS: Need help understanding .NET & Blazor? There are two ways I can help you with:
PPS: Would you like to sponsor this newsletter? Click here. 💌 |
Become a .NET & Blazor expert with weekly tutorials featuring best practices and the latest improvements, right in your inbox.
Hey Reader, Last week, I tested GPT-5 Agent Mode on .NET and Blazor apps, and the results were surprising. This week, I went a step further. I used GitHub Copilot inside Visual Studio and asked it to create real .NET and Blazor projects for me. Here’s what happened: Copilot generated a working calculator app in C#. It built a Web API that responded with “Hello GPT-5 Agents.” It even created a Blazor app with extra features like a reset button, a plus five counter, and a weather API. Were...
Hey Reader, I just published a new video where I put GPT-5’s Agent Mode to the test. 🚀 Instead of asking it for something simple, I went all in: A .NET 9 console app calculator A Web API with endpoints and docs A Blazor Server app with extra counter buttons And even a Blazor app that calls a public weather API 🎥 Watch it now: In the video, I didn’t just type “make me an app” and hope for the best. I gave GPT-5 very specific prompts, like: “Please create a .NET 9 console application that works...
Hey Reader, I’m back! 🎉 I’ve been on parental leave for the past 2 months, enjoying time with the family. Now I’m diving back into videos and here’s the first one! 🎥 Watch it now: Here’s the TL;DR if you can’t click right now: AI can already write code, fix bugs, and explain complex concepts. So… are we doomed? No. AI isn’t replacing developers anytime soon. But: Developers who use AI will replace those who don’t. The best devs are using GPT to: Skip repetitive work Prototype faster Learn...