Skip to content

Instantly share code, notes, and snippets.

@troufster
Created March 8, 2011 07:39
Show Gist options
  • Save troufster/859995 to your computer and use it in GitHub Desktop.
Save troufster/859995 to your computer and use it in GitHub Desktop.
Generic repository for Entity Framework CTP5
interface IRepository<T> where T : class
{
void Add(T o);
void Commit();
System.Linq.IQueryable<T> Get();
void Remove(T o);
}
public class Repository<T> : IRepository<T> where T : class
{
private readonly DbContext _context;
public Repository(){}
public Repository(DbContext context)
{
_context = context;
}
~Repository()
{
_context.Dispose();
}
public IQueryable<T> Get()
{
return _context.Set<T>().AsQueryable();
}
public void Add(T o)
{
_context.Set<T>().Add(o);
}
public void Remove(T o)
{
_context.Set<T>().Remove(o);
}
public void Commit()
{
_context.SaveChanges();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment