@nest-kr/ddd
v3.0.0
Published
```cli npm i @nest-kr/ddd ```
Downloads
32
Readme
Nest-kr/ddd
This library is for DDD
Installation
$ npm install --save @nest-kr/ddd
How to use
Import DDDModule
This will automatically register DomainEventHandlers to a DomainEventDispatcher.
import {DDDModule} from '@nest-kr/ddd';
@Module({
imports: [DDDModule],
providers: [UserDomainEventService],
})
export class AppModule {}
ValueObject
import {ValueObject} from '@nest-kr/ddd';
class Address extends ValueObject<Address>{
}
Identity
import {Identity} from '@nest-kr/ddd';
class Identity extends Identity<UserId>{
}
Entity
import {Entity} from '@nest-kr/ddd';
class User extends Entity<User, UserId>{
}
AggregateRoot
import {AggregateRoot} from '@nest-kr/ddd';
class Order extends AggregateRoot<Order, OrderId>{
}
Domain Event
import {DomainEvent} from '@nest-kr/ddd';
class UserCreated extends DomainEvent{
}
Domain Event Handler
import { Injectable } from '@nestjs/common';
import { DomainEventHandler } from '@nest-kr/ddd';
import { UserCreated } from '../../domain/events/UserCreated';
@Injectable()
export class UserDomainEventService {
@DomainEventHandler(UserCreated)
public handleUserCreated(userCreated: UserCreated): void {
}
}
Domain Event Dispatcher
import { Repository, DomainEventDispatcher } from '@nest-kr/ddd';
import { Injectable } from '@nestjs/common';
...
@Injectable()
export class UserRepository extends Repository<User, UserId> {
constructor(
private readonly mapper: UserMapper,
private readonly domainEventDispatcher: DomainEventDispatcher,
) {
super();
}
async save(user: User): Promise<void> {
const entity = this.mapper.toEntity(user);
await this.ormRepository().save(entity);
// Get domain events from aggregate root and dispatch all
const events = user.getDomainEvents();
this.domainEventDispatcher.dispatch(events);
}
...
}