-
Hi everyone, Firstly, thanks Sairyss for this amazing repository, it's very helpful as I'm new to the DDD concepts. I'm currently trying to build an application with "Member" as an aggregate root and "MemberEmail" as a child entity. From what I understand, the "MemberEmail" should extends the Here is the @CommandHandler(CreateMemberCommand)
export class CreateMemberService implements ICommandHandler {
constructor(
@Inject(MEMBER_REPOSITORY)
protected readonly memberRepo: MemberRepositoryPort,
) {}
async execute(
command: CreateMemberCommand,
): Promise<Result<AggregateID, MemberAlreadyExistsError>> {
const member = MemberEntity.create({
firstName: command.firstName,
lastName: command.lastName,
gender: command.gender,
address: new Address({
country: command.country,
city: command.city,
postalCode: command.postalCode,
street: command.street,
number: command.number,
box: command.box,
}),
emails: command.emails.map((e) => {
return MemberEmailEntity.create({
email: e.email,
isDefault: e.isDefault,
});
}),
});
try {
await this.memberRepo.transaction(async () =>
this.memberRepo.insert(member),
);
return Ok(member.id);
} catch (error: unknown) {
if (error instanceof ConflictException) {
return Err(new MemberAlreadyExistsError(error));
}
throw error;
}
}
} My issue is that I can't figure out how to save it into my database, as I want to save the member emails in an other SQL table (meaning I also need the memberId) Thanks a lot for your time. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
Email seems like a Value Object to me, it's a property of a Member, not an entity with a separate identity. So I'd say extend ValueObject. One of the most important things to understand about DDD is that your domain model is not the same as database model. You shouldn't create an entity for something just because it will be in a separate table. You can just use it as a regular value, or as a value object, and let repository decide how it wants to save it. |
Beta Was this translation helpful? Give feedback.
-
When I have child entity, i consider:
|
Beta Was this translation helpful? Give feedback.
-
@Sairyss First of all thank you for this awesome repo 🙏 How should the mapping be handled in this use case if you want to create a
|
Beta Was this translation helpful? Give feedback.
-
@Sairyss Thanks for this fantastic repo! I have 2 questions on persisting entities.
in Patterns of Enterprise Application Architecture by Martin Fowler. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
Email seems like a Value Object to me, it's a property of a Member, not an entity with a separate identity. So I'd say extend ValueObject.
For saving it to the database, your memberRepo can save the email in a separate table, with the same ID as a member ID (many to one relation). When you query you just join 2 tables.
One of the most important things to understand about DDD is that your domain model is not the same as database model. You shouldn't create an entity for something just because it will be in a separate table. You can just use it as a regular value, or as a value object, and let repository decide how it wants to save it.