Skip to content

Instantly share code, notes, and snippets.

@kemsky
Last active July 4, 2023 17:09
Show Gist options
  • Save kemsky/f9a2492a270eb3f134a4e468df99fe2c to your computer and use it in GitHub Desktop.
Save kemsky/f9a2492a270eb3f134a4e468df99fe2c to your computer and use it in GitHub Desktop.
[Medium] DRY violation
public class UserService
{
private readonly DbContext _context;
public UserService(DbContext context)
{
_context = context;
}
public Task<User> GetUser(int userId)
{
return _context.Set<User>()
.Where(x => x.userId == userId)
.Where(x => x.Active) // repeated predicate
.SingleOrDefaultAsync();
}
public Task<User> GetUserInDepartment(int userId, int departmentId)
{
return _context.Set<User>()
.Where(x => x.userId == userId)
.Where(x => x.Active) // repeated predicate
.Where(x => x.Departments.Any(z => z.Id == departmentId))
.SingleOrDefaultAsync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment