Skip to content

Domain service pattern implementation

The Domain service pattern implementation demonstrated here focuses on encapsulating specific business logic—specifically report generation and retrieval—that does not naturally fit within a single entity or value object^[001-TODO__code-getway.md#L130-136]. This approach separates the orchestration of domain operations from the controller layer and the integration with external systems.

Architecture

The implementation follows a classic interface-separation structure where the controller depends on an abstraction rather than a concrete implementation^[001-TODO__code-getway.md#L57-59].

  • Interface (ReportManageDomainService): Defines the contract for domain operations, such as startingWorkReport and makeDownloadFile^[001-TODO__code-getway.md#L132-136].
  • Implementation (ReportManageDomainServiceImpl): Contains the actual business logic and interacts with infrastructure components like Feign clients^[001-TODO__code-getway.md#L59-62].

Application Logic

The domain service acts as a bridge between the API layer and backend services.

Report Orchestration

When a report request is received, the service delegates the creation of the report record to an external service via BasicsFeignClient^[001-TODO__code-getway.md#L63-65].

File Processing and Transformation

The makeDownloadFile method illustrates how a domain service can handle data transformation^[001-TODO__code-getway.md#L66-76]. The service performs the following steps:

  1. Fetches raw data (byte array and metadata) from the external client^[001-TODO__code-getway.md#L68].
  2. Constructs appropriate HTTP headers, including Content-Disposition and filename based on the ReportType^[001-TODO__code-getway.md#L70-71].
  3. Converts raw bytes into a Spring InputStreamResource suitable for HTTP response transmission^[001-TODO__code-getway.md#L72-73].

Integration with Controllers

The controller remains lightweight, delegating authority checks and business execution to the service^[001-TODO__code-getway.md#L57-59]. For example, the ReportManageController invokes methods on the ReportManageDomainService to handle user requests for report generation and downloads^[001-TODO__code-getway.md#L74-76].

Sources

^[001-TODO__code-getway.md]

  • [[Domain-Driven Design (DDD)]]
  • [[Service Layer Pattern]]
  • [[Microservices]]