Apr 08 2025
public record User(Guid Id, string Name, string Email);
public static class InMemoryUsers
{
public static readonly List<User> Users = new();
}
public record CreateUser(string Name, string Email);
public record GetUser(Guid Id);
public record UpdateUser(Guid Id, string Name, string Email);
public record DeleteUser(Guid Id);
public class CreateUserHandler
{
public User Handle(CreateUser command)
{
var user = new User(Guid.NewGuid(), command.Name, command.Email);
InMemoryUsers.Users.Add(user);
return user;
}
}
public class GetUserHandler
{
public User? Handle(GetUser query)
{
return InMemoryUsers.Users.FirstOrDefault(u => u.Id == query.Id);
}
}
public class UpdateUserHandler
{
public User? Handle(UpdateUser command)
{
var user = InMemoryUsers.Users.FirstOrDefault(u => u.Id == command.Id);
if (user is null) return null;
var updated = user with { Name = command.Name, Email = command.Email };
InMemoryUsers.Users.Remove(user);
InMemoryUsers.Users.Add(updated);
return updated;
}
}
public class DeleteUserHandler
{
public bool Handle(DeleteUser command)
{
var user = InMemoryUsers.Users.FirstOrDefault(u => u.Id == command.Id);
if (user is null) return false;
InMemoryUsers.Users.Remove(user);
return true;
}
}
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthorization();
builder.Host.UseWolverine(); // Enable Wolverine
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
// Create
app.MapPost("/users", async (CreateUser request, IMessageBus bus) =>
{
var user = await bus.InvokeAsync<User>(request);
return Results.Created($"/users/{user.Id}", user);
});
// Read
app.MapGet("/users/{id:guid}", async (Guid id, IMessageBus bus) =>
{
var user = await bus.InvokeAsync<User?>(new GetUser(id));
return user is null ? Results.NotFound() : Results.Ok(user);
});
// Update
app.MapPut("/users/{id:guid}", async (Guid id, UpdateUser command, IMessageBus bus) =>
{
if (id != command.Id) return Results.BadRequest();
var updated = await bus.InvokeAsync<User?>(command);
return updated is null ? Results.NotFound() : Results.Ok(updated);
});
// Delete
app.MapDelete("/users/{id:guid}", async (Guid id, IMessageBus bus) =>
{
var deleted = await bus.InvokeAsync<bool>(new DeleteUser(id));
return deleted ? Results.NoContent() : Results.NotFound();
});
app.Run();
1. Design Patterns that Deliver
This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 15,250+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 15,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.