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));
	}
}

Last updated