✔️
types-ddd
  • Introduction
  • Result
    • ok
    • success
    • fail
    • combine
  • UniqueEntityID
  • BaseDomainEntity
  • ValueObject
  • Entity
  • AggregateRoot
    • IHandle
    • IDomainEvent
    • DomainEvents
  • WriteList
    • ReadList
  • IUseCase
  • IBaseConnection
  • BaseRepository
  • IRepository
  • Filter
  • IMapper
  • Utils
    • SpecificationComposite
    • ChangesObserver
Powered by GitBook
On this page

Was this helpful?

AggregateRoot

Encapsulate and are composed of entity classes and value objects that change together in a business transaction

Aggregate root should be an entity, an aggregate can even be a single entity.

import { 
	AggregateRoot, 
	UniqueEntityID, 
	Result, 
	BaseDomainEntity,
	EmailValueObject,
	PasswordValueObject,
	UserNameValueObject
} from 'types-ddd';

interface Props extends BaseDomainEntity {
	email: EmailValueObject;
	password: PasswordValueObject;
	name: UserNameValueObject;
}

class User extends AggregateRoot<Props> {
	private constructor (props: Props) {
		super(props, User.name);
	}

	get name (): UserNameValueObject {
		return this.props.name;
	}

	get email (): EmailValueObject {
		return this.props.email;
	}

	get password (): PasswordValueObject {
		return this.props.password;
	}
	
	changeEmail (email:EmailValueObject): void {
		this.props.email = email;
	} 

	public static create (props: Props): Result<UserAggregate> {
		return Result.ok<UserAggregate>(new UserAggregate(props, id));
	}
}
PreviousEntityNextIHandle

Last updated 3 years ago

Was this helpful?