4

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.

4

1 に答える 1

4

初め。サービス層は、リポジトリ機能を繰り返さないでください。UserService.UpdateUser関数の代わりに、オブジェクト フィールドのサブセットを取得UserService.UpdateBasicDataする必要があります。通常、サービス中の ORM レイヤー オブジェクトを公開しないでください。オブジェクトには確かに多くのプロパティがあり、関数はそれらを変更してはならないため、サービス コンシューマを誤解してはならず、それらを変更する可能性があります。UserService.UpdatePasswordUserUserUpdateUser

2番。引き続き一般的なリポジトリを使用して、インジェクション用のリポジトリ集約を作成できます。例:

public class UserRelatedRepositories : IUserRelatedRepositories {
    IRepository<User> User { get; set; }
    IRepository<Business> Business { get; set; }
}

そして使用する

UserRelatedRepositories.User.Create()
于 2012-06-05T22:04:13.017 に答える