Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository pattern with Entity Frameworks 4

I used to use NHibernate with repository interfaces.

What is the proper way to use this pattern with EF?
How can I implement this repository interface, for a RepositoryBase<T>?

public interface IRepository<T>
{
    T GetById(object id);
    void Save(T entity);
    T[] GetAll();
    void Delete(T entity);
}
like image 973
stacker Avatar asked Oct 14 '22 02:10

stacker


2 Answers

For some reason all of the examples given expose the collections as IQueryable or IEnumerable. EF4 has an interface for this very purpose - IObjectSet (or IDbSet if you're using the latest CTP).

Julie Lerman has a tremendous post on doing this, including creating a MockSet that implements IObjectSet, so you can do some disconnected unit testing

http://thedatafarm.com/blog/data-access/agile-entity-framework-4-repository-part-6-mocks-amp-unit-tests/

like image 199
Adam Rackis Avatar answered Nov 15 '22 09:11

Adam Rackis


It's not really a whole lot different than any other ORM. Here's an example: http://blogs.microsoft.co.il/blogs/gilf/archive/2010/01/20/using-repository-pattern-with-entity-framework.aspx

like image 31
Andrew Avatar answered Nov 15 '22 09:11

Andrew