Axs Document
  • AXS Framework
  • 1. Common structures
    • 의존성 관계 설정 - Dependency Injection
    • 캐싱 - Caching
    • 로깅 - Logging
    • 세션 - Session
    • 다중 테넌트 - Multi Tenancy
    • 모듈 시스템 - Module System
    • 객체 매핑 - Object To Object Mapping
    • 메일 연동 - Mail System
  • 2. Domain Layer
    • 엔티티 - Entities
    • 저장소 - Repositories
    • 도메인 서비스 - Domain Services
    • 작업 단위 - Unit Of Work
  • 3. Application Layer
    • 애플리케이션 컨트롤러 - Application Controller
    • DTO - Data Transfer Objects
    • DTO 객체의 유효성 체크 - Validating Data Transfer Objects
    • 인증 - Authorization
    • 감사 로그 - Audit Logging
    • 엔티티 기록 - Entity History
  • 4. Object Relational Mapping
    • EntityFramework 통합
    • Dapper 통합
  • 5. Background Services
    • 백그라운드 작업과 워커
  • 6. Presentation Layer
    • 에러 핸들링
Powered by GitBook
On this page
  1. 2. Domain Layer

도메인 서비스 - Domain Services

Previous저장소 - RepositoriesNext작업 단위 - Unit Of Work

Last updated 1 year ago

Domain Services

설명

도메인 서비스는 도메인 작업 및 비즈니스 규칙을 수행하는 데 사용됩니다. 도메인 서비스는 응용 프로그램 서비스 및 기타 도메인 서비스에서 사용할 수 있지만 프레젠테이션 계층에서 직접 사용할 수는 없습니다.

IDomainService 인터페이스 및 DomainService 클래스

Axs는 모든 도메인 서비스에서 IDomainService 인터페이스를 정의합니다. 구현되면 도메인 서비스가 종속성 주입() 시스템에 자동 등록됩니다.

도메인 서비스는 선택적으로 DomainService 클래스에서 상속 할 수 있습니다.

예제

작업 관리 시스템이 있고 사람에게 작업을 할당하는 동안 몇 가지 비즈니스 규칙이 있다고 가정합니다.

도메인 서비스 인터페이스

먼저 서비스에 대한 인터페이스를 정의합니다 :

public interface ILunchService : IDomainService
{
    void AssignLunchToMeal(Lunch lunch, Meal meal);
}

도메인 서비스의 이름은 규칙이 있습니다. LunchService 또는 LunchDomainService 일 수 있습니다.

도메인 서비스 구현

public class LunchService : DomainService, ILunchService
{
    public const int MaxLunchCount = 1;

    private readonly ITaskRepository _taskRepository;

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

    public void AssignLunchToMeal(Lunch lunch, Meal meal)
    {
        // 활성상태가 아니면 에러 발생
        if (task.State != TaskState.Active)
        {
            throw new ApplicationException("점심을 먹었습니다.");
        }

        // 허용된 활성된 숫자인지 체크
        if (HasLunchMaximumAssignedMeal(meal))
        {
            // 오버가 되면 에러 발생
            throw new UserFriendlyException(L("점심을 이미 먹었습니다..", meal.Name));
        }
    }

    // 규칙에 맞는지 체크한다
    private bool HasPersonMaximumAssignedTask(Meal meal)
    {
        return assignedTaskCount >= MaxActiveTaskCountForSample;
    }
}

여기에는 한 가지 업무 규칙이 있습니다.

  • 점심을 최대 한번만 먹을 수 있습니다.

애플리케이션 컨트롤러에서 도메인 서비스 사용하기

public class TaskAppController : AppController, ITaskAppController
{
    private readonly IRepository<Lunch, long> _lunchRepository;
    private readonly IRepository<Meal> _mealRepository;
    private readonly ILunchService _lunchService;

    public TaskAppController(IRepository<Lunch, long> lunchRepository, IRepository<Meal> mealRepository, ILunchService lunchService)
    {
        _lunchRepository = lunchRepository;
        _mealRepository = mealRepository;
        _lunchService = lunchService;
    }

    public void AssignLunchToMeal(AssignTaskToMealDto input)
    {
        var lunch = _lunchRepository.Get(input.LunchId);
        var meal = _mealRepository.Get(input.mealId);

        _lunchService.AssignLunchToMeal(lunch, meal);
    }
}

애플리케이션 컨트롤러에서 도메인 서비스를 종속성 주입을 통해 사용할 수 있다.

스웨거에서 확인

Dependency Injection
picture 2
picture 1