Skip to content

Instantly share code, notes, and snippets.

@couellet
Last active December 14, 2015 11:49
Show Gist options
  • Save couellet/5082131 to your computer and use it in GitHub Desktop.
Save couellet/5082131 to your computer and use it in GitHub Desktop.
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