Saturday 11 November 2023

CQRS pattern

 CQRS, which stands for Command Query Responsibility Segregation, is a software architectural pattern that separates the concerns of handling command input (write operations) from the concerns of handling query output (read operations). In CQRS, a system is divided into two parts: the Command side and the Query side.

Command Side: Responsible for handling commands and updating the state of the system.

Query Side: Responsible for handling queries and providing read access to the system's state.

Here's a simple example of implementing the CQRS pattern in C#:

 // Command
public class CreateProductCommand
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}

// Command Handler
public class CreateProductCommandHandler
{
    public void Handle(CreateProductCommand command)
    {
        // Logic to create a new product and update the state
        // This might involve validation, persistence, and other business logic

        Console.WriteLine($"Product '{command.Name}' created successfully with price {command.Price}");
    }
}

// Query
public class GetProductQuery
{
    public int ProductId { get; set; }
}

// Query Handler
public class GetProductQueryHandler
{
    public Product Handle(GetProductQuery query)
    {
        // Logic to retrieve product information from the state
        // This might involve querying a database, caching, or other mechanisms

        return new Product
        {
            ProductId = query.ProductId,
            Name = "Sample Product",
            Price = 29.99m
        };
    }
}

// Model
public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

// Example Usage
class Program
{
    static void Main()
    {
        // Command side usage
        var createProductCommand = new CreateProductCommand
        {
            Name = "Example Product",
            Price = 49.99m
        };

        var createProductHandler = new CreateProductCommandHandler();
        createProductHandler.Handle(createProductCommand);

        // Query side usage
        var getProductQuery = new GetProductQuery
        {
            ProductId = 1
        };

        var getProductHandler = new GetProductQueryHandler();
        var product = getProductHandler.Handle(getProductQuery);

        Console.WriteLine($"Product Name: {product.Name}, Price: {product.Price}");
    }
}

In this example, the CreateProductCommand represents a command for creating a new product, and the CreateProductCommandHandler handles this command, updating the system state accordingly. On the query side, the GetProductQuery represents a query to retrieve product information, and the GetProductQueryHandler handles this query, providing the necessary data from the system state. The Product class is a simple model representing a product.

Reference: https://www.c-sharpcorner.com


0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives