& Lt; ABP framework & gt; entity, abp framework entity

Source: Internet
Author: User

<ABP framework> entity: An ABC framework entity

Document directory


Content of this section:

  • Entity class
  • Aggregate root class
    • Domain events
  • Agreed Interface
    • Audit
    • Soft Delete
    • Active/passive entities
  • Entity change event
  • IEntity Interface


Entity is a core concept of DDD. Eric Evans described it as follows: "An object is not defined by its characteristics, but by the continuity and identity of a thread ". Therefore, an object has an id attribute stored in the database. An object is usually mapped to a table in a relational database.


Entity class

The Entity inherits from the Entity class. The sample code is as follows:

public class Person : Entity{    public virtual string Name { get; set; }    public virtual DateTime CreationTime { get; set; }    public Person()    {        CreationTime = DateTime.Now;    }}

The Person class is defined as an object. It has two attributes, and the Entity class defines an Id attribute, which is the primary key of the object. Therefore, all object primary key names are the same and are IDs.


The Id (primary key) type can be changed. The default value is int (Int32 ). If you want to define an Id as another type, you should explicitly declare it, as shown below:

public class Person : Entity<long>{    public virtual string Name { get; set; }    public virtual DateTime CreationTime { get; set; }    public Person()    {        CreationTime = DateTime.Now;    }}

Similarly, you can set it to string, Guid or another type.

The Entity class overrides the equality operator (=), which can be used to easily check whether two entities are equal (whether their IDs are equal) and define IsTransient () method to check whether the object has an Id.


Aggregate root class

"Aggregation is a mode in DDD, and a DDD aggregation is a domain object group, which can be created by individual units. For example, an order and its items can be separated objects, but it is useful to regard the order (together with its items) as a separate aggregation. "(Martin Fowler to view the complete description ).

Although you are not forced to use an aggregation, you may want to create an aggregation and aggregation root in your application. ABC extends Entity, defines the AggregateRoot class, and creates an aggregate root Entity for an aggregate.


Domain events

AggregateRoot defines the DomainEvents set and generates domain events by aggregating the root object. These events are automatically triggered before the current work unit is completed. In essence, any entity can generate domain events by implementing the IGeneratesDomainEvents interface, but usually (best practices) generate domain events in the aggregation root, this is why it is defaulted to AggregateRoot, rather than the Entity class.


Agreed Interface

In many applications, there are many similar object attributes (database table fields), such as the CreationTime when an object is created, and ABP provides some useful interfaces, defining and displaying these general attributes also provides a common way for the entities implementing these interfaces to compile these property codes.



IHasCreationTime refers to the "Creation Time" of an object. It adopts common attributes. Before an object is inserted to a database, the "Creation Time" attribute is set to the current time for the object that implements this interface.

public interface IHasCreationTime{    DateTime CreationTime { get; set; }}

Rewrite the Person class to implement the IHasCreationTime interface, as shown below:

public class Person : Entity<long>, IHasCreationTime{    public virtual string Name { get; set; }    public virtual DateTime CreationTime { get; set; }    public Person()    {        CreationTime = DateTime.Now;    }}

ICreationAudited extends IhasCreationTime by adding CreatorUserId:

public interface ICreationAudited : IHasCreationTime{    long? CreatorUserId { get; set; }}

When a new object is saved, the ABC automatically sets CreatorUserId to the id of the current user. You can also let your class inherit the CreationAuditedEntity class to implement ICreationAudited. It also has a generic version that applies to different types of Id attributes.

There is also a similar "modify" Interface

public interface IHasModificationTime{    DateTime? LastModificationTime { get; set; }}public interface IModificationAudited : IHasModificationTime{    long? LastModifierUserId { get; set; }}

When an object is updated, these attributes are also automatically set by the ABP. You only need to define them for your class.

If you want to implement all audit attributes, you can directly implement the IAudited interface:

public interface IAudited : ICreationAudited, IModificationAudited{}

You can inherit the AuditedEntity class to implement IAudited directly. The AuditiedEntity class also has a generic version that applies to different types of Id attributes.

Note: You can obtain the user ID from the ABP session.


Soft Delete

Soft Delete is a common mode. It marks an object as "deleted" instead of deleting it directly from the database. For example, if you do not want to hard delete a User from the database, because it may be associated with other tables, the ISoftDelete interface is for this purpose:

public interface ISoftDelete{    bool IsDeleted { get; set; }}

The out-of-the-box soft deletion mode is implemented by the ABP. When a soft Delete object starts to be deleted, the ABP detects it to prevent it from being deleted, sets IsDeleted to true, and updates the object to the database. At the same time, the ABP will not obtain soft-deleted entities from the database (select) and will automatically filter them.

If you use soft Delete, when soft delete an object, you may also want to save who deleted it and when to delete it, You can implement the IDeletionAudited interface, as shown below:

public interface IDeletionAudited : ISoftDelete{    long? DeleterUserId { get; set; }    DateTime? DeletionTime { get; set; }}

More quickly: You can inherit your entity from all FullAuditedEntity classes that have been implemented.

  • Note 1: All audit interfaces and classes have a generic version (such as ICreationAudited <TUser> and FullAuditedEntity <TPrimaryKey, TUser> ).
  • NOTE 2: Both of them have an AggregateRoot version, such as AuditedAggregateRoot.


Active/passive entities

Some entities need to be marked as Active and Passive, and then you take action based on the Active/Passive state of the entity. You can implement IPassivable for this purpose, which defines the IsActive attribute.

If your entity is active when it is created, you can set IsActive to true in the constructor.

This is different from the soft Delete (IsDeleted). If an object is soft-deleted, it cannot be obtained from the database (it is blocked by default by ABP ), however, whether or not to obtain active/negative entities depends on you.


Entity change event

When an object is inserted, updated, or deleted, the ABP automatically triggers some events, so you can register these events to execute any logic you need. View the predefined event topic of the event bus document for more information.


IEntity Interface

Essentially, the Entity class implements the IEntity interface (and the Entity <TPrimaryKey> implements IEntity <TPrimaryKey> ). If you do not want to inherit from the Entity class, you can directly implement these interfaces. These interfaces are also applicable to other Entity classes, but they are not recommended unless you have a good reason.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.