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 IBreadSizeService | |
| { | |
| Task<int> BreadSizeRequiredForPizzaSize( | |
| int pizzaDiameterInCm); | |
| } |
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
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger(); | |
| var useCase = new CreatePizzaRecipe( | |
| new BreadSizeService(), | |
| new CheeseQuantityService(), | |
| new TomatoQuantityService()); |
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 CreatePizzaRecipe | |
| { | |
| private readonly IBreadSizeService breadSizeService; | |
| private readonly ICheeseQuantityService cheeseQuantityService; | |
| private readonly ITomatoQuantityService tomatoQuantityService; | |
| public CreatePizzaRecipe(IBreadSizeService breadSizeService, | |
| ICheeseQuantityService cheeseQuantityService, | |
| ITomatoQuantityService tomatoQuantityService) | |
| { |
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 BreadSizeService | |
| : IBreadSizeService | |
| { | |
| public async Task<int> BreadSizeRequiredForPizzaSize( | |
| int pizzaDiameterInCm) | |
| { | |
| Log.Logger.Information("Retrieving bread size for {size} cm pizza...", | |
| pizzaDiameterInCm); | |
| return await Task.FromResult(3); | |
| } |
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 void ConfigureServices(IServiceCollection services) | |
| { | |
| //... | |
| services.AddMvc(options => | |
| { | |
| options.Filters.Add( | |
| new ControllerActionProfilingFilter()); | |
| }); | |
| } |
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 ControllerActionProfilingFilter : IActionFilter | |
| { | |
| private Stopwatch stopwatch; | |
| public void OnActionExecuted(ActionExecutedContext context) | |
| { | |
| stopwatch.Stop(); | |
| Log.Logger.Information( | |
| $"Action {context.ActionDescriptor.DisplayName} " + | |
| $"executed in {stopwatch.Elapsed.ToString()} " + |
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 void ConfigureServices(IServiceCollection services) | |
| { | |
| services.AddLibServices( | |
| Configuration.GetValue("EnableLibraryProfiling", true)); | |
| //...other dependencies | |
| } |
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
| internal static class ServiceCollectionExtensions | |
| { | |
| internal static IServiceCollection AddLibServices( | |
| this IServiceCollection services, bool enableProfiling) | |
| { | |
| if (enableProfiling) | |
| { | |
| services.AddScoped(serviceProvider => | |
| { | |
| var proxy = ProfilingProxy.CreateProxy( |
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
| { | |
| //...other configs | |
| "EnableLibraryProfiling": true | |
| } |
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 void ConfigureServices(IServiceCollection services) | |
| { | |
| services.AddSingleton(); | |
| services.AddScoped(serviceProvider => | |
| { | |
| var proxy = ProfilingProxy.CreateProxy( | |
| new PeriodRepository(serviceProvider.GetService()), | |
| serviceProvider.GetService()); | |
| return proxy; |