(3) (warehouse Repository in the domain layer), abprepository

Source: Internet
Author: User

(3) (warehouse Repository in the domain layer), abprepository

I. warehousing definition: "in the mediation of the domain layer and the data ing layer, domain objects are accessed using interfaces similar to a set"
(Martin Fowler ).


Warehousing 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.

public UserAppService(            IRepository<User, long> repository,             UserManager userManager,             IRepository<Role> roleRepository,             RoleManager roleManager)            : base(repository)        {            _userManager = userManager;            _roleRepository = roleRepository;            _roleManager = roleManager;        }

The preceding is a service at the application layer. In the "ABP", the storage class must be implemented.IRepositoryInterface,

In most cases, 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. Therefore, in most cases, we can implement IRepository like the above userappservice to meet data access needs.

2. Assume that IRepository does not have the method we need. Custom warehousing

1. At the domain layer (under core), create the IRepositories folder and define the IBackendTaskRepository class.

Using Abp. domain. repositories; using System. collections. generic; using Task = LearningMpaAbp. tasks. task; namespace LearningMpaAbp. IRepositories {// <summary> // custom storage example /// </summary> public interface IBackendTaskRepository: IRepository <Task >{/// <summary> /// obtain the tasks assigned by a user /// </summary> /// <param name = "personId"> User ID </param> // <returns> Task List </returns> List <Task> GetTaskByAssignedPersonId (long personId );}}

2. AddBackendTaskRepository class.

Using Abp. entityFramework; using LearningMpaAbp. IRepositories; using System. collections. generic; using System. linq; using Task = LearningMpaAbp. tasks. task; namespace LearningMpaAbp. entityFramework. repositories {public class BackendTaskRepository: Role <Task>, IBackendTaskRepository {public BackendTaskRepository (IDbContextProvider <LearningMpaAbpDbContext> dbContextProvider): base (dbContextProvider) {}/// <summary> /// obtain the tasks assigned by a user /// </summary> /// <param name = "personId"> User ID </param >/// <returns> Task List </returns> public List <Task> GetTaskByAssignedPersonId (long personId) {var query = GetAll (); if (personId> 0) {query = query. where (t => t. assignedPersonId = personId);} return query. toList ();}}}
This warehousing implementation inherits LearningMpaAbpRepositoryBaseGeneric abstract class, and then implement IBackendTaskRepositoryInterface. 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. Iii. Knowledge about warehousing
  • When the database connection is enabled or disabled, the TTL automatically performs connection management in the warehousing method.
  • When the warehousing method is called, the database connection is automatically started and the transaction starts.
  • When the warehousing method calls another warehousing method, they actually share the same database connection and transaction.
  • The storage objects are temporary because the IRepository interface inherits from the ITransientDependency interface by default. Therefore, the Ioc container automatically creates a new instance only when the object needs to be injected.
  • By default, generic warehousing can meet most of our needs. A custom warehouse is created only when it is not met.

Above...
Reference http://www.jianshu.com/p/6e90a94aeba4

  

 

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.