Step 1: Create Handlers in Class Library
Access / Use Handler in our Catalog Micro services
Open Microservice Project, Go to “CreateProductEndpoint” file, Locate the “CreateProductEndpoint” class.
using Catalog.API.Models;
using MsExternalBlocks.CQRS;
namespace Catalog.API.Products.CreateProduct;
// A Command Object using MediatR IRequest of type CreateProductResult
public record CreateProductCommand(
string Name,
List<string> Category,
string Description,
string ImageFile,
decimal Price)
: ICommand<CreateProductResult>;
// Result Object
public record CreateProductResult(Guid Id);
// A Command Object Handler
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());
}
}
Register Mediator library into ASP.Net Dependency Injection Service and Request pipeline.

