In Today’s discussion, we will be discussing about clean architecture and how to get started with it. Clean Architecture belong to the Domain Driven Design family. Before that, let’s understand what was the problem with N-Layered Architecture.
With layered architecture, you can see database is at center, which means, all dependencies point towards the database. With domain centric architecture, domain and use cases are central, and other things like presentation, persistence are just details. here, domain is at the center of the application, encapsulated in application layer. And, all dependencies point towards domain.
Having said that, now, let’s look at few domain centric architecture.
First we have alistair cockburn, hexagonal architecture. its a layered architecture with the application layer, domain at the center of the architecture. In a way, its a plugin architecture, which includes ports and adapters. Here, outer layer of the architecture, adopting inner layer of the application through the various presentation, persistence and external systems. You can run and test the entire system without UI, database or any external dependency.
Next, we have onion architecture by Jefferry palermo. This is also layered architecture, with domain at the center surrounded by application layer. The outer layers consist of a thin UI as a presentation layer, tests and an Infrastructure, which includes persistence. In addition, all dependencies point towards center of the architecture, which means no inner layer knows about outer layer. Once, again, this application can be tested without an UI, database or any external dependency.
Finally, we have the clean architecture uncle bob. Once again its a layered architecture with domain that is the entities, being the focal point of the entire system. Domain, layer is surrounded by an application layer, which is nothing but the use cases. The outer layers consists of ports and adapters adapting to the application core to the external dependencies via controllers, gateways and presenters. In addition, uncle bob goes one step ahead by incorporating Iva Jacobson’s BEC architecture pattern to explain how the presentation layer and application layer should be wired up. All three of these architectures, have roughly of the same benefits.
However, as Mark Semmans pointed out, all of these domain centric architectures just focusing on different aspects of the same key set of ideas. Essentially, they all put domain model at the center, wrap it in application layer which involves use cases adapts the application to the implementation details and all dependencies should point to inward towards the domain.
1) Focus on domain which is basically center focal point of the architecture.
2) Less coupling between domain logic and implementation details. This means system becomes more flexible and adaptable.
3) This also means we can easily evolve architecture over time.
4) This also means, it allows us use DDD pattern which is a great set of strategies by eric evans.
Having said that, let’s create the application. First, we will create the solution.
After that, let’s add API project
Similarly, I added other projects like shown below. Other Projects are class libraries project.
Then, I added project references. Here, the key thing is inner layer should not know about outer layer but outer layer should know about inner layer. Hence, Core will have no project reference. But, Infrastructure will know about Core. Application will know about Core and Infrastructure. And, API will know about all three layers like shown below.
Now, my infrastructure is going to need these nuget packages.
Application is going to need below mentioned packages.
And, API is going to need these packages. Don’t worry about these. You will get to know in a while, why these packages added here.
Having said that, let’s build application once, just to make sure that application is in right shape. Now, let’s go in the core Project and create folder with the name Entities. Now, let’s create another folder with the name Base inside the Entities folder. Here, I will create one interface called IEntityBase, where in I will substitute the ID here. Because in future you maybe switching to different Db say mongo, where ID being string, hence you can then substitute that easily. Hence, it will look like.
And, Entity base will look like as shown below. This is the place, where I am keeping common properties, which I will be implementing later that’s why I marked this as abstract.
I have marked this as virtual so that I can override that and I have also set as protected so that in the derived class, I can set this Id.
Next, I will be having Entity class, which is also an abstract class, which is the base representation of entity itself. Think of these stuff as from Framework perspective, nothing to do from clean architecture. And, here I am supplying Int as type for the TId. For different data adapters, you can pass different type here. This way, its become more generic implementation here.
Now, let’s go ahead and create Movie class under Entities folder like shown below.
Let’s create Repositories folder now. And in here create Base folder. Now, let’s create IRepository.cs interface in the folder. It will look like
This is the generic CRUD signature and nothing else. Now, I can have another interface inheriting IRepository with some custom method which is movie related at this place.
Therefore, my Core project will look like
In the next post, we will discuss other areas and implement the same. Till then stay tuned and Happy Coding.
3,969 total views, 2 views today