Posts

Showing posts with the label mediator

No More Data Transfer Objects: A Guide to Adopting CQRS 📝🔎

Image
   In the software engineering industry, the term CQRS is used quite often. It describes a pattern which allows you to handle services in your system in a vertical manner. It gives you the ability to free yourself from any other service in the same domain and allows you to focus on the service that you wish to implement. I know that there are hundreds of other resources that can explain the CQRS pattern better in terms of definition and philosophy. However, in this article I'm going to explain it in another way, a rather practical way. Imagine you have an entity named Customer . Normally if we follow clean architecture , we will have a single horizontally scaled service called CustomerService . In this CustomerService there are all types of services that perform different operations on this entity. However not all services need to operate on the whole entity model. This is why Data Transfer Objects exist. Data Transfer Objects (DTOs) are a simplified versions of entity mode...

MediatR Behavioral Pipeline: Validation

Image
 In my previous post  Download Image API Using MediatR Vertical Architecture  I talked about how to use MediatR to perform commands or queries however I have not explained how to integrate a validator so that you can validate first then handle the request you receive. In this article I'm going to show you how to add a validator to your pipeline. A pipeline that in my case will only consists of two steps validation and handling. I will be using the same set-up as my previous post so if you're going to follow the next steps make sure you complete the example in my previous post linked above. Fluent Validation First step we need to add a very useful and popular library which will be the validator that holds all of the rules that the request should follow. It's an important library that provides validation rules (custom and out of the box) with readable members such as NotEmpty() , Must() , etc. Fluent Validation is not the only option you can also use Data Annotations howev...

Download Image API Using MediatR Vertical Architecture

Image
As I mentioned in one of my previous posts, I will be showing you how to make an API that downloads an image to a local path.  But I will take this as an opportunity to show you an example of how to achieve that using vertical architecture with MediatR . One of the most common problems with clean architecture is that there usually is a service class for each domain entity that encapsulates all application logic regarding this entity.  Something that if you have applied on a larger scale project with a little too much business logic you would find that the class got too big, and maintainability becomes a little more difficult by each added logic.  Also, data transfer objects ( DTO ) start to have weirder names because you can't have a one-size-fits-all model that accommodate the growing logic in your service class.  (eg. CustomerDTO.cs, LoyalCustomerDTO.cs, CustomerLightWeightDTO.cs) Not to mention of course pull requests and merging codes become a living nightmare....