이 전에 레거시 프로젝트를 진행할 때, 기본 MVC 패턴을 사용해서 비즈니스 로직을 처리 했었는데, 새로운 기능을 추가하거나 유지보수 하는데 시간이 많이 걸릴 것 같다고 느껴졌어요..
그래서 무엇보다 이번 리팩토링 작업을 진행하면서 비즈니스 로직을 최대한 읽기 쉽게 하자는 생각으로 개발을 진행했어요!
그저께 패치를 진행했었는데, 해당 프로젝트를 배포하고 2주 뒤에 다시 코드를 읽었었는데..
비즈니스 로직의 작업 단위가 깔끔하게 읽혀서, 원하는 데이터를 처리하고 기능도 쉽게 구현할 수 있었다는 것을 느꼈어요!
한 가지 예를 들자면..
@Slf4j
@Getter
@NoArgsConstructor
@ToString
@Entity
public class Nmap {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Enumerated(EnumType.STRING)
private NmapOption option;
@NotEmpty(message = "IP는 필수값입니다.")
private String ip;
private String port;
@Transient
private String[] transCmd;
private String regDts;
private String logName;
private char complete;
@Builder
public Nmap(NmapOption option, String ip, String port) {
SimpleDateFormat format = new SimpleDateFormat("YYYYMMddHHmmss");
this.regDts = format.format(new Date());
this.logName = this.regDts + "_nmap.txt";
this.complete = 'N';
// 포트 지정 스캔 후 포트를 지정하지 않았을 경우 Exception
if (option == NmapOption.SET && (port == null || port.isEmpty())) throw new BaseException("포트 지정은 필수입니다.");
// 쉘 명령 조합
if (option == NmapOption.SET) {
this.option = option;
this.ip = ip;
this.port = port;
transCmd = new String[4];
transCmd[0] = "nmap";
transCmd[1] = option.getOption();
transCmd[2] = port;
transCmd[3] = ip;
}
if (option == NmapOption.ALL) {
this.option = option;
this.ip = ip;
transCmd = new String[3];
transCmd[0] = "nmap";
transCmd[1] = option.getOption();
transCmd[2] = ip;
}
}
public void Complete() {
this.complete = 'Y';
}
}
한 도메인 클래스인데, 비즈니스 로직을 담당하는 서비스 클래스에서 객체를 생성하게 돼요..
생성자에 이 도메인에 대한 비즈니스 로직을 도메인 클래스에 추가해 줌으로써, 실제 비즈니스 로직을 담당할 서비스 클래스의 로직이 줄어들게 됐습니다.. 무결성도 확보되고!
@Slf4j
@Service
@RequiredArgsConstructor
public class NmapServiceImpl implements NmapService {
private final ShellCommandExecutor shellCommandExecutor;
private final NmapRepository nmapRepository;
@Override
public NmapInfo execute(NmapCmd nmapCmd) {
var initCmd = nmapCmd.toEntity();
nmapRepository.save(initCmd);
shellCommandExecutor.execute(initCmd.getTransCmd(), initCmd.getLogName());
String log = shellCommandExecutor.getLog(initCmd.getLogName());
initCmd.Complete();
nmapRepository.save(initCmd);
return new NmapInfo(initCmd, log);
}
}
실제 비즈니스 로직을 담당하는 서비스 클래스인데, Entity 메서드를 통해 해당 도메인 클래스를 생성합니다.
execute 메서드 전, 후로 작업 시작, 작업 완료를 나타내는데, 이렇게 비즈니스 로직의 작업 단위가 명확하게 읽혀서 확장 및 유지 보수가 쉬워졌다고 느껴졌어요..!!
'Java > Trouble-Shooting' 카테고리의 다른 글
리눅스 환경에서 Springboot Thymeleaf(View) Templates을 가져오지 못하는 오류 + 해결 (1) | 2025.03.12 |
---|---|
실시간 로그 모니터링에 대한 문제 + 해결 (2) | 2025.03.11 |