ASP. net mvc + EF framework + EasyUI implement permission management series (9)-TT template Learning

Source: Internet
Author: User

We have basically completed a Demo. The following permissions are implemented based on the Demo idea. We only need to add things to the underlying layer and then implement the business, the Permission Logic business is a little complicated, so you can first look at the permission business in the blog Park, of course, I will also talk about it, today, we will discuss how to use TT templates in this project. If you have learned TT templates, You can greatly reduce the amount of writing your code, of course, I am also working on the TT template. Therefore, this article is based on what we will continue to write in the future. Next we will start to learn how to use the TT template. Thank you for your consideration.

1. Why the T4 template is required?

(1) when the project is written to the beginning of article 7, we will feel that way. If we have a lot of database entities, and our database access interface layer (LYZJ. userLimitMVC. IDAL) has many interfaces, and each object corresponds to a RoleRepository (warehouse) and UserInfoRepository (warehouse). The key point is that the content of each object warehouse is basically the same, what should we do at this time? If every one is written, the project is large (if there are thousands of tables, thousands of physical interfaces will be written, and it cannot be imagined what the project is), and the database access layer (LYZJ. userLimitMVC. IDAL.

(2) The text above illustrates a problem. How can we enable the system to automatically generate these things for us, at this time, we thought of the Code Generator (TT) template.

(3) Since it is a Code Generator template, We need to write the template by ourselves. As long as we write the template well, other things will help us automatically generate the template.

(4) We will introduce the Microsoft standard code generation template, that is, the TT template.

2. Create a T4 Template

(1) At the database access layer (LYZJ. userLimitMVC. DAL) Right-click to add the new item, and then select the Text Template option below the general (in Entity FrameWork5.0, the TT template is used to help us generate some identical classes), named MyFirst. TT, that is, the creation of my first TT template, as shown in:

(2) After creating the TT template, we first see a Reference Code. The Code is as follows:


1 <# @ template debug = "false" hostspecific = "false" language = "C #" #>
2
3 <# @ assembly name = "System. Core" #>
4
5 <# @ import namespace = "System. Linq" #>
6
7 <# @ import namespace = "System. Text" #>
8
9 <# @ import namespace = "System. Collections. Generic" #>
10
11 <# @ output extension = ". txt "#> change txt to cs (3). At this time, we can see the language =" C # "in the first line, indicating that the current programming language is C #, the output in the last line indicates an output object, and extension indicates the output suffix. Here we want to output a class file, so change txt to cs.

3. Simple small cases of T4 templates

(1) When we know the usefulness of TT templates, do we urgently want to know how to compile TT templates as a beginner, below I will write a loop template to show you the magic. The Code is as follows:


1 <# @ template debug = "false" hostspecific = "false" language = "C #" #>
2
3 <# @ assembly name = "System. Core" #>
4
5 <# @ import namespace = "System. Linq" #>
6
7 <# @ import namespace = "System. Text" #>
8
9 <# @ import namespace = "System. Collections. Generic" #>
10
11 <# @ output extension = ". cs" #>
12
13 using System;
14
15 public class Kencery
16
17 {
18
19 <# for (int I = 0; I <10; I ++) {#>
20
21 public int HYL <# = I #>{ get; set ;}
22
23 <#}#>
24
25} (2) How do we implement C # code in our WebForm? Is the <%> Format to implement operations on the C # code. In the TT template, we changed a little bit, that is, <##>.

(3) This code is very simple. What do most bloggers know? If you don't know, you can ask me at any time. Let's discuss it together. Let's not talk about it here. When we write ten thousand TT codes, then, when we click Save, A MyFirst is automatically generated. CS class, corresponding to the code generated in the above TT template:


1 using System;
2
3 public class Kencery
4
5 {
6
7 public int HYL0 {get; set ;}
8
9 public int HYL1 {get; set ;}
10
11 public int HYL2 {get; set ;}
12
13 public int HYL3 {get; set ;}
14
15 public int HYL4 {get; set ;}
16
17 public int HYL5 {get; set ;}
18
19 public int HYL6 {get; set ;}
20
21 public int HYL7 {get; set ;}
22
23 public int HYL8 {get; set ;}
24
25 public int hyd9{ get; set ;}
26
27} (4) there are a lot of knowledge points in the TT template. Of course, I didn't learn it either, but I would just use something simple. In this blog, I will write all my articles. Generally, we seldom write TT templates by ourselves, install plug-ins directly, or search for them online.

4. Generate the corresponding warehouse for all tables based on the tables in the database

(1) According to the above introduction, we have roughly understood how to use the TT template, next we will implement a TT template for this project to generate a warehouse for all tables based on the tables in the database. We open the database access layer (LYZJ. userLimitMVC. the Code of RoleRepository In DAL) is copied to the TT template we created. The code in RoleRepsoitory is as follows:


1 using System;
2
3 using System. Collections. Generic;
4
5 using System. Linq;
6
7 using System. Text;
8
9 using System. Threading. Tasks;
10
11 using LYZJ. UserLimitMVC. IDAL;
12
13 using LYZJ. UserLimitMVC. Model;
14
15
16 namespace LYZJ. UserLimitMVC. DAL
17
18 {
19
20 public class RoleRepository: BaseRepository <Role>, IRoleRepository
21
22 {
23
26}
27
28} (2) then we create a TT template IService. TT template: copy the above Code to the TT template for modification. First, we can see that the class declared above is that we read the table name in the database and add the Repository suffix, that is, RoleRepository, the subsequent inheritance and warehousing are fixed. Just change the table name. In addition, the following interface is used to replace the table name in the middle. I have written some details in the note notes. You can refer to it and change it to the following TT template:


<# @ Template debug = "false" hostspecific = "true" language = "C #" #>

<# @ Include file = "EF. Utility. CS. ttinclude" #> // introduce the namespace of the TT template.

<# @ 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 = @ ".. \ LYZJ. UserLimitMVC. Model \ DataModel. edmx ";

EdmItemCollection ItemCollection = loader. CreateEdmItemCollection (inputFile );

String namespaceName = code. VsNamespaceSuggestion ();

EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager. Create (this );

#>

// Use the TT template to generate code snippets

Using System. Data. Objects;

Using LYZJ. UserLimitMVC. IDAL;

Using LYZJ. UserLimitMVC. Model;

 

Namespace LYZJ. UserLimitMVC. DAL

{

// Here we need a for loop to traverse all the tables in the database and place them below, so that the corresponding warehouse of all the tables is displayed.

<#

// Use the foreach loop to traverse the ItemCollection. GetItems <EntityType> set

Foreach (EntityType entity in ItemCollection. GetItems <EntityType> (). OrderBy (e => e. Name ))

{#>

Public partial class <# = entity. Name #> Repository: BaseRepository <# = entity. Name #>, I <# = entity. Name #> Repository

{

}

<#}#>

} (3) the above TT template code converts all the entity code at the database access layer into cs code, because the entity model contains information about two tables, Role and UserInfo, the code generated using the TT template is as follows:


1 // introduce the namespace of the TT Template
2
3 // use the TT template to generate code snippets
4
5 using System. Data. Objects;
6
7 using LYZJ. UserLimitMVC. IDAL;
8
9 using LYZJ. UserLimitMVC. Model;
10 namespace LYZJ. UserLimitMVC. DAL
11
12 {
13
14 // here we need a for loop to traverse all the tables in the database and place them below, so that the corresponding warehouse of all the tables is displayed.
15
16 public partial class RoleRepository: BaseRepository <Role>, IRoleRepository
17
18 {
19}
20
21 public partial class UserInfoRepository: BaseRepository <UserInfo>, IUserInfoRepository
22
23 {
24}
25} (4) Remember that the code in the template and the database access layer should be defined as a partial Classification when declaring the class. What is the role of the code? I don't believe it.

(5) What I want to declare here is that no matter whether we write code or do code-related tasks later, as long as the generated code is encountered, we must define them as partial ).

5. Summary

(1) This blog briefly describes how to use the TT template. It is not detailed enough. I believe you will not be able to see anything from this point. I recommend this blog here, you can learn how to use the TT template by yourself. It is really convenient and simple! References.

(2) tomorrow we will start to talk about the use of the source code manager. I am talking about VSS, that is, how to use it for the project, if you want to learn more, you are advised to search for it online.

(3) wish everyone good health and stay away from bird flu !!

 

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.