ABC getting started series (4) -- define warehousing at The Domain layer and implement-C #. Net Tutorial

Source: Internet
Author: User
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 )!

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.