Generator Changes at 9/27/2025 8:46:36 AM
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using Grpc.Core.Interceptors;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
|
||||
namespace CMSMicroservice.WebApi.Common.Behaviours;
|
||||
|
||||
public class LoggingBehaviour : Interceptor
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
public LoggingBehaviour(ILogger<LoggingBehaviour> logger, ICurrentUserService currentUserService)
|
||||
{
|
||||
_logger = logger;
|
||||
_currentUserService = currentUserService;
|
||||
}
|
||||
|
||||
public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
|
||||
TRequest request,
|
||||
ServerCallContext context,
|
||||
UnaryServerMethod<TRequest, TResponse> continuation)
|
||||
{
|
||||
var requestName = typeof(TRequest).Name;
|
||||
var userId = _currentUserService.UserId ?? string.Empty;
|
||||
_logger.LogInformation("gRPC Starting receiving call. Type/Method: {Type} / {Method} Request: {Name} {@UserId} {@Request}",
|
||||
MethodType.Unary, context.Method , requestName, userId, request);
|
||||
|
||||
try
|
||||
{
|
||||
return await continuation(request, context);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "gRPC Request: Unhandled Exception for Request {Name} {@Request}", requestName, request);
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Grpc.Core.Interceptors;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Diagnostics;
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
|
||||
namespace CMSMicroservice.WebApi.Common.Behaviours;
|
||||
|
||||
public class PerformanceBehaviour : Interceptor
|
||||
{
|
||||
private readonly Stopwatch _timer;
|
||||
private readonly ILogger _logger;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
public PerformanceBehaviour(ILogger<PerformanceBehaviour> logger, ICurrentUserService currentUserService)
|
||||
{
|
||||
_timer = new Stopwatch();
|
||||
_logger = logger;
|
||||
_currentUserService = currentUserService;
|
||||
}
|
||||
|
||||
public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
|
||||
TRequest request,
|
||||
ServerCallContext context,
|
||||
UnaryServerMethod<TRequest, TResponse> continuation)
|
||||
{
|
||||
_timer.Start();
|
||||
|
||||
var response = await continuation(request, context);
|
||||
|
||||
_timer.Stop();
|
||||
|
||||
var elapsedMilliseconds = _timer.ElapsedMilliseconds;
|
||||
|
||||
if (elapsedMilliseconds > 500)
|
||||
{
|
||||
var requestName = typeof(TRequest).Name;
|
||||
var userId = _currentUserService.UserId ?? string.Empty;
|
||||
|
||||
_logger.LogWarning("gRPC Long Running Request: {Name} ({ElapsedMilliseconds} milliseconds) {@UserId} {@Request}",
|
||||
requestName, elapsedMilliseconds, userId, request);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
64
src/CMSMicroservice.WebApi/Common/Mappings/GeneralMapping.cs
Normal file
64
src/CMSMicroservice.WebApi/Common/Mappings/GeneralMapping.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System.Globalization;
|
||||
namespace CMSMicroservice.WebApi.Common.Mappings;
|
||||
|
||||
public class GeneralMapping : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
config.NewConfig<string, decimal>()
|
||||
.MapWith(src => decimal.Parse(src));
|
||||
|
||||
config.NewConfig<decimal, string>()
|
||||
.MapWith(src => src.ToString("R", new CultureInfo("en-us")));
|
||||
|
||||
config.NewConfig<decimal?, string>()
|
||||
.MapWith(src => src == null ? string.Empty : src.Value.ToString("R", new CultureInfo("en-us")));
|
||||
|
||||
config.NewConfig<string, decimal?>()
|
||||
.MapWith(src => string.IsNullOrEmpty(src) ? null : decimal.Parse(src));
|
||||
|
||||
config.NewConfig<Guid, string>()
|
||||
.MapWith(src => src == Guid.Empty ? string.Empty : src.ToString());
|
||||
|
||||
config.NewConfig<string, Guid>()
|
||||
.MapWith(src => string.IsNullOrEmpty(src) ? Guid.Empty : Guid.Parse(src));
|
||||
|
||||
config.NewConfig<string, Guid?>()
|
||||
.MapWith(src => string.IsNullOrEmpty(src) ? null : Guid.Parse(src));
|
||||
|
||||
config.NewConfig<Timestamp, DateTime>()
|
||||
.MapWith(src => src.ToDateTime());
|
||||
|
||||
config.NewConfig<Timestamp, DateTime?>()
|
||||
.MapWith(src => src == null ? null : src.ToDateTime());
|
||||
|
||||
config.NewConfig<DateTime, Timestamp>()
|
||||
.MapWith(src => Timestamp.FromDateTime(DateTime.SpecifyKind(src, DateTimeKind.Utc)));
|
||||
|
||||
config.NewConfig<DateTime?, Timestamp>()
|
||||
.MapWith(src => src.HasValue ? Timestamp.FromDateTime(DateTime.SpecifyKind(src.Value, DateTimeKind.Utc)) : null);
|
||||
|
||||
config.NewConfig<Duration, TimeSpan>()
|
||||
.MapWith(src => src.ToTimeSpan());
|
||||
|
||||
config.NewConfig<Duration, TimeSpan?>()
|
||||
.MapWith(src => src == null ? null : src.ToTimeSpan());
|
||||
|
||||
config.NewConfig<TimeSpan, Duration>()
|
||||
.MapWith(src => Duration.FromTimeSpan(src));
|
||||
|
||||
config.NewConfig<TimeSpan?, Duration>()
|
||||
.MapWith(src => src.HasValue ? Duration.FromTimeSpan(src.Value) : null);
|
||||
|
||||
config.Default
|
||||
.UseDestinationValue(member => member.SetterModifier == AccessModifier.None &&
|
||||
member.Type.IsGenericType &&
|
||||
member.Type.GetGenericTypeDefinition() == typeof(Google.Protobuf.Collections.RepeatedField<>));
|
||||
|
||||
config.NewConfig<Google.Protobuf.ByteString, byte[]>()
|
||||
.MapWith(src => src.ToByteArray());
|
||||
|
||||
config.NewConfig<byte[], Google.Protobuf.ByteString>()
|
||||
.MapWith(src => Google.Protobuf.ByteString.CopyFrom(src));
|
||||
}
|
||||
}
|
||||
10
src/CMSMicroservice.WebApi/Common/Mappings/PackageProfile.cs
Normal file
10
src/CMSMicroservice.WebApi/Common/Mappings/PackageProfile.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.WebApi.Common.Mappings;
|
||||
|
||||
public class PackageProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
10
src/CMSMicroservice.WebApi/Common/Mappings/RoleProfile.cs
Normal file
10
src/CMSMicroservice.WebApi/Common/Mappings/RoleProfile.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.WebApi.Common.Mappings;
|
||||
|
||||
public class RoleProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.WebApi.Common.Mappings;
|
||||
|
||||
public class UserAddressProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.WebApi.Common.Mappings;
|
||||
|
||||
public class UserOrderProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
10
src/CMSMicroservice.WebApi/Common/Mappings/UserProfile.cs
Normal file
10
src/CMSMicroservice.WebApi/Common/Mappings/UserProfile.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.WebApi.Common.Mappings;
|
||||
|
||||
public class UserProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.WebApi.Common.Mappings;
|
||||
|
||||
public class UserRoleProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Security.Claims;
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace CMSMicroservice.WebApi.Common.Services;
|
||||
|
||||
public class CurrentUserService : ICurrentUserService
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public CurrentUserService(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public string? UserId => _httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
namespace CMSMicroservice.WebApi.Common.Services;
|
||||
public interface IDispatchRequestToCQRS
|
||||
{
|
||||
Task<TResponse> Handle<TRequest, TCommand, TResponse>(TRequest request,
|
||||
ServerCallContext context);
|
||||
Task<Empty> Handle<TRequest, TCommand>(TRequest request,
|
||||
ServerCallContext context);
|
||||
Task<TResponse> Handle<TCommand, TResponse>(ServerCallContext context);
|
||||
}
|
||||
public class DispatchRequestToCQRS : IDispatchRequestToCQRS
|
||||
{
|
||||
private readonly ISender _sender;
|
||||
|
||||
public DispatchRequestToCQRS(ISender sender)
|
||||
{
|
||||
_sender = sender;
|
||||
}
|
||||
|
||||
public async Task<TResponse> Handle<TRequest, TCommand, TResponse>(TRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (request is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(request));
|
||||
}
|
||||
|
||||
var cqrsInput = request.Adapt<TCommand>();
|
||||
|
||||
if (cqrsInput is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(cqrsInput));
|
||||
}
|
||||
|
||||
var output = await _sender.Send(cqrsInput, context.CancellationToken);
|
||||
return (output ?? throw new InvalidOperationException()).Adapt<TResponse>();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public async Task<TResponse> Handle<TCommand, TResponse>(ServerCallContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cqrsInput = Activator.CreateInstance<TCommand>();
|
||||
if (cqrsInput is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(cqrsInput));
|
||||
}
|
||||
|
||||
var output = await _sender.Send(cqrsInput, context.CancellationToken);
|
||||
return (output ?? throw new InvalidOperationException()).Adapt<TResponse>();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public async Task<Empty> Handle<TRequest, TCommand>(TRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (request is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(request));
|
||||
}
|
||||
|
||||
var cqrsInput = request.Adapt<TCommand>();
|
||||
|
||||
if (cqrsInput is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(cqrsInput));
|
||||
}
|
||||
|
||||
await _sender.Send(cqrsInput, context.CancellationToken);
|
||||
return new Empty();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user