Skip to content

Commit 4f14136

Browse files
committed
Refactor constructors and update Program.cs endpoints
Refactored constructors for better readability by placing each parameter on a new line and replacing `var` with explicit types where applicable. Modified `Startup.cs` to change `ConfigureServicesAsync` to a synchronous `ConfigureServices` method. Simplified `OrdersController.Get` method using a ternary operator. Updated `Program.cs` to remove `UseCore` and `UseGenocs` method calls, added `app.MapDefaultEndpoints()`, `app.Run()`, `app.UseHttpsRedirection()`, and `app.UseStaticFiles()`, and reformatted `builder` configuration for better readability.
1 parent d5cf8ab commit 4f14136

File tree

11 files changed

+67
-52
lines changed

11 files changed

+67
-52
lines changed

src/apps/api-gateway/Genocs.APIGateway/Startup.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ public Startup(IConfiguration configuration)
2020
Configuration = configuration;
2121
}
2222

23-
public async Task ConfigureServicesAsync(IServiceCollection services)
23+
public void ConfigureServices(IServiceCollection services)
24+
{
25+
// Find a more elegant way to do this
26+
Task.Run(async () => await ConfigureServicesAsync(services)).Wait();
27+
}
28+
29+
private async Task ConfigureServicesAsync(IServiceCollection services)
2430
{
2531
services.AddScoped<LogContextMiddleware>();
2632
services.AddScoped<UserMiddleware>();

src/apps/identity/Genocs.Identities.Application/Commands/CreateUser.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ public class CreateUser : ICommand
1111
public string Role { get; }
1212
public IEnumerable<string> Permissions { get; }
1313

14-
public CreateUser(Guid userId, string email, string name, string password, string role,
15-
IEnumerable<string> permissions)
14+
public CreateUser(
15+
Guid userId,
16+
string email,
17+
string name,
18+
string password,
19+
string role,
20+
IEnumerable<string> permissions)
1621
{
1722
UserId = userId == Guid.Empty ? Guid.NewGuid() : userId;
1823
Email = email;

src/apps/identity/Genocs.Identities.Application/Decorators/LoggingCommandHandlerDecorator.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ internal sealed class LoggingCommandHandlerDecorator<TCommand> : ICommandHandler
1515
private readonly ICorrelationIdFactory _correlationIdFactory;
1616
private readonly ILogger<ICommandHandler<TCommand>> _logger;
1717

18-
public LoggingCommandHandlerDecorator(ICommandHandler<TCommand> handler,
19-
ICorrelationIdFactory correlationIdFactory, ILogger<ICommandHandler<TCommand>> logger)
18+
public LoggingCommandHandlerDecorator(
19+
ICommandHandler<TCommand> handler,
20+
ICorrelationIdFactory correlationIdFactory,
21+
ILogger<ICommandHandler<TCommand>> logger)
2022
{
2123
_handler = handler;
2224
_correlationIdFactory = correlationIdFactory;
@@ -25,10 +27,10 @@ public LoggingCommandHandlerDecorator(ICommandHandler<TCommand> handler,
2527

2628
public async Task HandleAsync(TCommand command, CancellationToken cancellationToken = default)
2729
{
28-
var correlationId = _correlationIdFactory.Create();
30+
string? correlationId = _correlationIdFactory.Create();
2931
using (LogContext.PushProperty("CorrelationId", correlationId))
3032
{
31-
var name = command.GetType().Name.Underscore();
33+
string? name = command.GetType().Name.Underscore();
3234
_logger.LogInformation($"Handling a command: '{name}'...");
3335
await _handler.HandleAsync(command);
3436
}

src/apps/identity/Genocs.Identities.Application/Decorators/LoggingEventHandlerDecorator.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ internal sealed class LoggingEventHandlerDecorator<TEvent> : IEventHandler<TEven
1515
private readonly ICorrelationIdFactory _correlationIdFactory;
1616
private readonly ILogger<IEventHandler<TEvent>> _logger;
1717

18-
public LoggingEventHandlerDecorator(IEventHandler<TEvent> handler, ICorrelationIdFactory correlationIdFactory,
19-
ILogger<IEventHandler<TEvent>> logger)
18+
public LoggingEventHandlerDecorator(
19+
IEventHandler<TEvent> handler,
20+
ICorrelationIdFactory correlationIdFactory,
21+
ILogger<IEventHandler<TEvent>> logger)
2022
{
2123
_handler = handler;
2224
_correlationIdFactory = correlationIdFactory;
@@ -25,10 +27,10 @@ public LoggingEventHandlerDecorator(IEventHandler<TEvent> handler, ICorrelationI
2527

2628
public async Task HandleAsync(TEvent @event, CancellationToken cancellationToken = default)
2729
{
28-
var correlationId = _correlationIdFactory.Create();
30+
string? correlationId = _correlationIdFactory.Create();
2931
using (LogContext.PushProperty("CorrelationId", correlationId))
3032
{
31-
var name = @event.GetType().Name.Underscore();
33+
string? name = @event.GetType().Name.Underscore();
3234
_logger.LogInformation($"Handling an event: '{name}'...");
3335
await _handler.HandleAsync(@event);
3436
}

src/apps/identity/Genocs.Identities.Application/Decorators/OutboxCommandHandlerDecorator.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@ internal sealed class OutboxCommandHandlerDecorator<TCommand> : ICommandHandler<
1515
private readonly string _messageId;
1616
private readonly bool _enabled;
1717

18-
public OutboxCommandHandlerDecorator(ICommandHandler<TCommand> handler, IMessageOutbox outbox,
19-
OutboxOptions outboxOptions, IMessagePropertiesAccessor messagePropertiesAccessor)
18+
public OutboxCommandHandlerDecorator(
19+
ICommandHandler<TCommand> handler,
20+
IMessageOutbox outbox,
21+
OutboxOptions outboxOptions,
22+
IMessagePropertiesAccessor messagePropertiesAccessor)
2023
{
2124
_handler = handler;
2225
_outbox = outbox;
2326
_enabled = outboxOptions.Enabled;
2427

2528
var messageProperties = messagePropertiesAccessor.MessageProperties;
2629
_messageId = string.IsNullOrWhiteSpace(messageProperties?.MessageId)
27-
? Guid.NewGuid().ToString("N")
28-
: messageProperties.MessageId;
30+
? Guid.NewGuid().ToString("N")
31+
: messageProperties.MessageId;
2932
}
3033

3134
public Task HandleAsync(TCommand command, CancellationToken cancellationToken = default)

src/apps/identity/Genocs.Identities.Application/Decorators/OutboxEventHandlerDecorator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ internal sealed class OutboxEventHandlerDecorator<TEvent> : IEventHandler<TEvent
1515
private readonly string _messageId;
1616
private readonly bool _enabled;
1717

18-
public OutboxEventHandlerDecorator(IEventHandler<TEvent> handler, IMessageOutbox outbox,
19-
OutboxOptions outboxOptions, IMessagePropertiesAccessor messagePropertiesAccessor)
18+
public OutboxEventHandlerDecorator(
19+
IEventHandler<TEvent> handler,
20+
IMessageOutbox outbox,
21+
OutboxOptions outboxOptions,
22+
IMessagePropertiesAccessor messagePropertiesAccessor)
2023
{
2124
_handler = handler;
2225
_outbox = outbox;

src/apps/identity/Genocs.Identities.WebApi/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@
2525

2626
gnxBuilder.Build();
2727
var app = builder.Build();
28-
2928
app.UseCore();
3029
app.UseDispatcherEndpoints(endpoints => endpoints
31-
.Get(string.Empty, ctx => ctx.GetAppName())
3230
.Post<SignIn>("sign-in", afterDispatch: (cmd, ctx) =>
3331
{
3432
var auth = ctx.RequestServices.GetRequiredService<ITokenStorage>().Get(cmd.Id);
@@ -51,6 +49,8 @@
5149
.Put<LockUser>("users/{userId:guid}/lock", auth: true, roles: "admin")
5250
.Put<UnlockUser>("users/{userId:guid}/unlock", auth: true, roles: "admin"));
5351

52+
app.MapDefaultEndpoints();
53+
5454
app.Run();
5555

5656
Log.CloseAndFlush();

src/apps/orders/Genocs.Orders.WebApi/Controllers/OrdersController.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ public OrdersController(ICommandDispatcher commandDispatcher, IQueryDispatcher q
2424
public async Task<ActionResult<OrderDto>> Get([FromRoute] GetOrder query)
2525
{
2626
var order = await _queryDispatcher.QueryAsync(query);
27-
if (order is null)
28-
{
29-
return NotFound();
30-
}
31-
32-
return order;
27+
return order is null ? (ActionResult<OrderDto>)NotFound() : (ActionResult<OrderDto>)order;
3328
}
3429

3530
[HttpPost]

src/apps/orders/Genocs.Orders.WebApi/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@
7575
.UseCertificateAuthentication()
7676
.UseEndpoints(r => r.MapControllers())
7777
.UseDispatcherEndpoints(endpoints => endpoints
78-
.Get(string.Empty, ctx => ctx.Response.WriteAsync("Orders Service"))
79-
.Get("ping", ctx => ctx.Response.WriteAsync("pong"))
8078
.Get<GetOrder, OrderDto>("orders/{orderId}")
8179
.Post<CreateOrder>("orders", afterDispatch: (cmd, ctx) => ctx.Response.Created($"orders/{cmd.OrderId}")))
8280
.UseSwaggerDocs()
8381
.UseRabbitMQ()
8482
.SubscribeEvent<DeliveryStarted>();
8583

84+
app.MapDefaultEndpoints();
85+
8686
app.Run();
8787

8888
Log.CloseAndFlush();

src/apps/products/Genocs.Products.WebApi/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@
7575
.UseCertificateAuthentication()
7676
.UseEndpoints(r => r.MapControllers())
7777
.UseDispatcherEndpoints(endpoints => endpoints
78-
.Get(string.Empty, ctx => ctx.Response.WriteAsync("Products Service"))
79-
.Get("ping", ctx => ctx.Response.WriteAsync("pong"))
8078
.Get<BrowseProducts, PagedResult<ProductDto>>("products")
8179
.Get<GetProduct, ProductDto>("products/{productId}")
8280
.Post<CreateProduct>("products", afterDispatch: (cmd, ctx) => ctx.Response.Created($"products/{cmd.ProductId}")))
8381
.UseSwaggerDocs()
8482
.UseRabbitMQ();
8583

84+
app.MapDefaultEndpoints();
85+
8686
app.Run();
8787

8888
Log.CloseAndFlush();

0 commit comments

Comments
 (0)