3

我不是一个有经验的程序员。我总是浏览源代码来学习一些东西。ASP.NET Boilerplate 是我最喜欢的一个。昨天,我注意到有友谊应用服务(在服务/应用层)和友谊管理器(在业务/领域层)。我不明白为什么会有友谊经理。友情服务还不够?

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);
}
4

1 回答 1

5

从关于NLayer-Architecture的文档中:

应用层...执行[s]请求的应用功能。它使用数据传输对象从表示层或分布式服务层获取数据并返回数据。...

领域层......执行[s]业务/领域逻辑。...

在高级评论中,这意味着:

// 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?

于 2019-01-26T09:54:21.867 回答