Skip to content

Instantly share code, notes, and snippets.

View explorer14's full-sized avatar

Aman Agrawal explorer14

View GitHub Profile
public interface IBreadSizeService
{
Task<int> BreadSizeRequiredForPizzaSize(
int pizzaDiameterInCm);
}
class Program
{
static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger();
var useCase = new CreatePizzaRecipe(
new BreadSizeService(),
new CheeseQuantityService(),
new TomatoQuantityService());
public class CreatePizzaRecipe
{
private readonly IBreadSizeService breadSizeService;
private readonly ICheeseQuantityService cheeseQuantityService;
private readonly ITomatoQuantityService tomatoQuantityService;
public CreatePizzaRecipe(IBreadSizeService breadSizeService,
ICheeseQuantityService cheeseQuantityService,
ITomatoQuantityService tomatoQuantityService)
{
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);
}
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddMvc(options =>
{
options.Filters.Add(
new ControllerActionProfilingFilter());
});
}
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()} " +
public void ConfigureServices(IServiceCollection services)
{
services.AddLibServices(
Configuration.GetValue("EnableLibraryProfiling", true));
//...other dependencies
}
internal static class ServiceCollectionExtensions
{
internal static IServiceCollection AddLibServices(
this IServiceCollection services, bool enableProfiling)
{
if (enableProfiling)
{
services.AddScoped(serviceProvider =>
{
var proxy = ProfilingProxy.CreateProxy(
{
//...other configs
"EnableLibraryProfiling": true
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton();
services.AddScoped(serviceProvider =>
{
var proxy = ProfilingProxy.CreateProxy(
new PeriodRepository(serviceProvider.GetService()),
serviceProvider.GetService());
return proxy;