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