Skip to main content
Version: Next

Aggregate Root Entity

Files Referenced in this Document

Table of Contents​

  1. Introduction
  2. Inheritance and Audit Interface
  3. Properties and Access Control
  4. Constructor and Factory Method
  5. Business Behavior Methods
  6. Field Validation and Constraints
  7. Domain Events and Optimistic Concurrency Control
  8. Entity Lifecycle Diagram
  9. 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 Guid as the primary key type.
  • Full Audit Functionality: Automatically tracking CreationTime, CreatorId, LastModificationTime, LastModifierId, IsDeleted, DeleterId, and DeletionTime.
  • Soft Delete: Data is not physically deleted but marked as deleted via the IsDeleted property.
  • Optimistic Concurrency Control: Built-in ConcurrencyStamp property 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 NameTypeAccess ControlConstraintDescription
Codestringprotected setNot Null, Max Length 64Unique Business Code
Namestringprotected setNot Null, Max Length 64Display Name
Sortintprotected setNoneSort Index
Remarkstringprotected setMax Length 256Description/Remark
IsDisabledbool?protected setNoneEnable/Disable Status

Section sources

Constructor and Factory Method​

The entity provides two constructors:

  1. Protected Parameterless Constructor: For ORM framework reflection and deserialization.
  2. 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 Sort property. 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 MyEntityName instance, copying Code, Remark, and IsDisabled properties 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.NotNullOrWhiteSpace ensures Code and Name are 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.