Clean Architecture Project Setup From Scratch With .NET 7

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
clean architecture is a popular way to organize your applications some people love it some people absolutely hate it but at the end of the day it's still a good architectural approach and in this video I'm going to show you how to set up the clean architecture from scratch when I said that we were going to set up the clean architecture from scratch I literally meant it we are going to start from a blank solution in visual studio and work our way up to the full clean architecture structure I typically start by introducing a solution folder which is going to contain all of my projects this is useful for splitting your actual source code from your tests we're going to start off from the core of the clean architecture and this is the domain layer so this layer is typically just a class library in your solution which you can call domain or you can append your project name and then the main it depends what kind of naming convention you want to use let's make it a.net 7 project and let's create it we can get rid of this default class right away and I'm going to leave the domain project completely empty what you would typically Define in the domain project are your entities your core business rules Factory interfaces enumerations value objects custom exceptions and so on I'm not going to be doing that right now we're just going to focus on setting up the skeleton structure for the clean architecture the next project that we need to introduce is the application project again this is going to be a class Library I'm going to call it application and it's going to be at.net 7 project so this is where it gets a little bit more interesting let's get rid of this default class now the domain layer is not allowed to reference any of the outer layers in the clean architecture but the application layer needs to have a project reference to The Domain layer so I'm going to introduce that right away and let's see what we can add to our application layer this is where you want to implement your use cases for your application you can implement this in any way you like you can use Services I typically use mediator for my use cases so I'm going to add the mediator nuget package so let me go ahead and install it and notice that this is the latest mediator version which actually came out just a few days ago so I'm going to install it inside of the application layer what I also like to use in the application layer is the fluent validation package so I'm going to go ahead and install that as well I can go ahead and install the fluent validation Library directly or I can go ahead and install this one which contains the fluent validation Library within it and it also contains support for dependency injection this is useful if you want to Define your dependency injection logic per your layer in the clean architecture so let's try out that approach I'm going to install this Library here which contains the dependency injection extensions and let's see how we can introduce dependency injection into the application layer one approach can be to create a template class that you're going to have in every layer where you want to introduce dependency injection and let's just call it dependency injection I'm going to make this a public and static class and let's give it one static method which is going to be the add application method I can either make it an extension method on the iservice collection interface or I can have it accept the iservice collection interface as an argument let's for example make it an extension method on the iservice collection interface and what we need to put inside is the registration Logic for the two libraries that we added the first is to add mediator support and you can see that with this new version of mediator we have a new way to apply our configurations so we have to provide an action that accepts an mediator service configuration object so I'm going to name this argument as configuration and let's see what we have available you can see that we have methods for adding the pipeline behaviors what I'm interested in doing is to register services from the assembly and I'm going to pass it the current assembly so I'm going to say type of dependency injection and we're going to use the assembly coming from this type so let's pass that in so this is going to take care of configuring mediator let's also add the validators from this assembly this is going to wire up what's required for fluent validation I just need to pass in an assembly instance that I already have here so let's extract this into a variable so I'm going to say assembly and we're going to do this so let's pass it to the mediator and fluent validation methods and of course at the end I need to return a Services instance so that I can contain this using a fluent interface so this roughly takes care of the application layer we added the mediator and fluid validation libraries and we configured our dependency injection method the next layer in the clean architecture is the infrastructure layer and here you want to introduce any external concerns such as external services or maybe the database some people like to split the infrastructure layer into the infrastructure and persistence layers where the persistence layer takes care of everything that is database related you can do this if you want to but it comes down to a personal preference so let's keep it at one project to make it simple which I will call infrastructure let's also make it a.net 7 project we're not going to be adding any external libraries here let's also introduce the dependency injection class into the infrastructure project I'm going to copy the one from the application layer and we're going to adjust it for the infrastructure layer I'm going to rename this to the infrastructure namespace let's get rid of this part here we're going to call this method at infrastructure and we just need to add a nougat package to expose the I service collection interface you can do this by installing the Microsoft extensions dependency injection abstractions package so I'm going to go ahead and do that and this takes care of our infrastructure layer now the next layer is the presentation layer here again you have two options one approach is to have a separate project for the presentation layer here you're going to place your controllers or your minimal API endpoints or your razor Pages depending what you're using for the presentation the other approach is to justify a web API project and Define all of these inside of that project as well so I'm going to actually create a separate project for the presentation layer again it's going to be a class Library I'm going to call it presentation it's going to be a.net 7 project and let's introduce the dependency injection class like we did with the infrastructure project I'm going to to call it add presentation we're going to rename the namespace and let's install the library that we need to have access to the iservice collection interface so our project is slowly starting to get some structure and the last piece that we're missing is the actual web API project that is going to run our application so I'm going to add an asp.net core web API and let's call it web API so now we have a little bit more options for example I can enable Docker support if I want to Let's for example turn that on we can use controllers or minimal apis depending on what we want to do let's go ahead with minimal apis in this case I'm going to enable open API support so that we have Swagger and let's create this project the web API project is actually going to be the startup project of our solution and let's give it a reference to all of the other projects in the solution so presentation infrastructure and application you don't need to add a direct reference to The Domain project because you're going to get it implicitly through the application project and let's go to the program.cs class now you can see that here by default we have a lot of boilerplate code so let's unpack this and clean this up I'm going to get rid of this endpoint as well and this class here so this is your minimal API template that you get out of the box and let's add the registrations for all of our layers so Builder services and let's call add application and let's also call add infrastructure and lastly let's call add presentation so this takes care of configuring our separate layers with dependency injection if you're enjoying this video so far make sure that you smash that like button and also subscribe to my channel so that you don't miss any of my future videos now with that out of the way let's introduce one last component to our web API project and this is something that you always want to set up right at the start of your project and this is logging and I'm going to install the serolog asp.net core library because it integrates nicely with asp.net core and it comes with file and console syncs out of the box how you configure stereog very quickly is by calling the host Builder and you call the use serolog method you need to pass this method a function to apply the actual configuration with the host Builder context and the logger configuration as the arguments and the approach I like to use is to apply the serolog configuration from my application settings so you can say configuration read from the configuration of your application and you need to pass it on my configuration instance by calling context and passing the configuration value so the one thing that's missing is to specify the actual configuration in your application settings you can replace this logging section here with something like this we're just going to apply some configuration for serolog I already covered this in a previous video where I talked about serolog and structured logging in depth so I'm not going to go over that right here you can also go ahead and introduce HTTP request logging for your application by calling use server log request logging on the web application instance so this is the high level approach to achieve a clean architecture structure with dotnet7 you have your individual layers of the clean architecture the domain application infrastructure and presentation layers and you have one web API project that ties them all together at runtime this is a good project structure to get started with and you can evolve it over time as your application progresses or your architecture requirements change you have to be aware that the cleaner architecture is not a silver bullet and it's not going to magically solve all of your problems you're actually going to have to think hard about your architecture and decide if this is something that you should use in your own projects I've personally use clean architecture on many projects in the past and it has worked out very well for me which is why I advocate for it but there are of course many alternative approaches such as your typical and layer architecture and recently one approach that has been gaining a lot of popularity is the vertical slice architecture which I may talk about in a future video I hope that you enjoyed this video about scaffolding a clean architecture project from scratch subscribe to my channel so that you don't miss any of my future videos and until next time stay awesome
Info
Channel: Milan Jovanović
Views: 54,020
Rating: undefined out of 5
Keywords: clean architecture, clean architecture explained, clean architecture example, clean architecture .net, clean architecture .net core, clean architecture asp.net core, clean architecture asp.net core 7, clean architecture project setup, clean architecture tutorial, clean architecture from scratch, clean architecture cqrs, clean architecture setup, clean architecture c#, minimal apis, web apis, rest apis, c# api, .net api, clean architecture api, clean architecture minimal apis
Id: fe4iuaoxGbA
Channel Id: undefined
Length: 12min 1sec (721 seconds)
Published: Fri Mar 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.