Files
pfm/PostFundManagement.Application/Extensions/ServiceExtensionTools.cs
T
2026-10-04 14:31:06 +02:00

79 lines
3.0 KiB
C#

using PostFundManagement.Application.Interceptors;
namespace PostFundManagement.Application.Extensions;
public static class ServiceExtensionTools
{
public static IServiceCollection AddLoggedCachedServiceScoped<TInterface, TImplementation, TCacheDecorator>(this IServiceCollection services)
where TInterface : class
where TImplementation : class, TInterface
where TCacheDecorator : class, TInterface
{
services.AddScoped<TImplementation>();
services.AddScoped<TCacheDecorator>();
services.AddScoped(provider =>
{
var proxyGenerator = provider.GetRequiredService<ProxyGenerator>();
var loggingInterceptor = provider.GetRequiredService<LoggingInterceptor>();
var actual = provider.GetRequiredService<TImplementation>();
var cached = ActivatorUtilities.CreateInstance<TCacheDecorator>(provider, actual);
return proxyGenerator.CreateInterfaceProxyWithTarget<TInterface>(cached,
loggingInterceptor.ToInterceptor());
});
return services;
}
public static IServiceCollection AddLoggedAuditedCachedServiceScoped<TInterface, TImplementation, TCacheDecorator>(this IServiceCollection services)
where TInterface : class
where TImplementation : class, TInterface
where TCacheDecorator : class, TInterface
{
services.AddScoped<TImplementation>();
services.AddScoped<TCacheDecorator>();
services.AddScoped(provider =>
{
var proxyGenerator = provider.GetRequiredService<ProxyGenerator>();
var auditInterceptor = provider.GetRequiredService<AuditInterceptor>();
var loggingInterceptor = provider.GetRequiredService<LoggingInterceptor>();
var actual = provider.GetRequiredService<TImplementation>();
var cached = ActivatorUtilities.CreateInstance<TCacheDecorator>(provider, actual);
return proxyGenerator.CreateInterfaceProxyWithTarget<TInterface>(cached,
loggingInterceptor.ToInterceptor(),
auditInterceptor.ToInterceptor());
});
return services;
}
public static IServiceCollection AddLoggedAuditedServiceScoped<TInterface, TImplementation>(this IServiceCollection services)
where TInterface : class
where TImplementation : class, TInterface
{
services.AddScoped<TImplementation>();
services.AddScoped(provider =>
{
var proxyGenerator = provider.GetRequiredService<ProxyGenerator>();
var auditInterceptor = provider.GetRequiredService<AuditInterceptor>();
var loggingInterceptor = provider.GetRequiredService<LoggingInterceptor>();
var actual = provider.GetRequiredService<TImplementation>();
return proxyGenerator.CreateInterfaceProxyWithTarget<TInterface>(actual,
loggingInterceptor.ToInterceptor(),
auditInterceptor.ToInterceptor());
});
return services;
}
}