Last active
July 4, 2023 17:09
-
-
Save kemsky/f9a2492a270eb3f134a4e468df99fe2c to your computer and use it in GitHub Desktop.
[Medium] DRY violation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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