自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

EF Code First:使用T4模板生成相似代碼

開發(fā) 架構(gòu)
經(jīng)過前面EF的《第一篇》與《第二篇》,我們的數(shù)據(jù)層功能已經(jīng)較為完善了,但有不少代碼相似度較高,比如負責實體映射的 EntityConfiguration,負責倉儲操作的IEntityRepository與EntityRepository。而且每添加一個實體類型,就要手動去添加一套相應的代碼,也是比較累的工作。

一、前言

經(jīng)過前面EF的《第一篇》與《第二篇》,我們的數(shù)據(jù)層功能已經(jīng)較為完善了,但有不少代碼相似度較高,比如負責實體映射的 EntityConfiguration,負責倉儲操作的IEntityRepository與EntityRepository。而且每添加一個實體類型,就要手動去添加一套相應的代碼,也是比較累的工作。如果能有一個根據(jù)實體類型自動生成這些相似度較高的代碼的解決方案,那將會減少大量的無聊的工作。

VS提供的“文本模板”(俗稱T4)功能,就是一個較好的解決方案。要添加一個實體類型,只要把實體類型定義好,然后運行一下定義好的T4模板,就可以自動生成相應的類文件。

二、工具準備

 為了更好的使用 T4模板 功能,我們需要給VS安裝如下兩個插件:

  • Devart T4 Editor:為VS提供智能提示功能。
  • T4 Toolbox:在生成多文件時很有用。

三、T4代碼生成預熱

(一) 單文件生成:HelloWorld.cs

 下面,我們先來體驗一個最簡單的T4代碼生成功能,輸出一個最簡單的類文件。

首先,在 GMF.Demo.Core.Data中 添加一個名為 T4 的文件夾,用于存放生成本工程內(nèi)的代碼的T4模板文件。并在其中添加一個名為 HelloWorld.tt的“文本模板”的項。

