国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - ASP.NET教程 - MVC使用T4模板生成其他類(lèi)的具體實(shí)現(xiàn)學(xué)習(xí)筆記2

MVC使用T4模板生成其他類(lèi)的具體實(shí)現(xiàn)學(xué)習(xí)筆記2

2020-03-25 14:39叫我瑋仔 ASP.NET教程

這篇文章主要為大家詳細(xì)介紹了MVC使用T4模板生成其他類(lèi)的具體實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在前篇中我們已經(jīng)將User類(lèi)中的代碼做了具體的實(shí)現(xiàn),但仍然有多個(gè)實(shí)體類(lèi)未實(shí)現(xiàn),以后可能還會(huì)增加新的數(shù)據(jù)表,數(shù)據(jù)表結(jié)構(gòu)也有可能發(fā)生變化,所以我們使用T4模板來(lái)完成類(lèi)的生成,這樣就算數(shù)據(jù)庫(kù)表發(fā)生了改變,也會(huì)自動(dòng)根據(jù)改變后的實(shí)體對(duì)類(lèi)進(jìn)行重新生成。

下面是數(shù)據(jù)訪問(wèn)層的T4模板文件 Dal.tt

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<#@ 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);
 
//EF實(shí)體文件在項(xiàng)目中的路徑
string inputFile = @"..\\PMS.Model\\PMS.edmx";
 
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
 
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
 
#>
<#//這里為命名空間部分,手動(dòng)更改為相應(yīng)的命名空間 #>
using PMS.IDAL;
using PMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace PMS.DAL
{
<#
// Emit Entity Types
 
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
  //fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
  //BeginNamespace(namespaceName, code); 
#>   
  public partial class <#=entity.Name#>Dal :BaseDal<<#=entity.Name#>>,I<#=entity.Name#>Dal
  {
 
  }
<#}#>
  
}

我們將EF實(shí)體文件路徑、命名空間更改為對(duì)應(yīng)的值時(shí),Ctrl+S保存,即可生成對(duì)應(yīng)的其他類(lèi)型的數(shù)據(jù)訪問(wèn)類(lèi)

其他層中也大同小異,只需要做對(duì)應(yīng)的更改即可,下面我將提供相應(yīng)的代碼

IDAL層

IDal.tt

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<#@ 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 = @"..\\PMS.Model\\PMS.edmx";
 
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 System.Threading.Tasks;
using PMS.Model;
 
namespace PMS.IDAL
{
  
<#
// Emit Entity Types
 
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
  //fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
  //BeginNamespace(namespaceName, code); 
#> 
  public partial interface I<#=entity.Name#>Dal :IBaseDal<<#=entity.Name#>>
  {
   
  }
<#}#>
  
}

IDbSession.tt

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<#@ 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 = @"..\\PMS.Model\\PMS.edmx";
 
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
 
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
 
#>
 
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
 
namespace PMS.IDAL
{
  public partial interface IDbSession
  {
 
<#
// Emit Entity Types
 
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
  //fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
  //BeginNamespace(namespaceName, code); 
#> 
    I<#=entity.Name#>Dal <#=entity.Name#>Dal{get;set;}
<#}#>
  
}

DALFactory層

SimpleDalFactory.tt

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<#@ 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 =@"..\\PMS.Model\\PMS.edmx";
 
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
 
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
 
#>
 
using SW.OA.IDAL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace SW.OA.DALFactory
{
  public partial class AbstractFactory
  {
   
  
<#
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
#>   
    public static I<#=entity.Name#>Dal Create<#=entity.Name#>Dal()
    {
 
     string fullClassName = NameSpace + ".<#=entity.Name#>Dal";
     return CreateInstance(fullClassName) as I<#=entity.Name#>Dal;
 
    }
<#}#>
  }
  
}

DbSession.tt

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<#@ 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 = @"..\\PMS.Model\\PMS.edmx";
 
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
 
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
 
#>
using PMS.DAL;
using PMS.IDAL;
using PMS.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace PMS.DALFactory
{
  public partial class DBSession : IDBSession
  {
<#
// Emit Entity Types
 
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
  //fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
  //BeginNamespace(namespaceName, code); 
#> 
    private I<#=entity.Name#>Dal _<#=entity.Name#>Dal;
    public I<#=entity.Name#>Dal <#=entity.Name#>Dal
    {
      get
      {
        if(_<#=entity.Name#>Dal == null)
        {
          _<#=entity.Name#>Dal = AbstractFactory.Create<#=entity.Name#>Dal();
        }
        return _<#=entity.Name#>Dal;
      }
      set { _<#=entity.Name#>Dal = value; }
    }
<#}#>
  
}

BLL層

Service.tt

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<#@ 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 = @"..\\PMS.Model\\PMS.edmx";
 
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
 
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
 
#>
using PMS.IBLL;
using PMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace PMS.BLL
{
<#
// Emit Entity Types
 
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
  //fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
  //BeginNamespace(namespaceName, code); 
#> 
  public partial class <#=entity.Name#>Service :BaseService<<#=entity.Name#>>,I<#=entity.Name#>Service
  {
  
 
     public override void SetCurrentDal()
    {
      CurrentDal = this.CurrentDbSession.<#=entity.Name#>Dal;
    }
  
<#}#>
  
}

BLL層

IService.tt

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<#@ 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 = @"..\\PMS.Model\\PMS.edmx";
 
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
 
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
 
#>
 
using PMS.Model;
using PMS.Model.Search;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace PMS.IBLL
{
<#
// Emit Entity Types
 
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
  //fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
  //BeginNamespace(namespaceName, code); 
#> 
  public partial interface I<#=entity.Name#>Service : IBaseService<<#=entity.Name#>>
  {
    
  
<#}#>
  
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/JiaoWoWeiZai/p/5876914.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 欧美狠狠操 | 高清精品一区二区 | 亚洲香蕉视频 | 91在线网址| 亚洲视频在线免费观看 | 玖玖在线| 国产在线一区二区 | 精品一区二区电影 | 日本色综合 | 色婷婷综合久久久中文字幕 | 国产一区高清 | 99亚洲| 国产欧美日韩综合精品 | 亚洲一区二区三区四区五区午夜 | 在线观看亚洲 | 久久久久国产一区二区三区四区 | 久久久精品网 | 视频一区在线观看 | 日韩精品久久 | 成人久久久精品乱码一区二区三区 | 中文字幕视频 | 男女爱爱免费视频 | 一二三区字幕免费观看av | 永久av| 成人高清在线 | 在线国产视频 | 欧美成人a∨高清免费观看 亚洲国产精品尤物yw在线观看 | 亚洲三级在线 | 亚洲 欧美 国产 制服 动漫 | 在线观看国产一区二区 | 色花av| 亚洲欧美中文字幕 | 日韩精品一区二区三区在线观看 | 成人精品久久久 | 99热在线播放| 久久精品国产99国产 | 一区二区三区日韩在线 | 亚洲国产成人av | 欧美黄色网视频 | 国产精品久久久久久久久久久久久 | 九九久久免费 |