Created
March 8, 2011 07:39
-
-
Save troufster/859995 to your computer and use it in GitHub Desktop.
Generic repository for Entity Framework CTP5
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
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