Adds Decorator, wip
This commit is contained in:
parent
a86a2d7ade
commit
49f9b99ee1
9 changed files with 112 additions and 45 deletions
|
|
@ -5,9 +5,9 @@ namespace PlanTempus.Components;
|
|||
|
||||
public class CommandHandler(IComponentContext context) : ICommandHandler
|
||||
{
|
||||
public Task<TCommandResult> Handle<TCommand, TCommandResult>(TCommand command)
|
||||
public async Task<TCommandResult> Handle<TCommand, TCommandResult>(TCommand command)
|
||||
{
|
||||
var handler = context.Resolve<ICommandHandler<TCommand, TCommandResult>>();
|
||||
return handler.Handle(command);
|
||||
return await handler.Handle(command);
|
||||
}
|
||||
}
|
||||
15
PlanTempus.Components/ICommandHandler.cs
Normal file
15
PlanTempus.Components/ICommandHandler.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
namespace PlanTempus.Components;
|
||||
|
||||
public interface ICommandHandler
|
||||
{
|
||||
Task<TCommandResult> Handle<TCommand, TCommandResult>(TCommand command);
|
||||
}
|
||||
|
||||
public interface ICommandHandler<in T, TResult>
|
||||
{
|
||||
Task<TResult> Handle(T input);
|
||||
}
|
||||
|
||||
public interface ICommandHandlerDecorator
|
||||
{
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
using Autofac;
|
||||
using PlanTempus.Components.Users.Create;
|
||||
using PlanTempus.Core.SeqLogging;
|
||||
|
||||
namespace PlanTempus.Components.ModuleRegistry
|
||||
|
|
@ -9,15 +10,34 @@ namespace PlanTempus.Components.ModuleRegistry
|
|||
|
||||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
// Registrer alle handlers
|
||||
builder.RegisterAssemblyTypes()
|
||||
.AsClosedTypesOf(typeof(ICommandHandler<>))
|
||||
.InstancePerLifetimeScope();
|
||||
|
||||
// Registrer en decorator for alle ICommandHandler<TCommand>
|
||||
builder.RegisterGenericDecorator(
|
||||
typeof(CreateUserHandlerDecorator<>), // Din decorator-klasse
|
||||
typeof(ICommandHandler<>)); // Interface, der skal dekoreres
|
||||
builder.RegisterType<PlanTempus.Components.CommandHandler>()
|
||||
.As<PlanTempus.Components.ICommandHandler>()
|
||||
.InstancePerLifetimeScope();
|
||||
|
||||
builder.RegisterAssemblyTypes(ThisAssembly)
|
||||
.Where(t => !typeof(ICommandHandlerDecorator).IsAssignableFrom(t))
|
||||
.AsClosedTypesOf(typeof(ICommandHandler<,>))
|
||||
.InstancePerLifetimeScope();
|
||||
|
||||
|
||||
// Registrer alle handlers
|
||||
// builder.RegisterAssemblyTypes(ThisAssembly)
|
||||
// .AsClosedTypesOf(typeof(ICommandHandler<,>))
|
||||
// .InstancePerLifetimeScope();
|
||||
|
||||
builder.RegisterAssemblyTypes(ThisAssembly)
|
||||
.As<ICommandHandlerDecorator>();
|
||||
|
||||
|
||||
builder.RegisterDecorator(
|
||||
typeof(CreateUserHandlerDecorator),
|
||||
typeof(ICommandHandler<CreateUserCommand, CreateUserResult>));
|
||||
//
|
||||
// Registrer en decorator for alle ICommandHandler<TCommand>
|
||||
// builder.RegisterGenericDecorator(
|
||||
// typeof(CommandHandlerDecorator<,>),
|
||||
// typeof(ICommandHandler<,>));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,16 +8,6 @@ using PlanTempus.Core.Telemetry;
|
|||
|
||||
namespace PlanTempus.Components.Users.Create
|
||||
{
|
||||
public interface ICommandHandler
|
||||
{
|
||||
Task<TCommandResult> Handle<TCommand, TCommandResult>(TCommand command );
|
||||
}
|
||||
|
||||
public interface ICommandHandler<in T, TResult>
|
||||
{
|
||||
Task<TResult> Handle(T input);
|
||||
}
|
||||
|
||||
public class CreateUserHandler(
|
||||
TelemetryClient telemetryClient,
|
||||
IDatabaseOperations databaseOperations,
|
||||
|
|
@ -50,7 +40,6 @@ namespace PlanTempus.Components.Users.Create
|
|||
|
||||
var result = data.First();
|
||||
|
||||
telemetryClient.TrackTrace(GetType().Name, result.Format());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,41 @@
|
|||
using Microsoft.ApplicationInsights;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
|
||||
namespace PlanTempus.Components.Users.Create;
|
||||
|
||||
public class CreateUserHandlerDecorator : ICommandHandler<CreateUserCommand, CreateUserResult>
|
||||
public class CreateUserHandlerDecorator(
|
||||
ICommandHandler<CreateUserCommand, CreateUserResult> decoratedHandler,
|
||||
TelemetryClient telemetryClient)
|
||||
: ICommandHandler<CreateUserCommand, CreateUserResult>, ICommandHandlerDecorator
|
||||
{
|
||||
private readonly ICommandHandler<CreateUserCommand, CreateUserResult> _decoratedHandler;
|
||||
private readonly TelemetryClient _telemetryClient;
|
||||
|
||||
public CreateUserHandlerDecorator(
|
||||
ICommandHandler<CreateUserCommand, CreateUserResult> decoratedHandler,
|
||||
TelemetryClient telemetryClient)
|
||||
{
|
||||
_decoratedHandler = decoratedHandler;
|
||||
_telemetryClient = telemetryClient;
|
||||
}
|
||||
|
||||
public async Task<CreateUserResult> Handle(CreateUserCommand command)
|
||||
{
|
||||
_telemetryClient.TrackTrace($"Before handling {nameof(CreateUserCommand)}");
|
||||
var result = await _decoratedHandler.Handle(command);
|
||||
_telemetryClient.TrackTrace($"After handling {nameof(CreateUserCommand)}");
|
||||
return result;
|
||||
// var correlationId = Activity.Current?.RootId ?? command.CorrelationId;
|
||||
|
||||
using (var operation =
|
||||
telemetryClient.StartOperation<RequestTelemetry>($"Handle {nameof(CreateUserCommand)}",
|
||||
command.CorrelationId.ToString()))
|
||||
{
|
||||
try
|
||||
{
|
||||
operation.Telemetry.Properties["CorrelationId"] = command.CorrelationId.ToString();
|
||||
var result = await decoratedHandler.Handle(command);
|
||||
|
||||
operation.Telemetry.Success = true;
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
operation.Telemetry.Success = false;
|
||||
|
||||
telemetryClient.TrackException(ex, new Dictionary<string, string>
|
||||
{
|
||||
["CommandType"] = nameof(CreateUserCommand)
|
||||
});
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue