first commit

This commit is contained in:
2026-02-26 14:04:18 +07:00
parent 57ac80a666
commit 4b7236493f
92 changed files with 4999 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
using System.Diagnostics;
using MediatR;
using Microsoft.Extensions.Logging;
namespace MyNewProjectName.Application.Behaviors;
/// <summary>
/// Logging behavior for MediatR pipeline
/// </summary>
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : notnull
{
private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger;
public LoggingBehavior(ILogger<LoggingBehavior<TRequest, TResponse>> logger)
{
_logger = logger;
}
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
var requestName = typeof(TRequest).Name;
_logger.LogInformation("Handling {RequestName}", requestName);
var stopwatch = Stopwatch.StartNew();
try
{
var response = await next();
stopwatch.Stop();
_logger.LogInformation("Handled {RequestName} in {ElapsedMilliseconds}ms",
requestName, stopwatch.ElapsedMilliseconds);
return response;
}
catch (Exception ex)
{
stopwatch.Stop();
_logger.LogError(ex, "Error handling {RequestName} after {ElapsedMilliseconds}ms",
requestName, stopwatch.ElapsedMilliseconds);
throw;
}
}
}

View File

@@ -0,0 +1,47 @@
using FluentValidation;
using MediatR;
namespace MyNewProjectName.Application.Behaviors;
/// <summary>
/// Validation behavior for MediatR pipeline
/// </summary>
public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : notnull
{
private readonly IEnumerable<IValidator<TRequest>> _validators;
public ValidationBehavior(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
if (!_validators.Any())
{
return await next();
}
var context = new ValidationContext<TRequest>(request);
var validationResults = await Task.WhenAll(
_validators.Select(v => v.ValidateAsync(context, cancellationToken)));
var failures = validationResults
.Where(r => r.Errors.Any())
.SelectMany(r => r.Errors)
.ToList();
if (failures.Any())
{
var errors = failures
.GroupBy(e => e.PropertyName, e => e.ErrorMessage)
.ToDictionary(g => g.Key, g => g.ToArray());
throw new MyNewProjectName.Domain.Exceptions.ValidationException(errors);
}
return await next();
}
}

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using FluentValidation;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using MyNewProjectName.Application.Behaviors;
namespace MyNewProjectName.Application;
/// <summary>
/// Dependency Injection for Application Layer
/// </summary>
public static class DependencyInjection
{
public static IServiceCollection AddApplication(this IServiceCollection services)
{
var assembly = Assembly.GetExecutingAssembly();
// Register AutoMapper
services.AddAutoMapper(assembly);
// Register MediatR
services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssembly(assembly);
// Logging phải đứng đầu tiên để ghi nhận request đến
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
// Sau đó mới đến Validation
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
});
// Register FluentValidation validators
services.AddValidatorsFromAssembly(assembly);
return services;
}
}

View File

@@ -0,0 +1,8 @@
using MediatR;
namespace MyNewProjectName.Application.Features.Sample.Commands.CreateSample;
/// <summary>
/// Command to create a new sample
/// </summary>
public record CreateSampleCommand(string Name, string? Description) : IRequest<Guid>;

View File

@@ -0,0 +1,37 @@
using MyNewProjectName.Domain.Entities;
using MyNewProjectName.Domain.Interfaces;
using MediatR;
namespace MyNewProjectName.Application.Features.Sample.Commands.CreateSample;
/// <summary>
/// Handler for CreateSampleCommand
/// </summary>
public class CreateSampleCommandHandler : IRequestHandler<CreateSampleCommand, Guid>
{
private readonly IRepository<SampleEntity> _repository;
private readonly IUnitOfWork _unitOfWork;
public CreateSampleCommandHandler(IRepository<SampleEntity> repository, IUnitOfWork unitOfWork)
{
_repository = repository;
_unitOfWork = unitOfWork;
}
public async Task<Guid> Handle(CreateSampleCommand request, CancellationToken cancellationToken)
{
var entity = new SampleEntity
{
Id = Guid.NewGuid(),
Name = request.Name,
Description = request.Description,
IsActive = true,
CreatedAt = DateTime.UtcNow
};
await _repository.AddAsync(entity, cancellationToken);
await _unitOfWork.SaveChangesAsync(cancellationToken);
return entity.Id;
}
}

View File

@@ -0,0 +1,19 @@
using FluentValidation;
namespace MyNewProjectName.Application.Features.Sample.Commands.CreateSample;
/// <summary>
/// Validator for CreateSampleCommand
/// </summary>
public class CreateSampleCommandValidator : AbstractValidator<CreateSampleCommand>
{
public CreateSampleCommandValidator()
{
RuleFor(x => x.Name)
.NotEmpty().WithMessage("Name is required.")
.MaximumLength(200).WithMessage("Name must not exceed 200 characters.");
RuleFor(x => x.Description)
.MaximumLength(1000).WithMessage("Description must not exceed 1000 characters.");
}
}

View File

