リポジトリパターンを使用する場合、異なるリポジトリに同じロジックが表示されることがあります。以下の例では、EmployeeRepositoryのGetTempEmployees()とCompanyRepositoryのGetCompaniesWithTemps()の式が同じです。
e.IsTemp && e.IsDeleted == false
私の質問は、この式ロジックの重複を最小限に抑えるために推奨される方法は何ですか。
例えば。
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();
}
}