A common approach in vertical slice architecture is to encapsulate all aspects of a feature in one class. However, this makes the code hard to read, hard to maintain, and hard to locate for actual business logic.

 

in CatalogAPI microservices we will follow the vertical slice architecture using CQRS and mediatR

Each vertical slice cut through all architectural layers such as the UI, business logic and data access And the project organized with the models, features, data and abstractions folders etc

 

Model:

  • Lets create our models inside Models Fold-we
  • Database in User: PostgreSQL has Json column features that allowing us to store and query our data as a Json document.
  • it combines the flexibility of document database with the reliability of the relational PostgreSQL database.
  • Product.cs will be our Model Class

 

Feature Folder

  • Under Model folder, create another folder name will be the products.
  • This will be the our feature folder and under the products folder we will create each use case or feature
  • This helps in organizing our code based functionalities and keeps related components together
    • a handler will be our application logic layer 
    • endpoints will be represent our presentation API layer.
  • This separation makes it easier to locate specific parts of the code and manage them effectively. Especially, we will encapsulate business logic under the handle class, and we will see API related operations in endpoint class.

 

 

 

Develop feature handler class with CQRS and mediatR Library

The handler is responsible for executing the command logic with using the mediator library, the handler receives a command and process it, encapsulating the business logic for specific operations under the handler class.


Adding reference of Class Library Interface in our Catalog API

Add a project reference to our MsExternalBlocks library,  In our catalog project.

'

 

Create Handlers: 'CreateProductHandler.cs'

  • We will use ICommand and ICommand handler into the create product handler in catalog micro service
  • Our command object is inherited from the ICommand. And our command handler class inherits from the I command handler. And in the handler class make sure that the first parameter is create product command that triggers from the command handler.

 

using Catalog.API.Models;
using MediatR;
using System.Windows.Input;
using MsExternalBlocks.CQRS;

namespace Catalog.API.Products.CreateProduct;

// Create New Command Object, using MediatR IRequest of type CreateProductResult
public record CreateProductCommand(
   string Name, 
   List<string> Category, 
   string Description, 
   string ImageFile, 
   decimal Price)
   : ICommand<CreateProductResult>;

 

// Retrieve record
public record CreateProductResult(Guid Id);

 

internal class CreateProductCommandHandler
   : ICommandHandler<CreateProductCommand, CreateProductResult>
{
   public async Task<CreateProductResult> Handle(CreateProductCommand command, CancellationToken cancellationToken)
   {        
       //create Product entity from command object
       var product = new Product
       {
           Name = command.Name,
           Category = command.Category,
           Description = command.Description,
           ImageFile = command.ImageFile,
           Price = command.Price
       };


       //TODO
       //save to database - skip for now


       //return CreateProductResult result 
       return new CreateProductResult(Guid.NewGuid());
   }
}

 


Create Endpoints: 'CreateProductEndpoint.cs'
Including the minimal APIs and Carter library to expose an Http post endpoint.

 


Related Question