We have a very messy data repository component, with dozens of methods to interface a DbContext (entire database) in Entity Framework.
It was (and is) coded in a way that adds a new repo method for each table combination a query needs to run on/return.
For example:
GetEmployees(), returning something like DbContext.Employees.Where(...)
GetEmployeesWithDepartments, returning DbContext.Employees.Include(x => x.Deparments).Where(...)
GetEmployeesWithDeparmentsWithManagers, returning DbContext.Employees.Include(x => x.Departments).Include(x => x.Managers).Where(...)
something like that.
I am now working on refactoring that, and proposed to unify all such repo methods - into one around the same main table. In the example above, those 3 methods would give place to a single one looking like
GetEmployees(bool include includeDepartments = false, bool include includeManager = false) { var query = dbContext .Employees(); if (includeDepartments) query.Include(x => x.Departments); if (includeManagers) query.Include(x => x.Managers); //apply query predicate and return... }
Someone said this conflicts with S in SOLID. I don't see how (the responsibility is one - querying a specific db main table - with the convenience of allowing to return additional stuff). The proposed above has the obvious advantage that, when refactoring in the future - we will only need to touch a single place.
Am I wrong in principle ? Are there other motifs my suggestion is not a good idea ?
Looks like MSDN does encourage the behavior I am proposing, in their Repository best practices (see Implement a Generic Repository and a Unit of Work Class).