"Learning Domain driven Design (ii) context map and architecture" gives you a general introduction to the concept of context mapping, and a rough introduction to the domain-driven design of the main use of architectural knowledge, and this article is mainly to learn the role of entities in the domain-driven.
We need to introduce the concept of entities when we are considering the individual characteristics of an object, or when we need to distinguish between different objects.
An entity is a unique thing and can continue to change over a long period of time. We can make multiple changes to an entity, so the entity object may be a little different from its previous state. However, because they have the same identity identity, they remain the same entity.
unique identity and variability are the fundamental differences between an entity object and a value object
The ultimate purpose of this chapter: How to use and design entities correctly
1. Unique identification
When designing an entity, we first need to consider the intrinsic characteristics of the entity, rather than focusing on the attributes and behavior of the entity at the outset. Find a way to achieve a unique identity, while also considering how to maintain its uniqueness within the life cycle of the entity.
The unique identity used for entity entity matching usually depends on the readability of the identity.
1.1 How to generate unique identities
1. The user provides a unique identity
It may seem straightforward to have the user manually enter an object ID, but this approach can also become complex. The complexity lies in: 1. It is necessary for the user to generate high-quality identities themselves, and it is possible that the identities entered are unique, but may also be incorrect.
2. The application generates a unique identity
There are many reliable ways to automatically generate unique identities, but if your application is in a clustered environment or distributed across different compute nodes, we need to be careful. The following methods can generate 1. The current time of the computer node, in milliseconds 2. IP address of the computer node 3.GUID
3. Persistence mechanism generates unique identities
4. Another clearance context provides a unique identifier
5. Delegating identities
The delegated identity white point is the self-increment primary key of the database, which uses the attributes of the self-increment primary key to generate a unique identity. In the development process, most of our scenarios use this approach to generate unique identities to correlate entities.
1.2 Discovery of physical and essential characteristics 1.2.1 roles and responsibilities
One aspect of modeling is discovering the roles and responsibilities of an object, and in general, the role and responsibility analyst can apply to the domain object.
Domain Objects play multiple roles
In object-oriented programming, the role of an implementation class is typically defined by an interface. In the case of proper design, a class has a role for each interface it implements. If a class does not have an explicit role-that is, the class does not implement any explicit interfaces-by default, it plays the original role.
Public interface User{}//user Object
The user class does not implement any interfaces, but it still acts as a user role.
1.2.2 Verification
The primary purpose of validation is to check the correctness of the model, which can be a property or a combination of an entire object, or even multiple objects.
1. Validating attributes, also known as contract validation in C #
2. Verify the Overall object
Sometimes all the individual attributes in an entity are legitimate, but it does not mean that the whole entity is legal. To validate an entire entity, we need access to the entire object's state, which is the properties of all objects
3. Validating object Combinations
We can use deferred validation when complex objects need to be validated. Because sometimes we don't focus on the legality of individual entities, we also need to focus on whether the combination of multiple entities is legal.
Learning Domain driven Design (iii) entities