Terms:First of all, we must understand the domain-driven (DDD) knowledge. At least we must be familiar with some terms.Source codeAnnotations contain many of these terms. For exampleWarehousing,Warehouse Context,Warehouse Root,Domain objectAnd what fields can be considered as the storage root. Here I will give a brief introduction based on my understanding. Forgive us for any mistakes. Comparing DDD is not something that can be understood overnight. It requires continuous integration of theory and practice. Here I am just a simple DDD.
1. domain objects:Is an object in our business. I don't need to explain this.
2. Warehouse root:It is the domain object that can be directly operated on the database. It is a subset of the domain object. All operations in the business are operations on the warehouse root. Only the warehouse root can be put into the warehouse and then solidified into the database. In different businesses, the warehouse root is different. In order, the order object must be the warehouse root, but the order address is not. The order address only exists as the order property. In our framework, we use this taggregateroot excuse
3. warehousing:It is actually the place where domain objects are stored. Each domain object has its own storage. Many objects of the same type are stored.
4. warehousing context:Manage the warehouse where the warehouse cannot be stored.
I will not introduce more terms one by one. I will only introduce more terms I use. We recommend that you learn more about this field.
I. Overview of irepository and repositor
As the name implies, repository is a repository. repository inherits the irepository interface and implements a set of common methods for operating the repository root. Through these methods, we can flexibly operate databases. Repository is only the basic storage. As for the specific data solidification and query through EF or NH. We only need to inherit the repository class. Repository is an abstract class. It can only be inherited. It is up to us to implement various types of warehousing, whether to operate data with EF or NH. Irepository provides a set of standards for warehousing. I chose EF here, which will be detailed later.
1. Basic storage standards of irepository
1 /* **************************************** ************ 2 * Author: egojit 3 * Date: 2012-7-13 4 * Description: Warehousing interface. 5 **************************************** *********** */ 6 Using System; 7 Using System. Collections. Generic; 8 Using System. LINQ. expressions; 9 Using Egojitframework. domain. specifications; 10 11 12 Namespace Egojitframework. domain. Repositories 13 { 14 /// <Summary> 15 /// Warehousing Interface 16 /// </Summary> 17 /// <Typeparam name = "taggregateroot"> Warehouse root type </Typeparam> 18 Public Interface Irepository <taggregateroot> 19 Where Taggregateroot: Class , Iaggregateroot 20 { 21 /// <Summary> 22 /// Obtain a warehouse context instance 23 /// </Summary> 24 Irepositorycontext context { Get ;} 25 /// <Summary> 26 /// Add a warehouse root to the warehouse 27 /// </Summary> 28 /// <Param name = "aggregateroot"> Warehouse root. </Param> 29 Void Add (taggregateroot aggregateroot ); 30 /// <Summary> 31 /// Obtain the root instance from the warehouse using the key 32 /// </Summary> 33 /// <Param name = "key"> Key. </Param> 34 /// <Returns> Warehouse root instance </Returns> 35 Taggregateroot getbykey (guid key ); 36 /// <Summary> 37 /// Obtain all the warehouse root instances from the warehouse 38 /// </Summary> 39 /// <Returns> Warehouse root instance list </Returns> 40 Ienumerable <taggregateroot> Getall (); 41 /// <Summary> 42 /// Retrieve all the warehouse root instances from the warehouse by PAGE 43 /// </Summary> 44 /// <Param name = "pagenumber"> Page number </Param> 45 /// <Param name = "pagesize"> Quantity per page </Param> 46 /// <Returns> Specify the page's warehouse root list </Returns> 47 Ienumerable <taggregateroot> getall ( Int Pagenumber, Int Pagesize ); 48 /// <Summary> 49 /// Obtain and sort all the root instances from the warehouse 50 /// </Summary> 51 /// <Param name = "sortpredicate"> The sort predicate which is used for sorting. </Param> 52 /// <Param name = "sortorder"> The <See CREF = "apworks. sortorder"/> Enumeration which specifies the sort order. </Param> 53 /// <Returns> All the aggregate roots got from the repository, with the aggregate roots being sorted 54 /// Using the provided sort predicate and the sort order. </Returns> 55 Ienumerable <taggregateroot> getall (expression <func <taggregateroot, dynamic>Sortpredicate, sortorder ); 56 /// <Summary> 57 /// Retrieve and sort all the warehouse root instances from the warehouse by PAGE 58 /// </Summary> 59 /// <Param name = "sortpredicate"> Sorting Condition </Param> 60 /// <Param name = "sortorder"> Sorting Method </Param> 61 /// <Param name = "pagenumber"> Page number </Param> 62 /// <Param name = "pagesize"> Number of pages per page </Param> 63 /// <Returns> Sorted warehouse root list </Returns> 64 Ienumerable <taggregateroot> getall (expression <func <taggregateroot, dynamic> sortpredicate, sortorder, Int Pagenumber, Int Pagesize ); 65 } 66 }
Of course, this interface is far more than that.Code. For more information, see source code. Slave
/// <Summary>///Warehousing Interface/// </Summary>/// <Typeparam name = "taggregateroot">Warehouse root type</Typeparam>Public InterfaceIrepository <taggregateroot>WhereTaggregateroot:Class, Iaggregateroot
In this line of code, we can easily see that the operation objects in the irepository class are the warehouse root of the basic self-taggregateroot interface, which effectively limits the database operations of common entities.
2. inherit from the repository storage class of the irepository Interface
Some code is as follows:
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using Egojitframework. domain. specifications; Using System. LINQ. expressions; /* **************************************** ************* Author: egojit * Date: 2012-7-13 * description: warehousing abstract class ************************************** ************* */ Namespace Egojitframework. domain. repositories { /// <Summary> /// Warehousing Basics /// </Summary> /// <Typeparam name = "taggregateroot"> The type of the aggregate root on which the repository operations /// Shocould be saved med. </Typeparam> Public Abstract Class Repository <taggregateroot>: irepository <taggregateroot> Where Taggregateroot: Class , Iaggregateroot { # Region Private Field Private Readonly Irepositorycontext context; # Endregion # Region Attribute /// <Summary> /// An instance of the warehousing Context /// </Summary> /// <Param name = "context"> The storage context used by this warehouse </Param> Public Repository (irepositorycontext context ){ This . Context = Context ;} # Endregion # Region Private Method /// <Summary> /// Add a warehouse root to Warehouse /// </Summary> /// <Param name = "aggregateroot"> Added warehouse Root </Param> Protected Abstract Void Doadd (taggregateroot aggregateroot ); /// <Summary> /// Obtain the warehouse root through the key in this warehouse /// </Summary> /// <Param name = "key"> Key </Param> /// <Returns> Warehouse instance </Returns> Protected Abstract Taggregateroot dogetbykey (guid key );...... Saving more code this time }}
Through the code, we can easily know that it is also a repository root and inheritsIrepositoryInterface, and it is abstract and must be inherited for implementation, so that we provide a very easy way to store based on EF, NH, or other implementations, you only need to reproduce the methods in its subclass. We can see that the repository context is brought to this class through the constructor.
This section describes this basic Warehousing Section. This section describes the transaction management in the warehouse. (Phase Source Code download)
Copyright: It is jointly owned by the blog and egojit. For details, refer to the source.