Last active
December 14, 2015 11:49
-
-
Save couellet/5082131 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 abstract class Entity | |
{ | |
public int Id {get;set;} | |
} | |
public class EntityFrameworkSession : ISession | |
{ | |
private readonly DbContext context; | |
public EntityFrameworkSession(DbContext context) | |
{ | |
this.context = context; | |
} | |
public T SingleOrDefault<T>(int id) where T : Entity | |
{ | |
return context.Set<T>().SingleOrDefault(x => x.Id == id); | |
} | |
public T FirstOrDefault<T>(Func<T, bool> predicate) where T : Entity | |
{ | |
return context.Set<T>().FirstOrDefault(predicate); | |
} | |
public IQueryable<T> All<T>() where T : Entity | |
{ | |
return context.Set<T>().AsQueryable() | |
} | |
public void Add<T>(T entity) where T : Entity | |
{ | |
context.Set<T>().Add(entity); | |
} | |
public void Commit() | |
{ | |
return context.SaveChanges() | |
} | |
public void Delete<T>(T entity) where T : Entity | |
{ | |
context.Set<T>().Remove(entity); | |
} | |
} | |
public class Customer : Entity | |
{ | |
public string Name {get;set;} | |
} | |
public class CustomerController : ApiController | |
{ | |
private readonly ISession session; | |
public CustomerController(ISession session) | |
{ | |
this.session = session; | |
} | |
public Customer GetById(int id) | |
{ | |
return session.SingleOrDefault<Customer>(id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment