【发布时间】:2021-04-18 05:13:25
【问题描述】:
我创建了这个聚合和这个值对象只是为了说明疑问,我的值对象还没有验证
聚合:
export class Person extends Entity<{name: PersonName}> {
private constructor(name: PersonName, id?: string) {
super({name}, id)
}
public static build(
name: PersonName, id: string
): Either<IErrorModel[], Person> {
const person = new Person(name, id)
return right(person)
}
}
值对象:
export class PersonName extends ValueObject<IName> {
public static maxLength: number = 30;
public static minLength: number = 4;
private constructor(props: IName) {
super(props)
}
public get fullName(): string {
return `${this.props.firstName} ${this.props.lastName}`
}
public get firstName(): string {
return this.props.firstName
}
public get lastName(): string{
return this.props.lastName
}
public static build(props: IName): Either<IErrorModel[], PersonName> {
const name = new PersonName(props)
return right(name)
}
}
我有一个问题,我应该如何在我的聚合中创建我的工厂 一个例子我有我的价值对象:名称,我必须在我的聚合工厂中创建并验证这个价值对象,或者我必须在服务中创建例如
在我的聚合工厂:
export class Person extends Entity<{ name: PersonName }> {
private constructor(name: PersonName, id?: string) {
super({ name }, id)
}
public static build(
{ lastName, firstName }: { firstName: string; lastName: string },
id: string,
): Either<IErrorModel[], Person> {
let errors = [] as IErrorModel[]
// others validations
const name = PersonName.build({lastName,firstName})
if(name.isLeft()) errors.push(...name.value)
const person = new Person(name.value as PersonName, id)
return right(person)
}
}
或者例如在我的个人服务中:
聚合工厂:
public static build(
name: PersonName, id: string
): Either<IErrorModel[], Person> {
const person = new Person(name, id)
return right(person)
}
服务:
export class PersonService{
execute(req: any) {
let errors = [] as IErrorModel[]
const {lastName, firstName} = req
const name = PersonName.build({lastName,firstName})
if(name.isLeft()) errors.push(...name.value)
const person = Person.build(name.value as PersonName, v4())
}
}
我的疑问是: 我应该在烧结厂还是其他地方制作我的贵重物品?
【问题讨论】:
标签: typescript domain-driven-design