I've got an ASP.NET MVC application, and I'm using the repository pattern, with service-specific repositories. I'm finding my setup is starting to feel very repetitive.
My layers look like this: UserController > UserService > UserRepository (the repository then uses entity framework)
The controller accepts the service in the constructor for testability:
public UserController(IUserService userService)
The service accepts the repository:
public UserService(IUserRepository)
The user might be able to update their business information, change their name, email, delete a contact address, etc.
So I end up with things like this in the service:
public class UserService {
User CreateUser(....);
User GetUserById(int id);
void UpdateUser(User user);
void DeleteUser(User user);
Business CreateBusiness(...);
Business GetBusinessById(int businessId);
void UpdateBusiness(Business business);
void DeleteBusiness(Business business);
IEnumerable<Business> GetBusinessesByUserId();
IEnumerable<BusinessType> GetBusinessTypes();
...
...
...
Each of these functions call functions like this in the repository layer:
public class UserRepository {
User CreateUser(....);
User GetUserById(int id);
void UpdateUser(User user);
void DeleteUser(User user);
Business CreateBusiness(...);
Business GetBusinessById(int businessId);
void UpdateBusiness(Business business);
void DeleteBusiness(Business business);
IEnumerable<Business> GetBusinessesByUserId();
IEnumerable<BusinessType> GetBusinessTypes();
...
...
...
Any time I need to do a CRUD/data access operation of any sort, I find myself doing the following:
- Add the operation to the repository's interface
- Implement and code the repository function
- Add the operation to the service layer's interface
- Implement and code the service function to call the above repository function
This is getting cumbersome, especially when there are multiple entities related to a particular service/repository. Multiply by multiple repositories and services throughout the application...
For background, I moved away from generic repositories to avoid the complexity of injecting multiple repositories left and right into service and/or controller constructors (or individual service functions).
It seems I am violating DRY, and constantly repeating myself. Is this as close as I can get, or is there a more efficient way to do this?? Thanks.