Wolverine FX
Wolverine is a toolset for command execution and message handling within .NET applications. Wolverine is:
1) An inline "mediator" pipeline for executing commands
2) A local message bus for in-application communication
3) A full-fledged asynchronous messaging framework for robust communication and interaction between services when used in conjunction with low level messaging infrastructure tools like RabbitMQ.
4) Wolverine's execution pipeline can be used directly as an alternative ASP.Net Core Endpoint provider.
Where most libraries focus on being a Mediator implementation or a Message Bus implementation, Wolverine is both.
using Wolverine;
app.MapPost("/carts/add-item", ([AsParameters] AddItemRequest request)
=> request.Bus.InvokeAsync(request.command, request.CancellationToken));
public record AddItem(Guid CartId, string Sku, int Amount);
public record AddItemRequest(AddItem command, IMessageBus Bus, CancellationToken CancellationToken);
public static class AddCartItemHandler
{
public static async ValueTask<Cart?> LoadAsync(AddItem command, ShoppingCartDbContext
context, CancellationToken cancellationToken)
{
var cart = await context.FindAsync<Cart>(command.CartId, cancellationToken);
return cart ?? throw new ApplicationException("Cart not found");
}
public static CartItemAdded Handle(AddItem command, Cart cart, ShoppingCartDbContext
context)
{
var item = cart.Items.SingleOrDefault(i => i.Sku == command.Sku);
if (item is null)
{
item = new CartItem(command.Sku, command.Amount);
cart.Items.Add(item);
}
else
{
item.Amount += command.Amount;
}
context.Update(cart);
return new CartItemAdded(cart.Id, item.Sku, item.Amount);
}
}
app.MapPost("/carts/add-item", ([AsParameters] AddItemRequest request)
=> request.Bus.InvokeAsync(request.command, request.CancellationToken));
public record AddItem(Guid CartId, string Sku, int Amount);
public record AddItemRequest(AddItem command, IMessageBus Bus, CancellationToken CancellationToken);
public static class AddCartItemHandler
{
public static async ValueTask<Cart?> LoadAsync(AddItem command, ShoppingCartDbContext
context, CancellationToken cancellationToken)
{
var cart = await context.FindAsync<Cart>(command.CartId, cancellationToken);
return cart ?? throw new ApplicationException("Cart not found");
}
public static CartItemAdded Handle(AddItem command, Cart cart, ShoppingCartDbContext
context)
{
var item = cart.Items.SingleOrDefault(i => i.Sku == command.Sku);
if (item is null)
{
item = new CartItem(command.Sku, command.Amount);
cart.Items.Add(item);
}
else
{
item.Amount += command.Amount;
}
context.Update(cart);
return new CartItemAdded(cart.Id, item.Sku, item.Amount);
}
}
Comments
Post a Comment