Basic CRUD Operations
Files Referenced in this Document
IMyEntityNameAppService.csMyEntityNameAppService.csMyEntityNameCreateDto.csMyEntityNameUpdateDto.csMyEntityNameGetListInput.cs
Table of Contents
- Introduction
- Create (CreateAsync)
- Read (GetAsync & GetListAsync)
- Update (UpdateAsync)
- Delete (DeleteAsync)
- Best Practices
Introduction
Basic CRUD (Create, Read, Update, Delete) operations are the foundation of data management in the CMS plugin. Implemented via MyEntityNameAppService, these operations follow the ABP framework's standard patterns, ensuring consistency, validation, and authorization.
Create (CreateAsync)
Method Signature: Task<MyEntityNameDto> CreateAsync(MyEntityNameCreateDto input)
Description: Creates a new MyEntityName entity in the database.
Process:
- Accepts
MyEntityNameCreateDtocontaining required fields (e.g., Name, Code). - Validates the input (e.g., required fields, max length).
- Maps DTO to Entity.
- Inserts Entity into the database.
- Maps the created Entity back to
MyEntityNameDtoand returns it.
Example Request:
POST /api/app/my-entity-name
{
"name": "New Entity",
"code": "NE-001"
}
Read (GetAsync & GetListAsync)
Get Single Entity
Method Signature: Task<MyEntityNameDto> GetAsync(Guid id)
Description: Retrieves a specific entity by its unique ID. Throws EntityNotFoundException if not found.
Get List
Method Signature: Task<PagedResultDto<MyEntityNameDto>> GetListAsync(MyEntityNameGetListInput input)
Description: Retrieves a paginated list of entities, optionally filtered and sorted.
Process:
- Applies filters from
MyEntityNameGetListInput(e.g., search by name). - Sorts data based on
Sortingparameter. - Applies paging (
SkipCount,MaxResultCount). - Returns a
PagedResultDtocontaining the list of items and total count.
Example Request:
GET /api/app/my-entity-name?Filter=test&Sorting=name%20asc&SkipCount=0&MaxResultCount=10
Update (UpdateAsync)
Method Signature: Task<MyEntityNameDto> UpdateAsync(Guid id, MyEntityNameUpdateDto input)
Description: Updates an existing entity identified by ID.
Process:
- Retrieves entity by ID.
- Validates input.
- Maps fields from
MyEntityNameUpdateDtoto the loaded entity. - Saves changes to the database.
- Returns the updated DTO.
Example Request:
PUT /api/app/my-entity-name/{id}
{
"name": "Updated Name"
}
Delete (DeleteAsync)
Method Signature: Task DeleteAsync(Guid id)
Description: Deletes an entity by ID.
Process:
- Checks if the entity exists.
- Deletes the entity (soft delete if
ISoftDeleteis implemented). - Returns 204 No Content upon success.
Example Request:
DELETE /api/app/my-entity-name/{id}
Best Practices
- DTOs: Always use specific DTOs for each operation (Create, Update) to control which fields can be modified.
- Validation: Use Data Annotations on DTOs for basic validation.
- Authorization: Ensure appropriate permissions are checked before each operation (handled by ABP's
[Authorize]attribute or policy configuration).