When using the repository pattern , sometimes you have the same logic that appears in different repositories.In the example below GetTempEmployees() in EmployeeRepository and GetCompaniesWithTemps() in CompanyRepository have the same expressions
e.IsTemp && e.IsDeleted == false
My question is what is the recommended practice for minimizing this duplication of expression logic.
eg.
public class Employee
{
public int EmployeeId { get; set; }
public bool IsTemp { get; set; }
public bool IsDeleted { get; set; }
}
public class Company
{
public int CompanyId { get; set; }
public bool IsDeleted { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
public class TestContext : DbContext
{
public TestContext()
{
}
public DbSet<Employee> Employee { get; set; }
public DbSet<Company> Company { get; set; }
}
public class EmployeeRepository
{
private readonly TestContext _context;
EmployeeRepository(TestContext context)
{
_context = context;
}
public ICollection<Employee> GetTempEmployees()
{
return _context.Employee.Where(e => e.IsTemp && e.IsDeleted==false).ToList();
}
}
public class CompanyRepository
{
private readonly TestContext _context;
CompanyRepository(TestContext context)
{
_context = context;
}
public ICollection<Company> GetCompaniesWithTemps()
{
return _context.Company.Where(c => c.Employees.Any(e => e.IsTemp && e.IsDeleted == false)).ToList();
}
}