Clean Architecture in Microservice with handling enterprise roles with DDD
When you want to handle business roles, DDD is one of the best choices for starting your programming. When you search on the internet you find lots of source codes and the owners that tell you this source code is so clean and you can download it and use it as a template in your project. But maybe it’s not true. You should try to find the best architecture for your project and of course, you can get help from other source codes published in the GIT or something like that. In my opinion, the fact that’s important about clean architecture in your project is you should check out lots of source codes and try to find a specific solution as a clean architect template for your project. When you want to create a microservice at first you should check whether has complex business roles or not. If you think has it, you should select DDD as a base of architecture and in Domain-driven design as you know, the start point of your project is the Domain layer(core layer). Before you start you should find all events of this domain and negotiate with a domain expert about it. For example OrderRecieved or UserRegistered. And after that, you should design the workflow for an eath event. In some situations for creating a workflow, you need to add something in a database or send an event to another microservice or need another thing, the best choice for this part is Interface. Yes, you can just create interfaces that you needed and after your domain layer is completed another layer should implement these interfaces that the domain uses in its workflow. Also, validation is one of the important steps in the Domain layer and before you created an object from entities class everything should be validated. In the domain, we don’t have access to create an object from an entity in an unvalidate state. The things that change the state of the entity and we should validate it are just in the command part of CQRS. In this project, I handled commands in the domain layer, and in the query part of CQRS, I sent any query directly from the application layer to the repository (part of the infrastructure layer) I used fluent validation to validate commands in the Domain layer and also used AutoMapper for mapped view models into the entity and vice versa. MongoDB is my DB in this project and I used MongoDB.Entities library for handling every communication with Mongo in the repository. As you know some times you need to send events to another microservice and RabbitMQ is the best choice for it and I used MassTransit to get events from Rabbit and send or publish new events to Rabbit for the use of other microservices.
You can check the source code of this article with click on below link