ABP (modern ASP. NET template Development Framework) series 10, ABP domain layer-entity

Source: Internet
Author: User

Click here to go to the ABP series articles General Catalogue

DDD-based Modern ASP.--ABP series 10, ABP domain layer-entity

The ABP is "ASP. Boilerplate Project (ASP. NET Template project) "for short.

ABP's official website :http://www.aspnetboilerplate.com

ABP's Open source project on GitHub : https://github.com/aspnetboilerplate

This article is provided by Shenzhen-carl translation

Entity is one of the core concepts of DDD (domain driven design). As Eric Evans describes, "many objects are not defined by their properties, but are defined by a series of continuity events and identities" (reference domain-driven design book).

The Translator notes: objects are not defined by their attributes, but by their linear continuity and identity. Therefore, the entity is an ID that has a unique identity and is stored in the database. Entities are typically mapped to a table in a database.

Solid Class (Entity classes)

In the ABP, the entity inherits from the entity class, see the following example:

 Public class person:entity{    publicvirtualstringgetset;}      Public Virtual Get Set ; }      Public Task ()    {        = datetime.now;    }}

The person class is defined as an entity. It has two properties, and it has an ID attribute in its parent class. The ID is the primary key for the entity. Therefore, the ID is the primary key for all entities that inherit from the entity class (the primary key for all entities is the ID field).

The Id (primary key) data type can be changed. The default is the Int (int32) type. If you want to define a different type for the ID, you should declare the type of the ID as in the example below.

 Public class person:entity<Long>{    publicvirtualstringget Set ; }      Public Virtual Get Set ; }      Public Task ()    {        = datetime.now;    }}

You can set it to string,guid or other data types.

The entity class overrides the equality (= =) operator to determine whether two entity objects are equal (the IDs of two entities are equal). A istransient () method is also defined to detect if an entity has an id attribute.

Interface conventions

In many applications, many entities have properties like CreationTime (the database table also has this field) to indicate when the entity was created. The APB provides a number of useful interfaces to implement these similar functions. That is, for these entities that implement these interfaces, a common encoding is provided (in layman's words, the specified functionality can be implemented as long as the specified interface is implemented).

(1) Audit (Auditing)

An entity class implements the Ihascreationtime interface to have creationtime properties. When the entity is inserted into the database, the ABP automatically sets the value of the property to the current time.

 Public Interface ihascreationtime{    getset;}}

The person class can be overridden to implement the Ihascreationtime interface as in the following example:

 Public class person:entity<Long>, ihascreationtime{    publicvirtualstring  getset;}      Public Virtual Get Set ; }      Public Task ()    {        = datetime.now;    }}

Icreationaudited is extended from Ihascreationtime and the interface has property Creatoruserid:

 Public Interface icreationaudited:ihascreationtime{    longgetset;}}

When a new entity is saved, the ABP automatically sets the Creatoruserid property value to the current user's ID

You can easily implement the icreationaudited interface by deriving from the entity class Creationauditedentity (because the class already implements the Icreationaudited interface, We can directly inherit the Creationauditedentity class to achieve the above function). It has a generic version that implements different ID data types (by default, int), which can be assigned different data types for IDs (IDs in the entity Class).
The following is an interface for implementing a similar modification function

 Public Interface imodificationaudited{    DateTimegetset;}     Long Get Set ; }}

When an entity is updated, the APB automatically sets the values for those properties. You only need to implement these attributes within your entity class.

If you want to implement all of the audit properties, you can extend the Iaudited interface directly, as shown in the following example:

 Public Interface iaudited:icreationaudited, imodificationaudited{        }

As a rapid development method, you can derive directly from the Auditedentity class, no need to implement the Iaudited interface (Auditedentity class has implemented this function, directly inherit the class can achieve the above functions), auditedentity class has a generic version that implements different ID data types (by default, int), you can assign different data types to IDs (IDs in the entity Class).

(2) Soft Delete (Soft delete)

Soft delete is a generic pattern that is used to mark an entity that has been deleted, rather than actually deleting records from the database. For example: You might not want to hard delete a user record from the database because it is associated with many other tables. In order to achieve the purpose of soft deletion we can implement the interface Isoftdelete:

 Public Interface isoftdelete{    boolgetset;}}

ABP enables out-of-the-box soft-delete mode. When an entity that implements a soft delete is being deleted, the ABP perceives the action and prevents it from being deleted, setting the IsDeleted property value to True and updating the entities in the database. In other words, a soft-deleted record cannot be retrieved from the database, and the ABP automatically filters the soft-deleted records for us. (For example: Select query, which is referred to through the ABP query, not through query parser in the database.) )

If you use a soft delete, you may also want to implement this function, that is, to record who deleted the entity. To implement this feature you can implement the Ideletionaudited interface, see the following example:

 Public Interface ideletionaudited:isoftdelete{    longgetset;}    DateTimegetset;}}

As you can see, the ideletionaudited extends from the Isoftdelete interface. When an entity is deleted, the ABP automatically sets values for these properties.
If you want to extend all the auditing interfaces (for example: Create (creation), modify (modification), and delete (deletion)) for the entity class, you can implement the Ifullaudited interface directly, as the interface has inherited these interfaces, see the following example:

 Public Interface ifullaudited:iaudited, ideletionaudited{        }

As a shortcut, you can derive your entity class directly from the Fullauditedentity class, because the class already implements the Ifullaudited interface.

Note: All audit interfaces and classes have a generic template in order to navigate the definition properties to your user entities (such as:icreationaudited<tuser> and Fullauditedentity<tprimarykey, TUser >), here Tuser refers to the types of entity classes that are created, modified, and deleted by the user, see source code (Fullauditedentity<tprimarykey under Abp.Domain.Entities.Auditing space, for details), Tuser> Class), Tprimarykey only is the entity base class ID type, which is int by default.

(3) Active status/Idle status (active/passive)

Some entities need to be marked as active or idle. Then you can take the action of Active/passive state for the entity. Entities created for this reason, you can extend the Ipassivable interface to implement this functionality. This interface defines the properties of the IsActive.

If the entity you created for the first time is marked as active, you can set the IsActive property value to True in the constructor.

This is different from soft deletion (IsDeleted). If the entity is soft deleted, it cannot be retrieved from the database (the ABP has filtered the soft-deleted record). But for entities that are active/inactive, you depend entirely on how you get the tagged entities.

IEntity interface

In fact, entity implements the IEntity interface (and entity<tprimarykey> implements the Ientity<tprimarykey> interface). If you do not want to derive from the entity class, you can implement these interfaces directly. Other entity classes can also implement the appropriate interfaces. But it is not recommended that you use this method. Unless you have a good reason not to derive from the entity class.

I hope that more domestic architects will be able to focus on the ABP project, and perhaps it will help you, perhaps with your participation, this project can develop better.

Welcome to add ABP Architecture Design Exchange QQ Group: 134710707

Click here to go to the ABP series articles General Catalogue

ABP (modern ASP. NET template Development Framework) series 10, ABP domain layer-entity

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.