I am looking for full ef repository interface (and implementation). I have this:
public interface IRepository<T> where T: class
{
IQueryable<T> GetQuery();
IEnumerable<T> GetAll();
IEnumerable<T> Find(Func<T, bool> where);
T Single(Func<T, bool> where);
T First(Func<T, bool> where);
void Delete(T entity);
void Add(T entity);
void Attach(T entity);
void SaveChanges();
}
And I am looking an interface of all methods include SingleOrDefault and so on.
Where I can find such thing?
The Repository Pattern allows us to create an abstraction layer between the data access layer and the business logic layer of an application. So, this Data Access Pattern offers a more loosely coupled approach to data access.
The unit of work class serves one purpose: to make sure that when you use multiple repositories, they share a single database context. That way, when a unit of work is complete you can call the SaveChanges method on that instance of the context and be assured that all related changes will be coordinated.
The single best reason to not use the repository pattern with Entity Framework? Entity Framework already implements a repository pattern. DbContext is your UoW (Unit of Work) and each DbSet is the repository. Implementing another layer on top of this is not only redundant, but makes maintenance harder.
A Repository pattern is a design pattern that mediates data from and to the Domain and Data Access Layers ( like Entity Framework Core / Dapper). Repositories are classes that hide the logics required to store or retreive data.
There are two ways to define repository. First is by exposing IQueryable which is enough to do anything:
public interface IRepository<T> where T: class
{
IQueryable<T> GetQuery();
// This method requires additional knowledge about entity
// or more compilcated approach. The point of the method
// is to check EF's internal storage first before querying DB
// T GetById(int Id);
void Delete(T entity);
void Add(T entity);
void Attach(T entity);
}
Having anything like GetAll
or First
is simply redundant because GetQuery
servers it all. The second approach is specific repository where you don't expose IQueryable
:
public interface IRepository<T> where T : class
{
IEnumerable<T> GetAll();
// Expressions!!!! Func will load all items to memeory
// and then perform filtering by linq-to-objects!!!!!!
IEnumerable<T> Find(Expression<Func<T, bool>> where);
T Single(Expression<Func<T, bool>> where);
T First(Expression<Func<T, bool>> where);
void Delete(T entity);
void Add(T entity);
void Attach(T entity);
}
The second version is then derived by concrete repository interfaces which add methods like GetXXXOrderedByName, GetXXXWithRelatedYYY, etc.
Another point is that SaveChanges
is usually not part of repository because you can need to modify items from multiple repositories and save changes on all of them by single method. For this purpose another pattern exists - unit of work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With