db first数据模型多层架构T4模板的使用

T4,即4个T开头的英文字母组合:Text Template Transformation Toolkit。即一种自定义规则的代码生成器。根据业务模型可生成任何形式的文本文件或供程序调用的字符串。
利用T4模板,方便的从数据库映射成的Model模型中自动生成多层架构中结构相同的重复代码,避免重复手写。以后需要增加model的话,T4模板能追加相应代码。

IDAL层

<#@ 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 = @"..\\Weick.Learn.Model\\WeickLearnModel.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 Weick.Learn.Model;

namespace Weick.Learn.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#>>
    {
      
    }
<#}#>
	
}

在这里插入图片描述
DAL层

<#@ 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 = @"..\\Weick.Learn.Model\\WeickLearnModel.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 Weick.Learn.IDAL;
using Weick.Learn.Model;

namespace Weick.Learn.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
    {

    }
<#}#>
}

在这里插入图片描述
DALFactory层

<#@ 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 =@"..\\Weick.Learn.Model\\WeickLearnModel.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.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using Weick.Learn.IDAL;

namespace Weick.Learn.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;
        }
<#}#>
	}
}

在这里插入图片描述

<#@ 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 = @"..\\Weick.Learn.Model\\WeickLearnModel.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;
using Weick.Learn.DAL;
using Weick.Learn.IDAL;

namespace Weick.Learn.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; }
        }
<#}#>
	}	
}

在这里插入图片描述
IBLL层

<#@ 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 = @"..\\Weick.Learn.Model\\WeickLearnModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weick.Learn.Model;

namespace Weick.Learn.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#>>
    {
       
    }   
<#}#>
}

在这里插入图片描述
BLL层

<#@ 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 = @"..\\Weick.Learn.Model\\WeickLearnModel.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 Weick.Learn.IBLL;
using Weick.Learn.Model;

namespace Weick.Learn.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;
        }
    }   
<#}#>	
}

在这里插入图片描述
有关T4模板介绍
https://blog.csdn.net/shuai7boy/article/details/53203294?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

https://blog.csdn.net/linjcai/article/details/81263215

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值