This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services | |
.AddLogging(configure => | |
{ | |
configure.AddConsole(); | |
configure.SetMinimumLevel(LogLevel.Information); | |
}) | |
.AddSingleton<IImportantDataProvider, ImportantDataProvider>() | |
.AddSingleton<IExampleService, ExampleService>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static class ServiceCollectionsExtensions | |
{ | |
public static IServiceCollection DecorateWithDispatchProxy<TInterface, TProxy>(this IServiceCollection services) | |
where TInterface : class | |
where TProxy : DispatchProxy | |
{ | |
MethodInfo createMethod; | |
try | |
{ | |
createMethod = typeof(TProxy) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class DispatchProxyLoggingDecorator<TDecorated> : DispatchProxy | |
{ | |
private TDecorated _decorated; | |
private ILogger<DispatchProxyLoggingDecorator<TDecorated>> _logger; | |
protected override object Invoke(MethodInfo targetMethod, object[] args) | |
{ | |
try | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class DispatchProxyMinimalLoggingDecorator<TDecorated> : DispatchProxy | |
{ | |
private TDecorated _decorated; | |
private ILogger<DispatchProxyLoggingDecorator<TDecorated>> _logger; | |
protected override object Invoke(MethodInfo targetMethod, object[] args) | |
{ | |
try | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class DispatchProxyEmptyDecorator<TDecorated> : DispatchProxy | |
{ | |
private TDecorated _decorated; | |
protected override object Invoke(MethodInfo targetMethod, object[] args) | |
{ | |
var result = targetMethod.Invoke(_decorated, args); | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services | |
.AddLogging(configure => | |
{ | |
configure.AddConsole(); | |
configure.SetMinimumLevel(LogLevel.Information); | |
}) | |
.AddSingleton<IImportantDataProvider, ImportantDataProvider>() | |
.AddSingleton<IExampleService, ExampleService>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ConfigureServices(IServiceCollection services) | |
{ | |
services | |
.AddLogging(configure => | |
{ | |
configure.AddConsole(); | |
configure.SetMinimumLevel(LogLevel.Information); | |
}) | |
.AddSingleton<IImportantDataProvider, ImportantDataProvider>() | |
.AddSingleton<IExampleService>(sp => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IServiceCollection Decorate<TInterface, TDecorator>(this IServiceCollection services) | |
where TInterface : class | |
where TDecorator : class, TInterface | |
{ | |
var objectFactory = ActivatorUtilities.CreateFactory( | |
typeof(TDecorator), | |
new[] { typeof(TInterface) }); | |
// Save all descriptors that needs to be decorated into a list. | |
var descriptorsToDecorate = services |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal sealed class ExampleServiceClassicDecorator : IExampleService | |
{ | |
private readonly IExampleService _decorated; | |
private readonly ILogger<ExampleServiceClassicDecorator> _logger; | |
public ExampleServiceClassicDecorator(IExampleService decorated, | |
ILogger<ExampleServiceClassicDecorator> logger) | |
{ | |
_decorated = decorated; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal interface IImportantDataProvider | |
{ | |
TimeSpan GetTimeToWait(); | |
} | |
internal sealed class ImportantDataProvider : IImportantDataProvider | |
{ | |
/// <inheritdoc /> | |
public TimeSpan GetTimeToWait() => TimeSpan.FromSeconds(2); | |
} |
NewerOlder