Skip to main content
Version: Next

Sort Adjustment

Files Referenced in this Document

Table of Contents​

  1. Introduction
  2. Role of Entity Sort Field
  3. Sort Adjustment Method Implementation Principle
  4. Data Competition and Response Strategies in Concurrent Environments
  5. Frontend Call Suggestions and Batch Submission Optimization
  6. Conclusion

Introduction​

This document details the implementation mechanism of the MyEntityName entity sorting function in the system. It focuses on analyzing the role of the Sort field in list display, parsing how the AdjustSortAsync method receives the target entity ID and target sort value, recalculates the sort values of related entities, and ensures the continuity and uniqueness of sorting. It also discusses potential data competition issues in high concurrency scenarios and response strategies, and provides optimization call suggestions for frontend drag-and-drop sorting.

Section sources

Role of Entity Sort Field​

The Sort field (type int) in the MyEntityName entity is used to define the display order of the entity in the list. This field is mapped to the sort column in the database table and serves as the default sorting basis to ensure a stable order when the list is displayed.

In the query interface GetListAsync, if no sorting rule is specified, it defaults to ascending order by the Sort field, ensuring that list items in the user interface are always displayed in the preset order. The continuity and uniqueness of this field are maintained by business logic to avoid duplicate or missing sort values.

Diagram sources

Section sources

Sort Adjustment Method Implementation Principle​

The AdjustSortAsync method is the core logic for implementing sort adjustment, located in the MyEntityNameAppService class. Its workflow is as follows:

  1. Get All Entity List: First, get all MyEntityName entities from the repository and arrange them in ascending order by the current Sort value.
  2. Initialize Continuous Sorting: Traverse the list and assign a continuous sequence number starting from 1 to each entity to ensure the continuity of sort values.
  3. Locate Target Entity: Find the target entity based on the passed id.
  4. Adjust Sort Range:
    • If the target sort is 1, the sort values of all other entities are increased by 1 overall.
    • If the original sort is greater than the target sort (move up), entities located at the target position and above (excluding itself) have their sort +1, others -1.
    • If the original sort is less than the target sort (move down), entities located above the target position (excluding itself) have their sort +1, others -1.
  5. Set Target Sort Value: Set the Sort of the target entity to the specified value.
  6. Batch Update Database: Call UpdateManyAsync to submit all entity sort changes at once.

This algorithm ensures the continuity (no gaps) and uniqueness (no duplicates) of sorting, and reduces database interactions through batch updates.

Diagram sources

Section sources

Data Competition and Response Strategies in Concurrent Environments​

In high concurrency scenarios, multiple users adjusting sorting simultaneously may cause data competition problems. Typical scenarios are as follows:

  • Users A and B load the same list simultaneously
  • User A moves item 1 to position 3
  • User B moves item 2 to position 3
  • Since both calculate sorting based on old data, the final result may contain duplicate sort values or disordered sequence

Response Strategies​

Use FOR UPDATE to lock relevant records during GetListAsync query to prevent other transactions from reading uncommitted data. EF Core can implement this via ExecuteSqlRaw or custom Dapper queries.

2. Application Layer Distributed Lock​

Use middleware like Redis to implement distributed locks. Use the entity collection as the lock key and lock during the execution of AdjustSortAsync to ensure that only one request can modify sorting at the same time.

3. Optimistic Concurrency Control​

Introduce a ConcurrencyStamp field (already exists) in the MyEntityName entity and verify the version number upon each update. If the version is inconsistent, throw an exception and prompt the user to refresh and retry.

The current implementation does not explicitly handle concurrency. It is recommended to supplement any of the above locking mechanisms in high concurrency scenarios.

Section sources

Frontend Call Suggestions and Batch Submission Optimization​

To improve user experience and system performance, the frontend is advised to adopt the following strategies:

Drag-and-Drop Sorting Optimization Scheme​

  1. Local Preview: When the user drags, the frontend updates the sorting locally and renders it in real-time to provide immediate feedback.
  2. Delayed Submission: Set a debounce mechanism (e.g., 500ms) to avoid frequent requests.
  3. Batch Submission: If multi-item sorting is supported, collect all changes and call the batch interface at once (can extend AdjustSortAsync to support batching).
  4. Error Retry: If it fails due to concurrency conflict, prompt the user "Data has changed, please refresh and retry".

Interface Extension Suggestions​

Can add a batch adjustment interface:

Task AdjustSortBatchAsync(List<(Guid id, int sort)> items);

Reduce network round trips and improve performance.

Section sources

Conclusion​

The sorting function of the MyEntityName entity is implemented through the Sort field, and the AdjustSortAsync method ensures the continuity and uniqueness of sorting. The current implementation logic is clear, but there is a risk of data competition in high concurrency scenarios. It is recommended to introduce database row lock or distributed lock mechanisms. The frontend should adopt debounce and batch submission strategies to optimize user experience. Future extensions can include batch interfaces to further improve performance.