0

I understand that the repository should be responsible for CRUD in repository pattern. My question is: should a respository be responsible to update specified field of model stored in external service?

For example:

  • The UserAccount data is stored in external web service.
  • The user could have multiple UserAccount
  • By the app, the user can select target UserAccount and can update displayName of UserAccount
  • There are some words not allowed to use in displayName and the app doesn't know what are those words.

pesdocode:

public class MainController
{
    IUserAccountService userAccountService; // injected

    public void Main ()
    {
        userAccountService.GetUsers ().Then (UpdateUserView);
    }

    // ...

    void OnUserAccountSelected (UserAccount selectedUserAccount, string newDisplayName)
    {
        userAccountService.UpdateDisplayName (selectedUserAccount, newDisplayName)
        .Then (UpdateUserView)
        .Catch<WordInDisplayNameNotAllowedException> (HandleWordInDisplayNameNotAllowedException)
        .Catch (HandleError);
    }
}

public class UserAccountService : IUserAccountSercie
{
    IUserAccountRepository repository; // injected

    // ...

    public Promise<UserList> GetUsers ()
    {
        return repository.GetAll ();
    }

    public UpdateDisplayName (UserAccount userAccount, string newDisplayName)
    {
        // Should I like the following?
        repository.UpdateDisplayName (userAccount, newDisplayName);
    }
}

public class UserAccountRepository : IUserAccountRepository
{
    public Promise<UserList> GetAll ()
    {
        // request to external web service...
    }

    public Promise<UserAccount> UpdateDisplayName (UserAccount userAccount, string newDisplayName)
    {
        // request to external web service...
    }
}

Where should I put codes to call web API to update UserAccount::displayName?

homu
  • 119
  • 5
  • The repository is the place to make the update call. The external Web API is your data store in this case, so put the repository in charge of working with it. – dbugger Jan 28 '16 at 16:16

1 Answers1

1

Use case logic like this should go in an application service. The repository should only care about updating whole aggregates.

I think this answer of mine will provide enough information about application services to point you in the right direction.

Community
  • 1
  • 1
theDmi
  • 17,546
  • 6
  • 71
  • 138