ABP Starter Series (4)--Domain layer definition warehousing and implementation

Source: Internet
Author: User
Warehousing (Repository): warehousing is used to manipulate databases for data access. Warehousing interfaces are defined in the domain layer, and the implementation class for warehousing should be written on the infrastructure layer.

In ABP, the warehousing class implements the IRepository interface, and the interface defines common additions and deletions and aggregation methods, including synchronous and asynchronous methods. The main methods include the following:


ABP gives the interface a default implementation for different ORM frameworks;
For EntityFramework, the implementation of the generic version of Efrepositorybase<tdbcontext, TEntity, tprimarykey> is provided.
For NHibernate, the implementation of the generic version of Nhrepositorybase<tentity, Tprimarykey> is provided.

The implementation of the generic version means that, most of the time, these methods are sufficient to meet the needs of the general entity. If these methods are sufficient for the entity, we do not need to create the warehousing interfaces/classes required by this entity.

Directly by defining a warehousing reference at the Application service layer, and then injecting it through the constructor function. In our application services tier, you can use task warehousing in the following ways:

public class Taskappservice:itaskappservice {private readonly irepository<task> _taskrepository, public Taskapps Ervice (irepository<task> taskrepository) {     _taskrepository = taskrepository;}

This is how it is used in the sample code

Second, how to implement custom warehousing

Suppose we need to find out which tasks a user is assigning.

At the domain level, create the Irepositories folder, and then define the ibackendtaskrepository.

namespace learningmpaabp.irepositories{///<summary>///Custom Warehousing example///</summary>public interface ibackendtaskrepository:irepository<task>{//<summary>///To    get what tasks a user has assigned///    </ summary>//    <param name= "personId" > User id</param>//    <returns> Task List </returns>    list<task> Gettaskbyassignedpersonid (long PersonId);}}

At the infrastructure level, implement the warehousing.

Namespace Learningmpaabp.entityframework.repositories{public class Backendtaskrepository: learningmpaabprepositorybase<task>,ibackendtaskrepository{Public   Backendtaskrepository ( Idbcontextprovider<learningmpaabpdbcontext> dbcontextprovider): Base (Dbcontextprovider)    {    }    // /<summary>///To    get 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 ();}}}    

The warehousing implementation inherits from the Learningmpaabprepositorybase generic abstract class generated by the template, and then implements the Ibackendtaskrepository interface. Here to show the argument constructor of the declaration implementation class, use the generic Idbcontextprovider to pass the subclass of the database context Chargestationcontext to the constructor of the parent class.


Third, the storage of precautions

In the warehousing method, the ABP automatically opens and closes the database connection.

When the warehousing method is called, the database connection is automatically turned on and the transaction is started.

When the warehousing method calls another method of warehousing, they are actually sharing the same database connection and transaction.

Warehousing objects are transient because the IRepository interface inherits from the Itransientdependency interface by default. Therefore, the storage object is automatically created by the IOC container only when it needs to be injected.

The default generic warehousing can meet most of our needs. Custom warehousing is created only if it is not satisfied.

Source has been uploaded to GITHUB-LEARNINGMPAABP, can be self-reference.
ABP Starter Series Catalogue-A practical walkthrough of learning ABP Framework

The above is the ABP Primer series (4)-The domain layer defines the warehousing and implementation of content, more relevant content please follow topic.alibabacloud.com (www.php.cn)!

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