HelloWorld.tt定義如下:

  1. <#@ template debug="false" hostspecific="false" language="C#" #>  
  2. <#@ assembly name="System.Core" #>  
  3. <#@ import namespace="System.Linq" #>  
  4. <#@ import namespace="System.Text" #>  
  5. <#@ import namespace="System.Collections.Generic" #>  
  6. <#@ output extension=".cs" #>  
  7. using System;  
  8.  
  9. namespace GMF.Demo.Core.Data.T4  
  10. {  
  11.     public class HelloWorld  
  12.     {  
  13.         private string _word;  
  14.  
  15.         public HelloWorld(string word)  
  16.         {  
  17.             _word = word;  
  18.         }  
  19.     }  

直接保存文件(T4的生成將會在保存模板,模板失去焦點等情況下自動觸發(fā)生成。),將會在模板的當前位置生成一個同名的類文件:

HelloWorld.cs的內(nèi)容如下:

  1. using System;  
  2.  
  3. namespace GMF.Demo.Core.Data.T4  
  4. {  
  5.     public class HelloWorld  
  6.     {  
  7.         private string _word;  
  8.  
  9.         public HelloWorld(string word)  
  10.         {  
  11.             _word = word;  
  12.         }  
  13.     }  

這樣,我們的HelloWorld之旅就結(jié)束了,非常簡單。

(二) 多文件生成

當前位置方案的方案只能生成如下所示的代碼:

生成的文件會與T4模板在同一目錄中,這里就不詳述了,可以參考 蔣金楠 一個簡易版的T4代碼生成"框架" 。

本項目的多文件需要生成到指定文件夾中,但又想對T4模板進行統(tǒng)一的管理,T4文件夾里放置T4模板文件,但生成的映射文件EntityConfiguration將放置到文件夾Configurations中,倉儲操作的文件IEntityRepository與EntityRepository將放置到Repositories文件夾中。且生成的代碼文件應能自動的添加到解決方案中,而不是只是在文件夾中存在。

要實現(xiàn)此需求,一個簡單的辦法就是通過 T4 Toolbox 來進行生成。想了解 T4 Toolbox 的細節(jié),可以自己使用 ILSpy 來對 T4Toolbox.dll 文件進行反編譯來查看源代碼,如果是通過 T4 Toolbox 是通過VS的插件來安裝的,將在“C:\Users\Administrator\AppData\Local\Microsoft\VisualStudio\11.0\Extensions\dca4f0lt.jdx”文件夾中(我的機器上)。下面,我們直接進行多文件生成的演示。

首先,添加一個名為 HelloWorldTemplate.tt 的 T4 Toolbox 的代碼模板。

此模板將繼承于 T4 Toolbox 的 CSharpTemplate 類:

  1. <#+  
  2. // <copyright file="HelloWorldTemplate.tt" company="郭明鋒@中國">  
  3. //  Copyright © 郭明鋒@中國. All Rights Reserved.  
  4. // </copyright>  
  5.  
  6. public class HelloWorldTemplate : CSharpTemplate  
  7. {  
  8.     private string _className;  
  9.  
  10.     public HelloWorldTemplate(string className)  
  11.     {  
  12.         _className = className;  
  13.     }  
  14.  
  15.     public override string TransformText()  
  16.     {  
  17. #>  
  18. using System;  
  19.  
  20. namespace GMF.Demo.Core.Data.T4  
  21. {  
  22.     public class <#=_className #>  
  23.     {  
  24.         private string _word;  
  25.  
  26.         public <#=_className #>(string word)  
  27.         {  
  28.             _word = word;  
  29.         }  
  30.     }  
  31. }  
  32. <#+  
  33.         return this.GenerationEnvironment.ToString();  
  34.     }  
  35. }  
  36. #> 

模板類中定義了一個 className 參數(shù),用于接收一個表示要生成的類名的值。所有生成類的代碼都以字符串的形式寫在重寫的 TransformText 方法中。

再定義一個T4模板文件 HelloWorldMulti.tt,用于調(diào)用 上面定義的代碼模板進行代碼文件的生成。

  1. <#@ template debug="false" hostspecific="false" language="C#" #>  
  2. <#@ assembly name="System.Core" #>  
  3. <#@ import namespace="System.IO" #>  
  4. <#@ import namespace="System.Linq" #>  
  5. <#@ import namespace="System.Text" #>  
  6. <#@ import namespace="System.Collections.Generic" #>  
  7. <#@ include file="T4Toolbox.tt" #>  
  8. <#@ include file="HelloWorldTemplate.tt" #>  
  9. <#  
  10.     string curPath = Path.GetDirectoryName(Host.TemplateFile);  
  11.     string destPath = Path.Combine(curPath, "outPath");  
  12.     if(!Directory.Exists(destPath))  
  13.     {  
  14.         Directory.CreateDirectory(destPath);  
  15.     }  
  16.     string[] classNames = new[]{"HelloWorld1""HelloWorld2""HelloWorld3"};  
  17.     foreach(string className in classNames)  
  18.     {  
  19.         HelloWorldTemplate template = new HelloWorldTemplate(className);  
  20.         string fileName = string.Format(@"{0}\{1}.cs", destPath, className);  
  21.         template.Output.Encoding = Encoding.UTF8;  
  22.         template.RenderToFile(fileName);  
  23.     }  
  24.  #> 

以上是整個T4模板的執(zhí)行方,在執(zhí)行方中,要引用所有需要用到的類庫文件,命名空間,包含的模板文件等。

最后,文件的生成是調(diào)用 T4 Toolbox 的Template基類中定義的 RenderToFile(string filename)方法來生成各個文件的,輸入的參數(shù)為生成文件的文件全名。在這里,生成將如下所示:

outPPath文件夾中生成了 HelloWorld1.cs、HelloWorld2.cs、HelloWorld3.cs 文件,而 HelloWorldMulti.tt 所在文件夾中也會生成一個空的 HelloWorldMulti.cs 類文件。

#p#

 

四、生成數(shù)據(jù)層實體相關相似代碼

(一) 生成準備

我們的生成代碼是完全依賴于業(yè)務實體的,所以,需要有一個類來對業(yè)務實體的信息進行提取封裝。

  1. namespace GMF.Component.Tools.T4  
  2. {  
  3.     /// <summary>  
  4.     /// T4實體模型信息類  
  5.     /// </summary>  
  6.     public class T4ModelInfo  
  7.     {  
  8.         /// <summary>  
  9.         /// 獲取 模型所在模塊名稱  
  10.         /// </summary>  
  11.         public string ModuleName { getprivate set; }  
  12.  
  13.         /// <summary>  
  14.         /// 獲取 模型名稱  
  15.         /// </summary>  
  16.         public string Name { getprivate set; }  
  17.  
  18.         /// <summary>  
  19.         /// 獲取 模型描述  
  20.         /// </summary>  
  21.         public string Description { getprivate set; }  
  22.  
  23.         public IEnumerable<PropertyInfo> Properties { getprivate set; }  
  24.  
  25.         public T4ModelInfo(Type modelType)  
  26.         {  
  27.             var @namespace = modelType.Namespace;  
  28.             if (@namespace == null)  
  29.             {  
  30.                 return;  
  31.             }  
  32.             var index = @namespace.LastIndexOf('.') + 1;  
  33.             ModuleName = @namespace.Substring(index, @namespace.Length - index);  
  34.             Name = modelType.Name;  
  35.             var descAttributes = modelType.GetCustomAttributes(typeof(DescriptionAttribute), true);  
  36.             Description = descAttributes.Length == 1 ? ((DescriptionAttribute)descAttributes[0]).Description : Name;  
  37.             Properties = modelType.GetProperties();  
  38.         }  
  39.     }  

另外,通過模板生成的代碼,與我們手寫的代碼有如下幾個區(qū)別:

  1. 手寫代碼可以自由定義,生成代碼是根據(jù)模板生成的,必然遵守模板定義的規(guī)范。
  2. 手寫代碼的修改可以永久保存,生成代碼的修改將會在下次生成后丟失,即生成代碼不應該進行修改。

基于以上幾個區(qū)別,我提出如下解決方案,來解決生成代碼的修改問題

  1. 通過模板生成的類應盡量定義為分部類(partial class),在需要進行修改及擴展的時候可以定義另外的同名分部類來進行修改。
  2. 對于需要外部實現(xiàn)的功能,定義分部方法來對外提供擴展。
  3. 生成代碼的類文件命名把擴展名由“.cs”更改為“generated.cs”,以區(qū)分生成代碼與手寫代碼文件。

(二) 生成實體相關相似代碼

1. 生成實體映射配置類

實體映射配置類模板 EntityConfigurationTemplate.tt 定義:

  1. <#+  
  2. // <copyright file="EntityConfigurationTemplate.tt" company="郭明鋒@中國">  
  3. //  Copyright © 郭明鋒@中國. All Rights Reserved.  
  4. // </copyright>  
  5.  
  6. public class EntityConfigurationTemplate : CSharpTemplate  
  7. {  
  8.     private T4ModelInfo _model;  
  9.           
  10.     public EntityConfigurationTemplate(T4ModelInfo model)  
  11.     {  
  12.         _model = model;  
  13.     }  
  14.  
  15.     /// <summary>  
  16.     /// 獲取 生成的文件名,根據(jù)模型名定義  
  17.     /// </summary>  
  18.     public string FileName  
  19.     {  
  20.         get 
  21.         {   
  22.             return string.Format("{0}Configuration.generated.cs", _model.Name);  
  23.         }  
  24.     }  
  25.  
  26.     public override string TransformText()  
  27.     {  
  28. #>  
  29. //------------------------------------------------------------------------------  
  30. // <auto-generated>  
  31. //     此代碼由工具生成。  
  32. //     對此文件的更改可能會導致不正確的行為,并且如果  
  33. //     重新生成代碼,這些更改將會丟失。  
  34. //        如存在本生成代碼外的新需求,請在相同命名空間下創(chuàng)建同名分部類實現(xiàn) <#= _model.Name #>ConfigurationAppend 分部方法。  
  35. // </auto-generated>  
  36. //  
  37. // <copyright file="<#= _model.Name #>Configuration.generated.cs">  
  38. //        Copyright(c)2013 GMFCN.All rights reserved.  
  39. //        CLR版本:4.0.30319.239  
  40. //        開發(fā)組織:郭明鋒@中國  
  41. //        公司網(wǎng)站:http://www.gmfcn.net  
  42. //        所屬工程:GMF.Demo.Core.Data  
  43. //        生成時間:<#= DateTime.Now.ToString("yyyy-MM-dd HH:mm") #>  
  44. // </copyright>  
  45. //------------------------------------------------------------------------------  
  46.  
  47. using System;  
  48. using System.Data.Entity.ModelConfiguration;  
  49. using System.Data.Entity.ModelConfiguration.Configuration;  
  50.  
  51. using GMF.Component.Data;  
  52. using GMF.Demo.Core.Models;  
  53.  
  54.  
  55. namespace GMF.Demo.Core.Data.Configurations  
  56. {  
  57.     /// <summary>  
  58.     /// 實體類-數(shù)據(jù)表映射——<#= _model.Description #>  
  59.     /// </summary>      
  60.     internal partial class <#= _model.Name #>Configuration : EntityTypeConfiguration<<#= _model.Name #>>, IEntityMapper  
  61.     {  
  62.         /// <summary>  
  63.         /// 實體類-數(shù)據(jù)表映射構(gòu)造函數(shù)——<#= _model.Description #>  
  64.         /// </summary>  
  65.         public <#= _model.Name #>Configuration()  
  66.         {  
  67.             <#= _model.Name #>ConfigurationAppend();  
  68.         }  
  69.           
  70.         /// <summary>  
  71.         /// 額外的數(shù)據(jù)映射  
  72.         /// </summary>  
  73.         partial void <#= _model.Name #>ConfigurationAppend();  
  74.           
  75.         /// <summary>  
  76.         /// 將當前實體映射對象注冊到當前數(shù)據(jù)訪問上下文實體映射配置注冊器中  
  77.         /// </summary>  
  78.         /// <param name="configurations">實體映射配置注冊器</param>  
  79.         public void RegistTo(ConfigurationRegistrar configurations)  
  80.         {  
  81.             configurations.Add(this);  
  82.         }  
  83.     }  
  84. }  
  85. <#+  
  86.         return this.GenerationEnvironment.ToString();  
  87.     }  
  88. }  
  89. #> 

生成模板調(diào)用方 EntityCodeScript.tt 定義

  1. <#@ template language="C#" debug="True" #>  
  2. <#@ output extension="cs" #>  
  3. <#@ Assembly Name="System.Core" #>  
  4. <#@ Assembly Name="$(SolutionDir)\GMF.Component.Tools\bin\Debug\GMF.Component.Tools.dll" #>  
  5. <#@ import namespace="System.IO" #>  
  6. <#@ Import Namespace="System.Linq" #>  
  7. <#@ Import Namespace="System.Text" #>  
  8. <#@ import namespace="System.Reflection" #>  
  9. <#@ Import Namespace="System.Collections.Generic" #>  
  10. <#@ Import Namespace="GMF.Component.Tools" #>  
  11. <#@ Import Namespace="GMF.Component.Tools.T4" #>  
  12. <#@ include file="T4Toolbox.tt" #>  
  13. <#@ include file="Include\EntityConfigurationTemplate.tt" #>  
  14. <#  
  15.     string currentPath = Path.GetDirectoryName(Host.TemplateFile);  
  16.     string projectPath =currentPath.Substring(0, currentPath.IndexOf(@"\T4"));  
  17.     string solutionPath = currentPath.Substring(0, currentPath.IndexOf(@"\GMF.Demo.Core.Data"));  
  18.  
  19.     string modelFile= Path.Combine(solutionPath, @"GMF.Demo.Core.Models\bin\Debug\GMF.Demo.Core.Models.dll");  
  20.     byte[] fileData= File.ReadAllBytes(modelFile);  
  21.     Assembly assembly = Assembly.Load(fileData);  
  22.     IEnumerable<Type> modelTypes = assembly.GetTypes().Where(m => typeof(Entity).IsAssignableFrom(m) && !m.IsAbstract);  
  23.     foreach(Type modelType in modelTypes)  
  24.     {  
  25.         T4ModelInfo model = new T4ModelInfo(modelType);  
  26.         //實體映射類  
  27.         EntityConfigurationTemplate config = new EntityConfigurationTemplate(model);  
  28.         string path = string.Format(@"{0}\Configurations", projectPath);  
  29.         config.Output.Encoding = Encoding.UTF8;  
  30.         config.RenderToFile(Path.Combine(path, config.FileName));  
  31.     }  
  32. #> 

調(diào)用方通過反射從業(yè)務實體程序集 GMF.Demo.Core.Models.dll 中獲取所有基類為 Entity 的并且不是抽象類的實體類型信息,再調(diào)用模板逐個生成實體配置類文件。

例如,生成的登錄記錄信息(LoginLog)的映射文件 LoginLogConfiguration.generated.cs 如下:

  1. //------------------------------------------------------------------------------  
  2. // <auto-generated>  
  3. //     此代碼由工具生成。  
  4. //     對此文件的更改可能會導致不正確的行為,并且如果  
  5. //     重新生成代碼,這些更改將會丟失。  
  6. //        如存在本生成代碼外的新需求,請在相同命名空間下創(chuàng)建同名分部類實現(xiàn) LoginLogConfigurationAppend 分部方法。  
  7. // </auto-generated>  
  8. //  
  9. // <copyright file="LoginLogConfiguration.generated.cs">  
  10. //        Copyright(c)2013 GMFCN.All rights reserved.  
  11. //        CLR版本:4.0.30319.239  
  12. //        開發(fā)組織:郭明鋒@中國  
  13. //        公司網(wǎng)站:http://www.gmfcn.net  
  14. //        所屬工程:GMF.Demo.Core.Data  
  15. //        生成時間:2013-06-16 17:45  
  16. // </copyright>  
  17. //------------------------------------------------------------------------------  
  18.  
  19. using System;  
  20. using System.Data.Entity.ModelConfiguration;  
  21. using System.Data.Entity.ModelConfiguration.Configuration;  
  22.  
  23. using GMF.Component.Data;  
  24. using GMF.Demo.Core.Models;  
  25.  
  26.  
  27. namespace GMF.Demo.Core.Data.Configurations  
  28. {  
  29.     /// <summary>  
  30.     /// 實體類-數(shù)據(jù)表映射——登錄記錄信息  
  31.     /// </summary>      
  32.     internal partial class LoginLogConfiguration : EntityTypeConfiguration<LoginLog>, IEntityMapper  
  33.     {  
  34.         /// <summary>  
  35.         /// 實體類-數(shù)據(jù)表映射構(gòu)造函數(shù)——登錄記錄信息  
  36.         /// </summary>  
  37.         public LoginLogConfiguration()  
  38.         {  
  39.             LoginLogConfigurationAppend();  
  40.         }  
  41.           
  42.         /// <summary>  
  43.         /// 額外的數(shù)據(jù)映射  
  44.         /// </summary>  
  45.         partial void LoginLogConfigurationAppend();  
  46.           
  47.         /// <summary>  
  48.         /// 將當前實體映射對象注冊到當前數(shù)據(jù)訪問上下文實體映射配置注冊器中  
  49.         /// </summary>  
  50.         /// <param name="configurations">實體映射配置注冊器</param>  
  51.         public void RegistTo(ConfigurationRegistrar configurations)  
  52.         {  
  53.             configurations.Add(this);  
  54.         }  
  55.     }  

要配置登錄信息與用戶信息的 N:1 關系,只需要添加一個分部類 LoginLogConfiguration,并實現(xiàn)分類方法 LoginLogConfigurationAppend 即可。

  1. namespace GMF.Demo.Core.Data.Configurations  
  2. {  
  3.     partial class LoginLogConfiguration  
  4.     {  
  5.         partial void LoginLogConfigurationAppend()  
  6.         {  
  7.             HasRequired(m => m.Member).WithMany(n => n.LoginLogs);  
  8.         }  
  9.     }  

#p#

2. 生成實體倉儲接口

實體映射配置類模板 EntityConfigurationTemplate.tt 定義:

  1. <#+  
  2. // <copyright file="IEntityRepositoryTemplate.tt" company="郭明鋒@中國">  
  3. //  Copyright © 郭明鋒@中國. All Rights Reserved.  
  4. // </copyright>  
  5.  
  6. public class IEntityRepositoryTemplate : CSharpTemplate  
  7. {  
  8.     private T4ModelInfo _model;  
  9.           
  10.     public IEntityRepositoryTemplate(T4ModelInfo model)  
  11.     {  
  12.         _model = model;  
  13.     }  
  14.  
  15.     /// <summary>  
  16.     /// 獲取 生成的文件名,根據(jù)模型名定義  
  17.     /// </summary>  
  18.     public string FileName  
  19.     {  
  20.         get 
  21.         {   
  22.             return string.Format("I{0}Repository.generated.cs", _model.Name);  
  23.         }  
  24.     }  
  25.  
  26.     public override string TransformText()  
  27.     {  
  28. #>  
  29. //------------------------------------------------------------------------------  
  30. // <auto-generated>  
  31. //     此代碼由工具生成。  
  32. //     對此文件的更改可能會導致不正確的行為,并且如果  
  33. //     重新生成代碼,這些更改將會丟失。  
  34. //       如存在本生成代碼外的新需求,請在相同命名空間下創(chuàng)建同名分部類進行實現(xiàn)。  
  35. // </auto-generated>  
  36. //  
  37. // <copyright file="I<#= _model.Name #>Repository.generated.cs">  
  38. //        Copyright(c)2013 GMFCN.All rights reserved.  
  39. //        CLR版本:4.0.30319.239  
  40. //        開發(fā)組織:郭明鋒@中國  
  41. //        公司網(wǎng)站:http://www.gmfcn.net  
  42. //        所屬工程:GMF.Demo.Core.Data  
  43. //        生成時間:<#= DateTime.Now.ToString("yyyy-MM-dd HH:mm") #>  
  44. // </copyright>  
  45. //------------------------------------------------------------------------------  
  46.  
  47. using System;  
  48.  
  49. using GMF.Component.Data;  
  50. using GMF.Demo.Core.Models;  
  51.  
  52.  
  53. namespace GMF.Demo.Core.Data.Repositories  
  54. {  
  55.     /// <summary>  
  56.     ///   數(shù)據(jù)訪問層接口——<#= _model.Description #>  
  57.     /// </summary>  
  58.     public partial interface I<#= _model.Name #>Repository : IRepository<<#= _model.Name #>>  
  59.     { }  
  60. }  
  61.  
  62. <#+  
  63.         return this.GenerationEnvironment.ToString();  
  64.     }  
  65. }  
  66. #> 

相應的,在調(diào)用方 EntityCodeScript.tt 中添加模板調(diào)用代碼(如下 11-15 行所示):

  1. foreach(Type modelType in modelTypes)  
  2.     {  
  3.         T4ModelInfo model = new T4ModelInfo(modelType);  
  4.  
  5.         //實體映射類  
  6.         EntityConfigurationTemplate config = new EntityConfigurationTemplate(model);  
  7.         string path = string.Format(@"{0}\Configurations", projectPath);  
  8.         config.Output.Encoding = Encoding.UTF8;  
  9.         config.RenderToFile(Path.Combine(path, config.FileName));  
  10.  
  11.         //實體倉儲操作接口  
  12.         IEntityRepositoryTemplate irep= new IEntityRepositoryTemplate(model);  
  13.         path = string.Format(@"{0}\Repositories", projectPath);  
  14.         irep.Output.Encoding = Encoding.UTF8;  
  15.         irep.RenderToFile(Path.Combine(path, irep.FileName));  
  16.     } 

生成的登錄記錄信息倉儲操作接口 ILoginLogRepository.generated.cs:

  1. //------------------------------------------------------------------------------  
  2. // <auto-generated>  
  3. //     此代碼由工具生成。  
  4. //     對此文件的更改可能會導致不正確的行為,并且如果  
  5. //     重新生成代碼,這些更改將會丟失。  
  6. //       如存在本生成代碼外的新需求,請在相同命名空間下創(chuàng)建同名分部類進行實現(xiàn)。  
  7. // </auto-generated>  
  8. //  
  9. // <copyright file="ILoginLogRepository.generated.cs">  
  10. //        Copyright(c)2013 GMFCN.All rights reserved.  
  11. //        CLR版本:4.0.30319.239  
  12. //        開發(fā)組織:郭明鋒@中國  
  13. //        公司網(wǎng)站:http://www.gmfcn.net  
  14. //        所屬工程:GMF.Demo.Core.Data  
  15. //        生成時間:2013-06-16 17:56  
  16. // </copyright>  
  17. //------------------------------------------------------------------------------  
  18.  
  19. using System;  
  20.  
  21. using GMF.Component.Data;  
  22. using GMF.Demo.Core.Models;  
  23.  
  24.  
  25. namespace GMF.Demo.Core.Data.Repositories  
  26. {  
  27.     /// <summary>  
  28.     ///   數(shù)據(jù)訪問層接口——登錄記錄信息  
  29.     /// </summary>  
  30.     public partial interface ILoginLogRepository : IRepository<LoginLog>  
  31.     { }  

如果 IRepository<T> 中定義的倉儲操作不滿足登錄記錄倉儲操作的需求,只需要添加一個相應的分部接口,在其中進行需求的定義即可。這里當前沒有額外的需求,就不需要額外定義了。

3. 生成實體倉儲實現(xiàn)

實體倉儲操作的實現(xiàn)與倉儲操作接口類似,這里略過。

完成生成后,代碼結(jié)構(gòu)如下所示:

如果添加了或者刪除了一個實體,只需要重新運行一下調(diào)用模板 EntityCodeScript.tt 即可重新生成新的代碼。

五、源碼獲取

為了讓大家能第一時間獲取到本架構(gòu)的最新代碼,也為了方便我對代碼的管理,本系列的源碼已加入微軟的開源項目網(wǎng)站 http://www.codeplex.com,地址為:

https://gmframework.codeplex.com/

原文鏈接:http://www.cnblogs.com/guomingfeng/p/mvc-ef-t4.html

責任編輯:林師授 來源: 博客園
相關推薦

2011-05-25 13:26:09

數(shù)據(jù)實體

2013-09-08 21:41:10

RepositoryUnitOfWorkDbContext

2013-09-08 22:40:38

EF Code Fir數(shù)據(jù)查詢架構(gòu)設計

2013-09-08 23:37:30

EF Code Fir架構(gòu)設計MVC架構(gòu)設計

2009-07-22 16:34:36

使用T4ASP.NET MVC

2013-09-08 23:30:56

EF Code Fir架構(gòu)設計MVC架構(gòu)設計

2013-09-08 22:12:02

EF Code Fir數(shù)據(jù)遷移MVC架構(gòu)設計

2011-09-26 16:26:22

T4模板

2016-10-14 15:39:00

香港 數(shù)據(jù)中心

2011-09-21 16:52:11

Sparc T4芯片甲骨文

2010-12-08 09:53:42

2011-10-08 15:51:56

甲骨文SPARC

2011-08-16 10:17:12

XCode模版引擎XTemplate

2012-08-15 11:03:18

框架項目

2015-08-11 11:08:19

中科創(chuàng)達

2011-09-30 10:53:32

甲骨文SPARC T5SPARC T4

2016-08-23 13:35:22

MVCEFNuGet

2011-05-24 09:47:44

筆記本故障檢修

2015-01-21 20:58:48

慕課網(wǎng)
點贊
收藏

51CTO技術棧公眾號