Skip to main content

Posts

Showing posts with the label MediatR Pattern

A Demo On Clean Architecture | MediatR CQRS Pattern | .NET7 Web API

In this article, we will learn about 'Clean Architecture', 'CQRS Pattern With MediatR' by implementing in .NET7 Web API. Clean Architecture: The Clean Architecture comprises of: Domain Layer Application Layer Infrastructure Layer UI Layer Clean Architecture lives on the dependency inversion principle. In general business logic depends on the data access layer or infrastructure layer. But in clean architecture, it is inverted, which means the data access layer or infrastructure layers depends on the business logic layer(which means the Application Layer) . Domain Layer: the domain layer project will have 'Entities'(Domain Models), 'Domain Services', and 'Value Object'. This project is a parent project and it is independent on all other projects. Application Layer: the application layer project contains business logic files, DTOs, mapper, etc. This project can depend only on the 'Domain Layer Project'. Infrastructure Layer: the infrastruct

A Demo On Request Logging Using MediatR IPipelineBehavior In Asp.Net Core Application

Click here  for step by step implementation of the CQRS MediatR design pattern. In this article, we are going to do request logging into an Asp.Net application Core application build on the CQRS MediatR design pattern. The application request logging helps to track the time taken per request. The MediatR provides a pipeline interface that is 'IPipelineBehavior' that helps to implement our logging logic. IPipelineBehavior: The 'IPipelineBehavior' exactly works like Asp.Net Core middleware, but its starting execution and ending execution happens within the 'IMediator'. So the 'IMediator.send()' is usually used to invoke the 'Query' or 'Command' handlers. So if we implement 'IPipelineBehavior' then begin logic inside of its start executes then invokes 'Query' or 'Command' handlers, later again go through 'IPipelineBehavior' and executes the end logic. Create A .Net5 Web API Sample Application: In this

A .Net5 Sample Using CQRS(Command Query Responsibility Segregation) And MediatR Patterns

CQRS stands for Command Query Responsibility Segregation. CQRS guides us to separate our logical implementations into 2 categories like 'Commands', 'Query'. The 'Commands' specifies the operations like creation or updating of data into the data source(database). The 'Query' specifies the operations to fetch the data. In CQRS models(Request/Response classes) are independent or owned by a single operation, which means model classes can not be shared between the different 'Commands' or different 'Queries' or between a 'Command' and 'Query'. From the diagram one thing to observe Request/Response model(optional), that's because some times we will use query parameters or return a simple scalar type in those cases we won't create models. Create .Net5 Web API: To implement the CQRS pattern let's create a sample .Net5 Web API application. Configure Entity Framework Core Database Context: For this demo, I had created