Aggregate Root Entity
Files Referenced in this Document
Table of Contents
- Introduction
- Inheritance and Audit Interface
- Properties and Access Control
- Constructor and Factory Method
- Business Behavior Methods
- Field Validation and Constraints
- Domain Events and Optimistic Concurrency Control
- Entity Lifecycle Diagram
- Summary
Introduction
This document details the design and implementation of the MyEntityName entity class. As the Aggregate Root of the core domain model, MyEntityName encapsulates business data and behavioral logic, ensuring data consistency and integrity. The document will analyze its inheritance structure, property definitions, business methods, and lifecycle management in depth.
Inheritance and Audit Interface
MyEntityName inherits from the FullAuditedAggregateRoot<Guid> base class provided by the ABP framework. This inheritance relationship grants the entity the following capabilities:
- Global Unique Identifier: Using
Guidas the primary key type. - Full Audit Functionality: Automatically tracking
CreationTime,CreatorId,LastModificationTime,LastModifierId,IsDeleted,DeleterId, andDeletionTime. - Soft Delete: Data is not physically deleted but marked as deleted via the
IsDeletedproperty. - Optimistic Concurrency Control: Built-in
ConcurrencyStampproperty to prevent concurrent modification conflicts.
Section sources
Properties and Access Control
The entity defines core business properties, all using virtual modifiers to support EF Core's lazy loading and proxy mechanism. Access modifiers limit external direct modification permissions, enforcing modifications through business methods.
| Property Name | Type | Access Control | Constraint | Description |
|---|---|---|---|---|
| Code | string | protected set | Not Null, Max Length 64 | Unique Business Code |
| Name | string | protected set | Not Null, Max Length 64 | Display Name |
| Sort | int | protected set | None | Sort Index |
| Remark | string | protected set | Max Length 256 | Description/Remark |
| IsDisabled | bool? | protected set | None | Enable/Disable Status |
Section sources
Constructor and Factory Method
The entity provides two constructors:
- Protected Parameterless Constructor: For ORM framework reflection and deserialization.
- Public Parameterized Constructor: Acts as a factory method, forcing necessary parameters (
id,code,name) to be provided when creating an instance, and performing initialization validation.
public MyEntityName(Guid id, string code, string name, int sort, string remark) : base(id)
{
Code = Check.NotNullOrWhiteSpace(code, nameof(code), MyEntityNameConsts.MaxCodeLength);
Name = Check.NotNullOrWhiteSpace(name, nameof(name), MyEntityNameConsts.MaxNameLength);
Sort = sort;
Remark = remark;
}
Section sources
Business Behavior Methods
The entity exposes methods to modify its internal state, implementing encapsulation of business logic.
Update
Used to update basic information of the entity.
- Parameters:
code,name,remark,isDisabled - Logic: Validate parameter validity and update corresponding properties.
AdjustSort
Used to adjust the sort position.
- Parameters:
sort - Logic: Directly update the
Sortproperty. Usually cooperates with Domain Service or Application Service to adjust the order of other entities.
Clone
Used to create a copy of the entity.
- Parameters:
id(new ID),name(new name),sort(new sort) - Logic: Create and return a new
MyEntityNameinstance, copyingCode,Remark, andIsDisabledproperties from the current instance.
Section sources
Field Validation and Constraints
The entity uses the Check utility class (Volo.Abp.Check) to enforce invariants. Validation occurs in constructors and methods that modify state.
- Non-Null/Empty Check:
Check.NotNullOrWhiteSpaceensuresCodeandNameare not empty. - Length Check: The third parameter limits the maximum length of the string, referencing constants defined in
MyEntityNameConsts.
Section sources
Domain Events and Optimistic Concurrency Control
Although explicit domain event publishing code is not shown in the current file snippet, as an Aggregate Root, MyEntityName can publish domain events (e.g., MyEntityNameUpdated) via AddDistributedEvent when status changes.
Inheriting from AggregateRoot automatically includes the ConcurrencyStamp property. When updating an entity, the framework checks if this stamp matches the database version to prevent lost updates in concurrent scenarios.
Section sources
Entity Lifecycle Diagram
Diagram sources
Summary
The MyEntityName class is a standard implementation of a DDD Aggregate Root. It ensures data consistency and business rule enforcement through strict access control, rigorous constructor validation, and explicit business behavior methods. Inheriting from the ABP framework's base class provides it with robust infrastructure capabilities such as auditing, soft delete, and concurrency control, making it a reliable core for domain logic.