After reading this Stackoverflow question I have stumbled upon an MSDN article Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application.
There is a proposed implementation of a repository:
public virtual IEnumerable<TEntity> Get( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "") { IQueryable<TEntity> query = dbSet; if (filter != null) { query = query.Where(filter); } foreach (var includeProperty in includeProperties.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { query = query.Include(includeProperty); } if (orderBy != null) { return orderBy(query).ToList(); } else { return query.ToList(); } } I recently blamed people for the similar code, since in my opinion that code was bad, and now I am confused.
My arguments:
- Whoever uses this method must know and enumerate all included properties of a class
- Every time someone uses this method in a new code he must recall which required properties he needs to include, so he will probably copy-paste the previous usage case
- If someone decides to change property name, then he should Ctrl+Shift+F through a project and change all textual usages
- If someone adds a property which should be included everywhere, then he should find all usages and add ",NewProperty" to every string
For me it sounds like a violation of SRP, DRY and other rules of OOP.
I cannot believe that someone like tdykstra can write bad code, so there should be something I just can't understand and it makes me feel sad. Could you please explain where I made a mistake?