@@ -0,0 +1,8 @@
using MediatR;
namespace MyNewProjectName.Application.Features.Sample.Queries.GetSamples;
/// <summary>
/// Query to get all samples
/// </summary>
public record GetSamplesQuery : IRequest<List<SampleDto>>;

View File

@@ -0,0 +1,27 @@
using AutoMapper;
using MyNewProjectName.Domain.Entities;
using MyNewProjectName.Domain.Interfaces;
using MediatR;
namespace MyNewProjectName.Application.Features.Sample.Queries.GetSamples;
/// <summary>
/// Handler for GetSamplesQuery
/// </summary>
public class GetSamplesQueryHandler : IRequestHandler<GetSamplesQuery, List<SampleDto>>
{
private readonly IRepository<SampleEntity> _repository;
private readonly IMapper _mapper;
public GetSamplesQueryHandler(IRepository<SampleEntity> repository, IMapper mapper)
{
_repository = repository;
_mapper = mapper;
}
public async Task<List<SampleDto>> Handle(GetSamplesQuery request, CancellationToken cancellationToken)
{
var entities = await _repository.GetAllAsync(cancellationToken);
return _mapper.Map<List<SampleDto>>(entities);
}
}

View File

@@ -0,0 +1,22 @@
using AutoMapper;
using MyNewProjectName.Application.Mappings;
using MyNewProjectName.Domain.Entities;
namespace MyNewProjectName.Application.Features.Sample.Queries.GetSamples;
/// <summary>
/// Sample DTO
/// </summary>
public class SampleDto : IMapFrom<SampleEntity>
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public bool IsActive { get; set; }
public DateTime CreatedAt { get; set; }
public void Mapping(Profile profile)
{
profile.CreateMap<SampleEntity, SampleDto>();
}
}

View File

@@ -0,0 +1,14 @@
// using MyNewProjectName.Domain.Entities;
// using System.Security.Claims;
// namespace MyNewProjectName.Application.Interfaces.Common;
// public interface IJwtTokenGenerator
// {
// string GenerateAccessToken(User user, List<string> roles, Guid tenantId);
// string GenerateRefreshToken();
// ClaimsPrincipal GetPrincipalFromExpiredToken(string token);
// }

View File

@@ -0,0 +1,8 @@
namespace MyNewProjectName.Application.Interfaces.Common;
public interface IPasswordHasher
{
string Hash(string password);
bool Verify(string password, string hashedPassword);
}

View File

@@ -0,0 +1,12 @@
namespace MyNewProjectName.Application.Interfaces;
/// <summary>
/// Interface for getting current user information
/// </summary>
public interface ICurrentUserService
{
string? UserId { get; }
string? UserName { get; }
bool? IsAuthenticated { get; }
string? Role { get; }
}

View File

@@ -0,0 +1,10 @@
namespace MyNewProjectName.Application.Interfaces;
/// <summary>
/// Interface for date time operations (for testability)
/// </summary>
public interface IDateTimeService
{
DateTime Now { get; }
DateTime UtcNow { get; }
}

View File

@@ -0,0 +1,11 @@
using AutoMapper;
namespace MyNewProjectName.Application.Mappings;
/// <summary>
/// Interface for auto mapping registration
/// </summary>
public interface IMapFrom<T>
{
void Mapping(Profile profile) => profile.CreateMap(typeof(T), GetType());
}

View File

@@ -0,0 +1,55 @@
using System.Reflection;
using AutoMapper;
namespace MyNewProjectName.Application.Mappings;
/// <summary>
/// AutoMapper profile that auto-registers mappings from IMapFrom interface
/// </summary>
public class MappingProfile : Profile
{
public MappingProfile()
{
ApplyMappingsFromAssembly(Assembly.GetExecutingAssembly());
}
private void ApplyMappingsFromAssembly(Assembly assembly)
{
var mapFromType = typeof(IMapFrom<>);
var mappingMethodName = nameof(IMapFrom<object>.Mapping);
bool HasInterface(Type t) => t.IsGenericType && t.GetGenericTypeDefinition() == mapFromType;
var types = assembly.GetExportedTypes()
.Where(t => t.GetInterfaces().Any(HasInterface))
.ToList();
var argumentTypes = new Type[] { typeof(Profile) };
foreach (var type in types)
{
var instance = Activator.CreateInstance(type);
var methodInfo = type.GetMethod(mappingMethodName);
if (methodInfo != null)
{
methodInfo.Invoke(instance, new object[] { this });
}
else
{
var interfaces = type.GetInterfaces().Where(HasInterface).ToList();
if (interfaces.Count > 0)
{
foreach (var @interface in interfaces)
{
var interfaceMethodInfo = @interface.GetMethod(mappingMethodName, argumentTypes);
interfaceMethodInfo?.Invoke(instance, new object[] { this });
}
}
}
}
}
}

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\MyNewProjectName.Domain\MyNewProjectName.Domain.csproj" />
<ProjectReference Include="..\MyNewProjectName.Contracts\MyNewProjectName.Contracts.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.11.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
</ItemGroup>
</Project>