Skip to content

Commit

Permalink
Refactor product command handling
Browse files Browse the repository at this point in the history
Introduced `CreateProduct` command and handler in `CreateProduct.cs`, replacing the removed `ProductCreated` command and its associated classes. Updated service registration in `Program.cs` to reflect this change. Modified `MessagingSample.csproj` to set `<IsPackable>false</IsPackable>`, indicating the project is not packable as a NuGet package.
  • Loading branch information
penyland committed Jan 18, 2025
1 parent 83ce908 commit e6ebd4d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
28 changes: 28 additions & 0 deletions samples/MediatorSample/CreateProduct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Infinity.Toolkit.Experimental;
using Infinity.Toolkit.Experimental.Mediator;

namespace MediatorSample;

internal record CreateProduct(int Id, string Name) : ICommand;

internal class CreateProductHandler : IMediatorHandler<CreateProduct>
{
public Task<Result> HandleAsync(MediatorHandlerContext<CreateProduct> context, CancellationToken cancellationToken = default)
{
Console.WriteLine("CreateProductHandler:HandleAsync");
Console.WriteLine($"Product created: {context.Request.Id} - {context.Request.Name}");
Console.WriteLine("CreateProductHandler:HandleAsync:Done");
return Task.FromResult(Result.Success());
}
}

internal class CreateProductDecorator(IMediatorHandler<CreateProduct> inner) : IMediatorHandler<CreateProduct>
{
public async Task<Result> HandleAsync(MediatorHandlerContext<CreateProduct> context, CancellationToken cancellationToken = default)
{
Console.WriteLine("CreateProductDecorator:HandleAsync");
var result = await inner.HandleAsync(context, cancellationToken);
Console.WriteLine("CreateProductDecorator:HandleAsync:Done");
return result;
}
}
28 changes: 0 additions & 28 deletions samples/MediatorSample/ProductCreated.cs

This file was deleted.

6 changes: 3 additions & 3 deletions samples/MediatorSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
.AddMediator()
.Decorate<IMediator, LoggingMediator>();

services.AddMediatorHandler<ProductCreated, ProductCreatedHandler>()
.Decorate<ProductCreatedDecorator>();
services.AddMediatorHandler<CreateProduct, CreateProductHandler>()
.Decorate<CreateProductDecorator>();

services.AddMediatorHandler<ProductCreatedQuery, string, ProductCreatedQueryHandler>();
services.DecorateMediatorHandler<ProductCreatedQuery, string, ProductCreatedQueryHandlerDecorator>();

var serviceProvider = services.BuildServiceProvider();
var mediator = serviceProvider.GetRequiredService<IMediator>();

await mediator.SendAsync(new ProductCreated(1, "Product 1"));
await mediator.SendAsync(new CreateProduct(1, "Product 1"));
var result = await mediator.SendAsync<ProductCreatedQuery, string>(new ProductCreatedQuery());

Console.WriteLine("Done");
Expand Down
1 change: 1 addition & 0 deletions samples/MessagingSample/MessagingSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit e6ebd4d

Please sign in to comment.