The cainiao-level three-layer framework (EF + MVC) project is a series of two abstract data access layers.

Source: Internet
Author: User
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 a Cnblogs. Rdst. IDAO assembly1.1 create an Interface folder in the solution to store the abstract Interface. 1.2 add Cnblogs in the Interface folder. rdst. in IDAO Assembly 1.3, add the Domain assembly and System created in Reference Series 1. data. entity assembly2. Basic interfaces of the abstract data access layer2.1 create an 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 // obtain the object set 13 IQueryable according to the condition <T> LoadEntites (Func <T, bool> wherelamtes ); 14 15 // obtain the object set page Based on the condition 16 IQueryable <T> LoadEntites (Func <T, bool> wherelamtes, 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 28 bool DelEntityByWhere (Func <T, bool> whereLambda) according to the condition; 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 paste the following code into the template. The comments must be changed according to the project.

 

<# @ 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 "; // specifies 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 Domain namespace Cnblogs. rdst. IDAO // namespace where the object class interface is located {<# foreach (EntityType entity in ItemCollection. getItems <EntityType> (). orderBy (e => e. name) // facilitates the ing of object 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.

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.