Skip to content

Commit

Permalink
Simplify command and query registration
Browse files Browse the repository at this point in the history
  • Loading branch information
fredimachado committed Sep 28, 2023
1 parent 007b9c6 commit c0d4254
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Admin/NCafe.Admin.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

// Add services to the container.
builder.Services.AddEventStoreRepository(builder.Configuration)
.AddCommandHandlers(typeof(CreateProduct).Assembly)
.AddCommandHandlers<CreateProduct>()
.AddCommandHandlerLogger()
.AddQueryHandlers(typeof(CreateProduct).Assembly);
.AddQueryHandlers<CreateProduct>();

builder.Services.AddInMemoryReadModelRepository<Product>()
.AddEventStoreProjectionService<Product>()
Expand Down
4 changes: 2 additions & 2 deletions src/Barista/NCafe.Barista.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

// Add services to the container.
builder.Services.AddEventStoreRepository(builder.Configuration)
.AddCommandHandlers(typeof(PlaceOrder).Assembly)
.AddCommandHandlers<PlaceOrder>()
.AddCommandHandlerLogger()
.AddQueryHandlers(typeof(PlaceOrder).Assembly);
.AddQueryHandlers<PlaceOrder>();

builder.Services.AddInMemoryReadModelRepository<BaristaOrder>()
.AddEventStoreProjectionService<BaristaOrder>()
Expand Down
4 changes: 2 additions & 2 deletions src/Cashier/NCafe.Cashier.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

// Add services to the container.
builder.Services.AddEventStoreRepository(builder.Configuration)
.AddCommandHandlers(typeof(PlaceOrder).Assembly)
.AddCommandHandlers<PlaceOrder>()
.AddCommandHandlerLogger()
.AddQueryHandlers(typeof(PlaceOrder).Assembly);
.AddQueryHandlers<PlaceOrder>();

builder.Services.AddInMemoryReadModelRepository<Product>()
.AddEventStoreProjectionService<Product>()
Expand Down
8 changes: 4 additions & 4 deletions src/Common/NCafe.Infrastructure/DependencyRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ public static IServiceCollection AddEventStoreProjectionService<TModel>(this ISe
return services;
}

public static IServiceCollection AddCommandHandlers(this IServiceCollection services, Assembly assembly)
public static IServiceCollection AddCommandHandlers<T>(this IServiceCollection services)
{
services.AddSingleton<ICommandDispatcher, CommandDispatcher>();

services.Scan(s => s.FromAssemblies(assembly)
services.Scan(s => s.FromAssemblies(typeof(T).Assembly)
.AddClasses(c => c.AssignableTo(typeof(ICommandHandler<>)))
.AsImplementedInterfaces()
.WithScopedLifetime());

return services;
}

public static IServiceCollection AddQueryHandlers(this IServiceCollection services, Assembly assembly)
public static IServiceCollection AddQueryHandlers<T>(this IServiceCollection services)
{
services.AddSingleton<IQueryDispatcher, QueryDispatcher>();

services.Scan(s => s.FromAssemblies(assembly)
services.Scan(s => s.FromAssemblies(typeof(T).Assembly)
.AddClasses(c => c.AssignableTo(typeof(IQueryHandler<,>)))
.AsImplementedInterfaces()
.WithScopedLifetime());
Expand Down

0 comments on commit c0d4254

Please sign in to comment.