Created
February 18, 2016 19:48
-
-
Save RobSeder/1bc2609c3569951f815f to your computer and use it in GitHub Desktop.
In response to questions from this blog post: https://blog.robseder.com/2016/01/29/new-github-and-nuget-package-division42-data-repository-entityframework/#comments
This file contains 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 Customer | |
{ | |
[Key] | |
public Guid CustomerId { get; set; } | |
public String FirstName { get; set; } | |
public String LastName { get; set; } | |
} |
This file contains 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 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); | |
} | |
} | |
} |
This file contains 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
// 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