A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/XerProjects/Xer.Cqrs.CommandStack.Extensions.Attributes/ below:

XerProjects/Xer.Cqrs.CommandStack.Extensions.Attributes: Attribute registration extension for Xer.Cqrs.CommandStack

Xer.Cqrs.CommandStack.Extensions.Attributes

Attribute registration extension for Xer.Cqrs.CommandStack

You can simply clone this repository, build the source, reference the dll from the project, and code away!

Xer.Cqrs.CommandStack.Extensions.Attributes library is available as a Nuget package:

To install Nuget packages:

  1. Open command prompt
  2. Go to project directory
  3. Add the packages to the project:
    dotnet add package Xer.Cqrs.CommandStack.Extensions.Attributes
  4. Restore the packages:
Command Handler Attribute Registration
// Example command.
public class RegisterProductCommand
{
    public int ProductId { get; }
    public string ProductName { get; }

    public RegisterProductCommand(int productId, string productName) 
    {
        ProductId = productId;
        ProductName = productName;
    }
}

// Example Command handler.
public class RegisterProductCommandHandler : ICommandAsyncHandler<RegisterProductCommand>
{
    private readonly IProductRepository _productRepository;

    public RegisterProductCommandHandler(IProductRepository productRepository)
    {
        _productRepository = productRepository;
    }

    [CommandHandler] // This is in Xer.Cqrs.CommandStack.Extensions.Attributes. This allows the method to registered as a command handler through attribute registration.
    public Task HandleAsync(RegisterProductCommand command, CancellationToken cancellationToken = default(CancellationToken))
    {
        return _productRepository.SaveAsync(new Product(command.ProductId, command.ProductName));
    }
}
// Command Handler Registration
public CommandDelegator RegisterCommandHandlers()
{            
    // SingleMessageHandlerRegistration only allows registration of a single message handler per message type.
    var registration = new SingleMessageHandlerRegistration();
    
    // Register methods with [CommandHandler] attribute.
    registration.RegisterCommandHandlerAttributes(() => new RegisterProductCommandHandler(new ProductRepository()));

    // Build command delegator.
    IMessageHandlerResolver resolver = registration.BuildMessageHandlerResolver();
    return new CommandDelegator(resolver);
}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4