There are several good blogs about how to implement the repository pattern together with the unit-of-work pattern using generic classes.
Implementing a Data Access Layer with Entity Framework 6.1
Implementing the Repository and Unit of Work Patterns
The Idea is, to define a generic interface IRepository and a class Repository that hides how the data is actually accessed. It can be accessed using Entity Framework DbContext, or maybe the repository is an in memory collection for unit testing.
public interface public interface IRepository<T> where T : class
{
T GetById(int Id);
void DeleteById(int Id);
void Add(T entity);
void Update(T entity);
etc.
}
Quite often I see that several Query functions are added that are similar to Queryable and/or Enumerable functions.
For instance in Implementing a data access layer I see:
/// Returns an IEnumerable based on the query, order clause and the properties included
/// <param name="query">Link query for filtering.</param>
/// <param name="orderBy">Link query for sorting.</param>
/// <param name="includeProperties">Navigation properties seperated by comma for eager loading.</param>
/// <returns>IEnumerable containing the resulting entity set.</returns>
IEnumerable<T> GetByQuery(Expression<Func<T, bool>> query = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "");
/// <summary>
/// Returns the first matching entity based on the query.
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
T GetFirst(Expression<Func<T, bool>> predicate);
If the interface had a function IQueryable GetQuery(), then I wouldn't have to make functions like GetFirst() and GetByQuery().
Question: Why is this not recommended? Can people change the data in an undesirable way?