3

I'm not an experienced programmer. I always browse the source codes to learn some things. ASP.NET Boilerplate is my favorite one. Yesterday, I noticed there is friendship application service (in service/application layer) and friendship manager (in business/domain layer). I didn't understand why there is friendship manager. Friendship service isn't enough?

public interface IFriendshipAppService : IApplicationService
{
    Task<FriendDto> CreateFriendshipRequest(CreateFriendshipRequestInput input);

    Task<FriendDto> CreateFriendshipRequestByUserName(CreateFriendshipRequestByUserNameInput input);

    void BlockUser(BlockUserInput input);

    void UnblockUser(UnblockUserInput input);

    void AcceptFriendshipRequest(AcceptFriendshipRequestInput input);
}
public interface IFriendshipManager : IDomainService
{
    void CreateFriendship(Friendship friendship);

    void UpdateFriendship(Friendship friendship);

    Friendship GetFriendshipOrNull(UserIdentifier user, UserIdentifier probableFriend);

    void BanFriend(UserIdentifier userIdentifier, UserIdentifier probableFriend);

    void AcceptFriendshipRequest(UserIdentifier userIdentifier, UserIdentifier probableFriend);
}
aaron
  • 39,695
  • 6
  • 46
  • 102

1 Answers1

5

From the documentation on NLayer-Architecture:

The application layer ... perform[s] requested application functionalities. It uses Data Transfer Objects to get data from and return data to the presentation or distributed service layer. ...

The domain layer ... perform[s] business/domain logic. ...

Here's what that means, in high-level comments:

// IFriendshipManager implementation

public void CreateFriendshipAsync(Friendship friendship)
{
    // Check if friending self. If yes, then throw exception.
    // ...

    // Insert friendship via repository.
    // ...
}
// IFriendshipAppService implementation

public Task<FriendDto> CreateFriendshipRequest(CreateFriendshipRequestInput input)
{
    // Check if friendship/chat feature is enabled. If no, then throw exception.
    // ...

    // Check if already friends. If yes, then throw exception.
    // ...

    // Create friendships via IFriendshipManager.
    // ...

    // Send friendship request messages.
    // ...

    // Return a mapped FriendDto.
    // ...
}

Notice that the concerns (and resulting actions) in AppService and Manager are subtly different.

A Manager is meant to be reused by an AppService, another Manager, or other parts of code.

For example, IFriendshipManager can be used by:

  • ChatMessageManager
  • ProfileAppService
  • TenantDemoDataBuilder

On the other hand, an AppService should not be called from another AppService.

See: Should I be calling an AppService from another AppService?

aaron
  • 39,695
  • 6
  • 46
  • 102
  • "On the other hand, an AppService should not be called from another AppService." This sentence was very explanatory. I got it. I've had this problem in my demo applications. Call service from the service. Thank you very much for correction and answer. Have a nice day. – Taşyürek Gökşah Jan 26 '19 at 13:17