Address: http://www.cnblogs.com/rdst/archive/2012/08/12/2634159.html
Series Overview:The full series will detail the establishment of three layers of the abstract factory, as well as EF advanced applications and simple ASP. NET mvc3.0 applications. The technologies used include EF, lambda, LINQ, interface, and T4. Because many technical concepts are introduced online, the basic concepts are not described in this project.
Series 2 Overview:This series details how to abstract the common method (crud) and the application of T4 templates.
1. Create cnblogs. rdst. idaoProgramSet1.1 create an interface folder in the solution to store the abstract interface 1.2 Add the Assembly named cnblogs. rdst. idao to the interface folder. 1.3 Add the domain assembly and system. Data. entity Assembly created in reference to Series 1 2. Basic interfaces of the abstract data access layer 2.1 create the ibasedao interface in the just-created cnblogs. rdst. idao Program 2.2 define common crud methods in ibasedao
1 Using System; 2 Using System. Collections. Generic; 3 Using System. LINQ; 4 Using System. text; 5 6 Namespace Cnblogs. rdst. idao 7 { 8 Public Interface Ibasedao <t> 9 Where T: Class , 10 New () // The constraint T type must be instantiated. 11 { 12 // Returns an object set based on conditions. 13 Iqueryable <t> loadentites (func <t, Bool > Wherelambda ); 14 15 // Retrieve object set pagination based on conditions 16 Iqueryable <t> loadentites (func <t, Bool > Wherelam.pdf,Int Pageindex, Int Pagesize, Out Int Totalcount ); 17 18 // Add 19 T addentity (T entity ); 20 21 // Update 22 T updateentity (T entity ); 23 24 // Delete 25 Bool Delentity (T entity ); 26 27 // Delete based on conditions 28 Bool Delentitybywhere (func <t, Bool > Wherelambda ); 29 } 30 }
The crud method in the basic interface is defined. Next, we need to use the T4 template to generate all object class interfaces and implement the ibasedao interface.
3. generate all object class interfaces
3.1 Add a T4 Text Template named idaoext
3.2 Add the following to the template:CodeWhere the comments need to be changed according to their respective projects
<# @ Template Language = "C #" DEBUG = "False" Hostspecific = "True" #>< # @ Include file = "Ef. Utility. cs. ttinclude" #>< # @ Output extension = ". Cs" #>< # Codegenerationtools code = New Codegenerationtools ( This ); Metadataloader loader = New Metadataloader (This ); Coderegion region = New Coderegion ( This , 1); metadatatools EF = New Metadatatools ( This ); String Inputfile = @ ".. \ Cnblogs. rdst. domain \ model. edmx" ; // Specify the path of the edmx Object Model Edmitemcollection itemcollection = loader. createedmitemcollection (inputfile ); String Namespacename = code. vsnamespacesuggestion (); entityframeworktemplatefilemanager filemanager = entityframeworktemplatefilemanager. Create ( This ); #> Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using Cnblogs. rdst. domain; // Reference the domain namespace Namespace Cnblogs. rdst. idao // The namespace of the object class Interface {<# Foreach (Entitytype entity In Itemcollection. getitems <entitytype> (). orderby (E => E. Name )) // Facilitates the ing of Entity objects in the edmx Model {#> Public Interface I <# = entity. name #> Dao: ibasedao <# = entity. name #> // Object generation Interface {}<#};#>}
3.3 After the T4 template is edited, press Ctrl + S to save the template and check whether the template is running. Click OK. In this case, all object class interfaces are automatically generated and the ibasedao interface is implemented. The corresponding crud method definition is also available.
The cainiao-level three-layer framework (EF + MVC) project is an example of how the implementation class implements the methods defined in the basic interface.