Skip to content

Commit

Permalink
Changed tags to Customer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Wildenberg committed Apr 17, 2018
1 parent 99c9cb1 commit 4d1f2e6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ public CustomerController(ICustomerService customerService, CorrelationContainer
}

[HttpGet]
[Route("tags")]
public async Task<IEnumerable<string>> Get(int customerId)
public async Task<Customer> Get(int id)
{
// Set async local correlation id.
var correlationId = Guid.NewGuid();
_correlationContainer.SetCorrelationId(correlationId);

// Call controller dependency (decorated by LoggingDecorator).
var tags = await _customerService.GetCustomerTags(customerId);
var customer = await _customerService.GetCustomer(id);

// Return values.
return tags;
return customer;
}
}
}
4 changes: 2 additions & 2 deletions AsyncLocal.SimpleInjector.Web/Controllers/CustomerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public CustomerService(CorrelationContainer correlationContainer)
_correlationContainer = correlationContainer;
}

public Task<IEnumerable<string>> GetCustomerTags(int customerId)
public Task<Customer> GetCustomer(int customerId)
{
return Task.FromResult(new[] { "elm", "is", "cool" }.AsEnumerable());
return Task.FromResult(new Customer());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ namespace AsyncLocal.SimpleInjector.Web.Controllers
{
public interface ICustomerService
{
Task<IEnumerable<string>> GetCustomerTags(int customerId);
Task<Customer> GetCustomer(int customerId);
}

public class Customer
{
public string Name { get; set; } = "John Doe";

public int Age { get; set; } = 42;
}
}
16 changes: 10 additions & 6 deletions AsyncLocal.SimpleInjector.Web/Controllers/LoggingDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ public LoggingDecorator(
_correlationContainer = correlationContainer;
}

public async Task<IEnumerable<string>> GetCustomerTags(int customerId)
public async Task<Customer> GetCustomer(int customerId)
{
// Get async local correlation id.
var correlationId = _correlationContainer.GetCorrelationId();
_logger.LogWarning($"Getting customer tags for {customerId} ({correlationId})");
// Get async local correlation id and log.
var correlationIdBeforeAwait = _correlationContainer.GetCorrelationId();
_logger.LogWarning($"Getting customer by id {customerId} ({correlationIdBeforeAwait})");

// Call decoratee.
var decoratee = _decorateeFunc.Invoke();
var values = await decoratee.GetCustomerTags(customerId);
var customer = await decoratee.GetCustomer(customerId);

// For demo purposes: get correlation id again after await and log.
var correlationIdAfterAwait = _correlationContainer.GetCorrelationId();
_logger.LogWarning($"Retrieved customer by id {customerId} ({correlationIdBeforeAwait})");

// Return values.
return values;
return customer;
}
}
}

0 comments on commit 4d1f2e6

Please sign in to comment.