To implement the IRepository interface, the storage class defines common addition, deletion, modification, query, and aggregation methods, including synchronous and asynchronous methods. It mainly includes the following methods: Warehouse (Repository): Warehouse is used to operate databases for data access. The warehousing interface is defined at the domain layer, and the warehousing implementation class should be written at the infrastructure layer.
To implement the IRepository interface, the storage class defines common addition, deletion, modification, query, and aggregation methods, including synchronous and asynchronous methods. It mainly includes the following methods:
The NhRepositoryBase The implementation method of the generic version.
The implementation of generic versions means that most of the time, these methods are sufficient to meet the needs of general entities. If these methods are sufficient for an object, we do not need to create the warehousing interface/class required by the object.
You can define a repository reference at the application service layer and inject it through the constructor. On our application service layer, you can use the Task warehouse as follows:
public class TaskAppService : ITaskAppService { private readonly IRepository
_taskRepository; public TaskAppService(IRepository
taskRepository) { _taskRepository = taskRepository; }
This method is used in the sample code.
II. how to implement custom warehousing
Suppose we need to find the tasks assigned by a user.
At the domain layer, create the IRepositories folder and define IBackendTaskRepository.
Namespace LearningMpaAbp. IRepositories {////// Custom warehousing example ///Public interface IBackendTaskRepository: IRepository
{///
/// Obtain the tasks assigned by a user //////
User ID///
Task list
List
GetTaskByAssignedPersonId (long personId );}}
This storage is implemented at the infrastructure layer.
Namespace LearningMpaAbp. EntityFramework. Repositories {public class BackendTaskRepository: LearningMpaAbpRepositoryBase
, IBackendTaskRepository {public BackendTaskRepository (IDbContextProvider
DbContextProvider): base (dbContextProvider ){}///
/// Obtain the tasks assigned by a user //////
User ID///
Task list
Public List
GetTaskByAssignedPersonId (long personId) {var query = GetAll (); if (personId> 0) {query = query. where (t => t. assignedPersonId = personId);} return query. toList ();}}}
This warehousing implementation inherits the LearningMpaAbpRepositoryBase generic abstract class generated by the template, and then implements the IBackendTaskRepository interface. The declared implementation class has a constructor. use the generic IDbContextProvider to pass the ChargeStationContext subclass of the database context to the constructor of the parent class.
A series of open-source directories-practical drills for learning the ABP framework
The above is the content of the ABC getting started series (4)-definition warehousing and implementation at the domain layer. For more information, see The PHP Chinese website (www.php1.cn )!