Skip to content

Instantly share code, notes, and snippets.

@khellang
Created September 15, 2016 09:07
Show Gist options
  • Save khellang/c99a80c3f0e7ca3ec00efa4973a41c66 to your computer and use it in GitHub Desktop.
Save khellang/c99a80c3f0e7ca3ec00efa4973a41c66 to your computer and use it in GitHub Desktop.
[Route("customers")]
public class CustomerController
{
public CustomerController(DbContext context)
{
Context = context;
}
private DbContext Context { get; }
[HttpGet("{id}")]
public async Task<IActionResult> Get(int id, CancellationToken cancellationToken)
{
var customer = await Context.Customers.FindAsync(id, cancellationToken);
if (customer == null)
{
return new NotFoundResult();
}
return new ObjectResult(customer);
}
}
public class CustomerMapper : IEntityMapper<Customer>
{
public object Map(Customer customer, int? version)
{
if (version.HasValue)
{
switch (version.Value)
{
case 2: return new CustomerModelV2(customer);
case 3: return new CustomerModelV3(customer);
}
}
return new CustomerModelV1(customer);
}
}
public class VersioningStrategy : IVersioningStrategy
{
public Task<int?> GetVersion(HttpContext context)
{
var accept = context.Request.Headers["Accept"];
// Parse out the version from Accept header. Examples:
// - application/json -> 1
// - application/json; version=1 -> 1
// - application/vnd.my-app.v2+json -> 2
// - application/vnd.my-app+json; version=3 -> 3
return Task.FromResult<int?>(2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment