Domain-Driven Design: Tips and Tricks to Learn the Best Theories and Principles of Domain-Driven Design (2) by LEWIS Jim

Domain-Driven Design: Tips and Tricks to Learn the Best Theories and Principles of Domain-Driven Design (2) by LEWIS Jim

Author:LEWIS, Jim [LEWIS, Jim]
Language: eng
Format: azw3, epub
Published: 2020-05-19T16:00:00+00:00


In this section, we will look at the general life cycle of an entity.

Creation

You should use a type of factory when you create an entity. We will use basic factory methods to do this.

Factory Methods

Consider the following example:

class User {

// ...

private constructor (props: IUserProps, id?: UniqueEntityId) {

super(props, id);

}

public static createUser (props: IUserProps, id?: UniqueEntityId) : Result<User> {

const userPropsResult: Result = Guard.againstNullOrUndefined([

{ propName: 'name', value: props.name },

{ propName: 'email', value: props.email },

{ propName: 'active', value: props.active }

]);

if (userPropsResult.isSuccess) {

return Result.ok<User>(new User(props, id))

} else {

return Result.fail<User>(userPropsResult.error);

}

}

}

We create the entity User using the Factory Method createUser. You cannot use the new keyword to create a new user in the logic.

const user: User = new User(); // <= constructor is private

When you do this, you protect the data integrity of the logic. You can also control which instances or functions can use the user entity if they enter the domain level logic. You can use different factory methods to create more than one type or form of entity. Alternatively, you can use abstract factories.

Entity Base Class

Remember never to copy the value object or entity class from another code. An entity or value object is important for your code. Therefore, write your own code for these attributes. If you do not have the time to write fresh code, you can use the existing code, but remember to make necessary changes to the code.

import { UniqueEntityID } from './types';

const isEntity = (v: any): v is Entity<any> => {

return v instanceof Entity;

};

export abstract class Entity<T> {

protected readonly _id: UniqueEntityID;

protected props: T;

// Take note of this particular nuance here:

// Why is "id" optional?

constructor (props: T, id?: UniqueEntityID) {

this._id = id ? id : new UniqueEntityID();

this.props = props;

}

// Entities are compared based on their referential

// equality.

public equals (object?: Entity<T>) : boolean {

if (object == null || object == undefined) {

return false;

}

if (this === object) {

return true;

}

if (!isEntity(object)) {

return false;

}

return this._id.equals(object._id);

}

}

Note the following about the Entity<T> class. This is the base class.

This is an abstract class, which means that you cannot initialize it directly, but you can use it as a subclass. This decision is based on the domain logic. Remember, an entity can only exist if it has a specific type to it. For example, Car extends Entity<ICarProps>.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.