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
Domain-Driven Design: Tips and Tricks to Learn the Best Theories and Principles of Domain-Driven Design (2) by LEWIS Jim.epub
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.
Deep Learning with Python by François Chollet(12566)
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9775)
Dependency Injection in .NET by Mark Seemann(9335)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8293)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7026)
Microservices with Go by Alexander Shuiskov(6793)
Practical Design Patterns for Java Developers by Miroslav Wengner(6705)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6647)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Angular Projects - Third Edition by Aristeidis Bampakos(6054)
The Art of Crafting User Stories by The Art of Crafting User Stories(5585)
NetSuite for Consultants - Second Edition by Peter Ries(5519)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5321)
Kotlin in Action by Dmitry Jemerov(5061)
