Skip to end of metadata
Go to start of metadata

You are viewing an old version of this content. View the current version.

Compare with Current View Version History

Version 1 Current »

왜 JPA를 통한 DB 상호작용 외의 기능을 포함하면 부적절한가?

  1. SRP(단일 책임 원칙) 위반:

    • NotificationConfigService의 역할은 데이터 저장소와의 상호작용을 추상화하는 것입니다.

    • JPA로 DB 작업을 수행하는 Repository와 Port-Adapter 계층은 영속성 로직에만 집중하고, 다른 비즈니스 로직은 응용 계층(Application Service)에서 처리하는 것이 원칙입니다.

    • 추가적인 로직이 포함되면 SRP(단일 책임 원칙)를 위반하게 됩니다.

  1. 비즈니스 로직은 응용 서비스에서 처리해야 함:

    • 비즈니스 로직은 Core Domain이나 응용 서비스에서 처리하고, Port는 외부 시스템(DB, API 등)에 대한 의존성을 추상화하는 역할을 해야 합니다.

    • 따라서 NotificationConfigImplService는 단순히 DB와의 상호작용을 위한 Adapter로 남아야 합니다.

  1. 의존성 관리:

    • 헥사고날 아키텍처에서는 DB와의 상호작용은 Outbound Port에서 담당하며, 비즈니스 로직이 포함되면 Port의 의존성이 복잡해집니다.

    • Port-Adapter 패턴의 구현체(NotificationConfigImplService)에 복잡한 로직을 포함하면 Adapter와 Domain의 의존 관계가 서로 얽히게 됩니다.


올바른 책임 분리 예시

1. 비즈니스 로직을 응용 계층에서 처리:

@Service
public class NotificationService {

    private final NotificationConfigService configService;

    public NotificationService(NotificationConfigService configService) {
        this.configService = configService;
    }

    public void handleNewConfig(NotificationConfig config) {
        // 비즈니스 로직 처리
        if (config.isValid()) {
            configService.saveConfig(config); // DB와의 상호작용은 Port에서 처리
        } else {
            throw new IllegalArgumentException("Invalid config");
        }
    }
}

2. Port와 Adapter는 DB와의 상호작용만 수행:

// Port 인터페이스
public interface NotificationConfigService {
    NotificationConfig getConfigById(Long id);
    NotificationConfig saveConfig(NotificationConfig config);
}

// Adapter(구현체)
@Service
public class NotificationConfigImplService implements NotificationConfigService {

    private final NotificationConfigRepository repository;

    public NotificationConfigImplService(NotificationConfigRepository repository) {
        this.repository = repository;
    }

    @Override
    public NotificationConfig getConfigById(Long id) {
        return repository.findById(id)
            .orElseThrow(() -> new RuntimeException("Config not found"));
    }

    @Override
    public NotificationConfig saveConfig(NotificationConfig config) {
        return repository.save(config);
    }
}


결론

  • DB와의 상호작용 외의 로직은 NotificationConfigService에 포함되지 않도록 해야 합니다.

  • 비즈니스 로직은 응용 계층(Service)에서 처리하고, DB와의 상호작용은 Port-Adapter로 위임해야 합니다

  • No labels