欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MVC使用T4模板生成其他类的具体实现学习笔记2

程序员文章站 2023-11-30 08:20:58
在前篇中我们已经将user类中的代码做了具体的实现,但仍然有多个实体类未实现,以后可能还会增加新的数据表,数据表结构也有可能发生变化,所以我们使用t4模板来完成类的生成,这...

在前篇中我们已经将user类中的代码做了具体的实现,但仍然有多个实体类未实现,以后可能还会增加新的数据表,数据表结构也有可能发生变化,所以我们使用t4模板来完成类的生成,这样就算数据库表发生了改变,也会自动根据改变后的实体对类进行重新生成。

下面是数据访问层的t4模板文件 dal.tt

<#@ 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实体文件在项目中的路径
string inputfile = @"..\\pms.model\\pms.edmx";

edmitemcollection itemcollection = loader.createedmitemcollection(inputfile);
string namespacename = code.vsnamespacesuggestion();

entityframeworktemplatefilemanager filemanager = entityframeworktemplatefilemanager.create(this);

#>
<#//这里为命名空间部分,手动更改为相应的命名空间 #>
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实体文件路径、命名空间更改为对应的值时,ctrl+s保存,即可生成对应的其他类型的数据访问类

其他层中也大同小异,只需要做对应的更改即可,下面我将提供相应的代码

idal层

idal.tt

<#@ 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

<#@ 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

<#@ 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

<#@ 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

<#@ 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

<#@ 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#>>
  {
    
  }  
<#}#>
  
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。