The Clean Architecture | Simply Explained | .NET Core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] today we are going to discuss a high level overview on a very relevant topic many entry level programmers are either not aware of or didn't understand properly the clean architecture let's understand what and why we need a solid architecture first this is a poorly designed application which contains different modules like models databases logics all tightly integrated and one day you want to make some changes to the logic but due to tight coupling between the modules changes in one module have significant effect on the other this application is neither scalable nor extendable this is happening due to a lack of proper architecture to the project so how can we resolve this to be honest there are a lot of ways but today we are going to focus on clean architecture see clean architecture is telling us to group our project into different layers this is the outermost shell which is the infrastructure layer we will get into detail inside this comes the application layer here we deal with our business logics last and at the very center you can find the domain layer now let's understand what exactly each layer means and how they are in the depend for that consider we are creating an application to drive a car every day and whenever it sees a red light stop the car and write to a database we will create a new application with three projects representing three layers of clean architecture let's go to our solution right click on the solution click on add click on new project on the project window select class library click on next and name your project infrastructure as we are going to create the outer layer click on next select the framework click on next and now we can remove the default class now that we have created the infrastructure layer similarly create both the application and the domain layer so now we have the three different layers of the project ready to be coded [Music] let's analyze the dependency flow now clean architecture only allows dependencies to flow from outwards to inwards this means our outer layer infrastructure can depend on both the domain and the application layer whereas the application layer can only depend on domain layer and the domain layer can't depend on anything else to implement that right click on the infrastructure project and add the project dependency to both application and the domain layer now if you see the infrastructure has the dependency with both application and the domain layers similarly let the application layer depends on the domain project select the domain project and click ok now you can see that the domain project is added as a dependency to the application layer ideally when it comes to the domain layer we should keep it dependent free as possible project layers are done now what we need to put in these layers let's take our domain layer this is the layer where you need to store your core business models in our case the car class very important don't mess up this layer with any custom logics just your business objects and that is enough however if you have any other entities like constants or enums that are used by your model classes then you can place them in the same layer let's go with the application layer this is the layer where you need to store your logic your logic can use classes created in the previous domain layers the point here is your logic should be abstract means you should do your business logic using interfaces as much as possible see this we have created our business logic in the form of interfaces or abstract classes here we have created an interface for the car to drive break and to hone and if you see we are using our domain business object car as a parameter to invoke the drive method here since we use interfaces we don't care about how to drive we just know for sure somebody will drive by making use of interfaces and abstract classes as much as possible build up your logic which can turn out to be extremely extensible highly testable and easy to maintain along with your core abstract business logics if you have dto classes you can place them here if you are using mappers like automapper this is the better layer to place them to know everything on automapper check out the dedicated automapper video if you are using cqrs or mediator pattern in your project this is the better layer to place your handlers to know everything on cqrs and mediator check out the dedicated secures and mediator pattern video if you are using validators like fluent validator in your project you can also place them in this layer to know everything on fluent validator check out the dedicated fluid variator video [Music] now we will discuss about the last layer which is our infrastructure layer this is where things really starts to move here instead of working with interfaces we will implement them in a concrete class for our example we can create an implementation of car let's say limousine if limousine implements our icar interface it should define everything icare can do here we can write how to drive a limousine how to break a machine and how a limousine should hone if we create another car let's say od we can also drive brake and horn anodi tomorrow a new car came in you can simply add the car and implement the interface icar no change required on your core application business logic this is where we achieve the maintainability now extensibility is really important if you know the dependency inversion principle and the lisco substitution principle in solid programming practices you will be already aware that an interface can be ascend with its implementation this is where we achieve extensibility as our application layer receives everything as interfaces we can plug and play with any car like limousine od or rolls-royce provided they all implement the i-car now if you need to know what are solid principles we have a dedicated video for that as well as we are using interfaces on application layer you have the ability to mock in unit test projects this is why our application logic become well testable if you don't know how to write unit test or how to mock we have a dedicated video for that also along with interface implementations we can also use concrete classes to inherit or override methods in an abstract class from the application layer all these features really contribute to a great application where its foundation is so strong thank you
Info
Channel: The Code Switch
Views: 20,575
Rating: undefined out of 5
Keywords: c#, .net
Id: _1cJ0JOLe8Y
Channel Id: undefined
Length: 6min 39sec (399 seconds)
Published: Sat Sep 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.