Skip to content

Commit

Permalink
Removed redundant entity generic parameter for add command handling f…
Browse files Browse the repository at this point in the history
…rom the Simple CQRS with ESDB example.
  • Loading branch information
oskardudycz committed Dec 17, 2021
1 parent 70b393e commit d94d3d9
Showing 1 changed file with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace ECommerce.Core.Commands;

public static class CommandHandlerExtensions
{
public static async Task HandleCreateCommand<TCommand, TEntity>(
public static async Task HandleCreateCommand<TCommand>(
EventStoreClient eventStore,
Func<TCommand, object> handle,
Func<TCommand, string> getId,
TCommand command,
CancellationToken ct
) where TEntity : notnull
)
{
var entityId = getId(command);
var @event = handle(command);
Expand All @@ -41,25 +41,25 @@ public static async Task HandleUpdateCommand<TCommand, TEntity>(
await eventStore.Append(id, @event, getVersion(command), ct);
}

public static IServiceCollection AddCreateCommandHandler<TCommand, TEntity>(
public static IServiceCollection AddCreateCommandHandler<TCommand>(
this IServiceCollection services,
Func<TCommand, object> handle,
Func<TCommand, string> getId
) where TEntity : notnull =>
services.AddCreateCommandHandler<TCommand, TEntity>(_ => handle, getId);
) =>
services.AddCreateCommandHandler(_ => handle, getId);

public static IServiceCollection AddCreateCommandHandler<TCommand, TEntity>(
public static IServiceCollection AddCreateCommandHandler<TCommand>(
this IServiceCollection services,
Func<IServiceProvider, Func<TCommand, object>> handle,
Func<TCommand, string> getId
) where TEntity : notnull =>
) =>
services
.AddTransient<Func<TCommand, CancellationToken, ValueTask>>(sp =>
{
var eventStore = sp.GetRequiredService<EventStoreClient>();
return async (command, ct) =>
await HandleCreateCommand<TCommand, TEntity>(eventStore, handle(sp), getId, command, ct);
await HandleCreateCommand(eventStore, handle(sp), getId, command, ct);
});

public static IServiceCollection AddUpdateCommandHandler<TCommand, TEntity>(
Expand Down Expand Up @@ -122,7 +122,7 @@ public CommandHandlersBuilder<TEntity> AddOn<TCommand>(
Func<TCommand, string> getId
)
{
services.AddCreateCommandHandler<TCommand, TEntity>(_ => handle, getId);
services.AddCreateCommandHandler<TCommand>(_ => handle, getId);
return this;
}

Expand All @@ -131,7 +131,7 @@ public CommandHandlersBuilder<TEntity> AddOn<TCommand>(
Func<TCommand, string> getId
)
{
services.AddCreateCommandHandler<TCommand, TEntity>(_ => handle, getId);
services.AddCreateCommandHandler<TCommand>(_ => handle, getId);
return this;
}

Expand All @@ -154,4 +154,4 @@ Func<TCommand, uint> getVersion
return this;
}
}
}
}

0 comments on commit d94d3d9

Please sign in to comment.