Skip to content

Instantly share code, notes, and snippets.

@RobSeder
Created February 18, 2016 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobSeder/1bc2609c3569951f815f to your computer and use it in GitHub Desktop.
Save RobSeder/1bc2609c3569951f815f to your computer and use it in GitHub Desktop.
public class Customer
{
[Key]
public Guid CustomerId { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
}
public class CustomerController : ApiController
{
/// <summary>
/// Creates a new instance of this type. (Production use)
/// </summary>
public CustomerController() : this(new CustomerRepository())
{
}
/// <summary>
/// Creates a new instance of this type. (Unit testing use)
/// </summary>
/// <param name="repository"></param>
public CustomerController(IRepository<Customer> repository)
{
Repository = repository;
if (repository == null)
throw new ArgumentNullException("repository");
}
public IRepository<Customer> Repository { get; private set; }
public IHttpActionResult Get()
{
try
{
// I have NO IDEA if this is a real database repository
// or an in-memory one from unit testing.
IEnumerable<Customer> model = Repository.GetAll();
return Ok(model);
}
catch (Exception exception)
{
return InternalServerError(exception);
}
}
}
// using Division42.Data.Repository.EntityFramework;
public class CustomerRepository :
EntityFrameworkRepositoryBase<Customer>
{
/// <summary>
/// Creates a new instance of this type. (Production use)
/// </summary>
public CustomerRepository() : this(new PointOfSaleContext())
{
}
/// <summary>
/// Creates a new instance of this type. (Unit testing use)
/// </summary>
/// <param name="context"></param>
public CustomerRepository(DbContext context) : base(context)
{
}
public override Customer GetById(Guid id, params string[] entityIncludes)
{
// using System.Data.Entity.Infrastructure;
DbQuery<Customer> query = this.Entities;
if (entityIncludes != null)
{
foreach (String entityInclude in entityIncludes)
{
query = query.Include(entityInclude);
}
}
return query.FirstOrDefault(item => item.CustomerId == id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment