Abp框架 领域服务_04

地址  https://aspnetboilerplate.com/Pages/Documents/Domain-Services#introduction

1.介绍:

  领域服务(只是DDD领域中的服务)通常用来执行领域操作或业务规则。

  1. The operation relates to a domain concept that is not a natural part of an Entity or Value Object.
  2. The interface is defined in terms of other elements of the domain model.
  3. The operation is stateless.

  应用服务用来传输对象,领域服务获取 或者 返回 “实体或值类型”。

  域服务可由应用服务和其他域服务使用,但不能直接由表示层(UI层、webapi接口层)使用(应用程序服务用于该服务)。

2.如何创建领域服务

  一般情况下:

Creating an Interface

First, we define an interface for the service (not required, but good practice):

public interface ITaskManager : IDomainService
{
    void AssignTaskToPerson(Task task, Person person);
}

Service Implementation

public class TaskManager : DomainService, ITaskManager
{
    public const int MaxActiveTaskCountForAPerson = 3;

    private readonly ITaskRepository _taskRepository;

    public TaskManager(ITaskRepository taskRepository)
    {
        _taskRepository = taskRepository;
    }

    public void AssignTaskToPerson(Task task, Person person)
    {
        if (task.AssignedPersonId == person.Id)
        {
            return;
        }

        if (task.State != TaskState.Active)
        {
            throw new ApplicationException("Can not assign a task to a person when task is not active!");
        }

        if (HasPersonMaximumAssignedTask(person))
        {
            throw new UserFriendlyException(L("MaxPersonTaskLimitMessage", person.Name));
        }

        task.AssignedPersonId = person.Id;
    }

    private bool HasPersonMaximumAssignedTask(Person person)
    {
        var assignedTaskCount = _taskRepository.Count(t => t.State == TaskState.Active && t.AssignedPersonId == person.Id);
        return assignedTaskCount >= MaxActiveTaskCountForAPerson;
    }
}

 3. 应用层调用领域服务

  一般情况:

   创建应用层服务接口

   实现应用层服务接口

    实现例子:

  “ApplicationService, ITaskAppService

public class TaskAppService : ApplicationService, ITaskAppService
{
    private readonly IRepository<Task, long> _taskRepository;
    private readonly IRepository<Person> _personRepository;
    private readonly ITaskManager _taskManager;

    public TaskAppService(IRepository<Task, long> taskRepository, IRepository<Person> personRepository, ITaskManager taskManager)
    {
        _taskRepository = taskRepository;
        _personRepository = personRepository;
        _taskManager = taskManager;
    }

    public void AssignTaskToPerson(AssignTaskToPersonInput input)
    {
        var task = _taskRepository.Get(input.TaskId);
        var person = _personRepository.Get(input.PersonId);

        _taskManager.AssignTaskToPerson(task, person);
    }
}

 4.官网上的一些说明 

  Why not use only the Application Services?

  为什么不直接使用应用服务?

  参考官网说明 https://aspnetboilerplate.com/Pages/Documents/Domain-Services#some-discussions

  理解领域驱动不是一天两天就能理解的,慢慢跟着学习吧。

 5. 我们如何强制使用域服务

  参考 https://aspnetboilerplate.com/Pages/Documents/Domain-Services#how-do-we-force-to-use-of-the-domain-service-

  这个做法666666

原文地址:https://www.cnblogs.com/youlicc/p/14161587.html