Created
March 9, 2021 19:49
-
-
Save danielplawgo/9b0d2239136fbe7d0afd8f13a8a8e8ed to your computer and use it in GitHub Desktop.
Scrutor użycie dekoratora
This file contains hidden or 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 class DecoratorAttribute : Attribute | |
{ | |
} |
This file contains hidden or 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 interface IProductRepository : IRepository | |
{ | |
Product GetById(Guid id); | |
} | |
public class ProductRepository : IProductRepository | |
{ | |
public Product GetById(Guid id) | |
{ | |
return new Product() | |
{ | |
Id = id | |
}; | |
} | |
} |
This file contains hidden or 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 class ProductRepositoryLoggerDecorator : IProductRepository | |
{ | |
private readonly IProductRepository _productRepository; | |
private readonly ILogger<ProductRepositoryLoggerDecorator> _logger; | |
public ProductRepositoryLoggerDecorator(IProductRepository productRepository, ILogger<ProductRepositoryLoggerDecorator> logger) | |
{ | |
_productRepository = productRepository; | |
_logger = logger; | |
} | |
public Product GetById(Guid id) | |
{ | |
var repositoryType = _productRepository.GetType(); | |
_logger.LogInformation($"Executing {repositoryType.Namespace} GetById - id: {id}"); | |
var product = _productRepository.GetById(id); | |
_logger.LogInformation($"Executed {repositoryType.Namespace} GetById - id: {id}"); | |
return product; | |
} | |
} |
This file contains hidden or 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
[Decorator] | |
public class ProductRepositoryLoggerDecorator : IProductRepository | |
{ | |
.... | |
} |
This file contains hidden or 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 class ServiceCollectionExtensions | |
{ | |
public static IServiceCollection AddRepositories(this IServiceCollection services) | |
{ | |
services.AddScoped<IProductRepository, ProductRepository>(); | |
services.TryDecorate<IProductRepository, ProductRepositoryLoggerDecorator>(); | |
return services; | |
} | |
} |
This file contains hidden or 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 class ServiceCollectionExtensions | |
{ | |
public static IServiceCollection AddRepositories(this IServiceCollection services) | |
{ | |
services.Scan(s => s.FromAssemblyOf<IRepository>() | |
.AddClasses(c => c.AssignableTo<IRepository>().WithoutAttribute<DecoratorAttribute>()) | |
.AsImplementedInterfaces() | |
.WithScopedLifetime()); | |
services.TryDecorate<IProductRepository, ProductRepositoryLoggerDecorator>(); | |
return services; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment