백엔드는 DDD 원칙을 따르려고 노력했다.
DddContext 클래스를 만들어서 레포지토리, 서비스 등의 클래스들을 컨테이너에 담아 쉽게 쓸 수 있도록 하며 의존성을 관리했다. (기존 typeorm에서는 getRpository()
or new Class() 사용)
아직 이벤트는 따로 만들어야 할 필요성을 느끼지 못해서 나중에 필요하면 만들 생각이다.
import { customAlphabet } from 'nanoid';
import { Container, Token } from 'typedi';
import { EntityManager } from 'typeorm';
type ClassType<T> = new (...args: any[]) => T;
export class DddContext {
private _txId!: string;
get!: <T>(type: ClassType<T> | Token<T>) => T;
set!: <T>(type: ClassType<T> | Token<T>, instance: T) => void;
dispose: () => void;
constructor(txId: string) {
this._txId = txId;
const containerId = customAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-', 10)();
this.dispose = () => {
Container.reset(containerId);
};
const container = Container.of(containerId);
container.set(DddContext, this);
this.get = (type) => container.get(type);
this.set = (type, instance) => container.set(type, instance);
}
get txId(): string {
return this._txId;
}
get entityManager(): EntityManager {
return this.get(EntityManager);
}
set entityManager(entityManager: EntityManager) {
this.set(EntityManager, entityManager);
}
}