如何通过T4生成自定义类? [英] how to generate a Custom Class via t4?

查看:129
本文介绍了如何通过T4生成自定义类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过生成的T4下面的代码



 使用系统; 
使用MyDAL;

namspace测试
{
//选择...
公共部分类MyCustomer
{
公众诠释ID {获取;设置; }
公共字符串名称{;设置;}
公共字符串姓{获取;集;}

公共MyCustomer()
{

}

公开名单<客户> GetById(INT ID)
{
//做一些事情......
}
}
//删除,更新,保存
公共部分类MyCustomer
{
公共BOOL保存(新MyCustomer)
{
//做一些事情......
}

公共BOOL删除(INT ID)
{
//做一些事情......
}

公共BOOL更新(INT ID)
{
//做东西...
}
}
}



T4模板我到目前为止有:

  
<#@模板语言=C##>
< #@集名称=Microsoft.SqlServer.Management.Sdk.Sfc#>
<#@集名称=Microsoft.SqlServer.ConnectionInfo#>
<#@集名称=微软。 SqlServer.Smo#>



<#@导入命名空间=Microsoft.SqlServer.Management.Smo#>

命名空间DbClasses
{
<#

服务器SRV =新服务器();
数据库DB =新的数据库(SRVNarDb);
db.Refresh();
的foreach(在db.Tables表tablo)
{

的WriteLine(大众级+ tablo.Name +{);
的foreach(在tablo.Columns列列)
{
如果(column.DataType.Name ==为nvarchar)
的WriteLine(公共的+字符串+ + column.Name +;);
,否则
的WriteLine(公共的+INT++ column.Name +);
}
的WriteLine(});



}
#>
}


解决方案

我不完全知道什么是你所面临的问题:但并没有什么不对您的T4代码。在第二类中的保存方法的签名有新的,这是一个错误:

 公共BOOL保存(新MyCustomer)

 公共BOOL保存(MyCustomer客户)

此外,GetById应返回一个元素不是列表。



我尝试了,你没有连接到数据库通过编写虚拟类表,列等写的代码并没有什么错,你写的T4代码,除了即,按您的要求,你写的代码不会产生局部的类,而不是它创建属性。它只是创造了公共领域(而不是属性)每个表单一非分部类。如果你想生成局部类和属性(和方法),你在正确的方向是(我加了标签和换行符,使其在生成的文件看起来很漂亮,你可以按下Ctrl + K,Ctrl + D键有重新格式化它虽然:

 <#

的foreach(在表表tablo)
{
//启动类
的WriteLine(公共部分类我+ tablo.Name +\\\
{);
的foreach(在tablo.Columns列列)
{
如果(column.DataType.Name ==为nvarchar)
的WriteLine(\tpublic字符串+ column.Name +{获取;集;});
,否则
的WriteLine(\tpublic INT+ column.Name +{获取;集;});
}
//输出构造器
的WriteLine(\tpublic我的 + tablo.Name +(){\\\
\t // \\\
\t\t} constructor\\\
\t);
的WriteLine(\tpublic名单< + tablo.Name +> GetById(INT ID){\\\
\t // \\\
\t\t做something\\\
\t});
//结束类
的WriteLine(});

//输出注释,并启动类
的WriteLine(//删除,更新,保存);
的WriteLine( 公共部分类我+ tablo.Name +\\\
{);
的WriteLine(\tpublic布尔保存(我的+ tablo.Name ++ tablo.Name +)\\\
\t {// \\\
\t\t做something\\ \
\t});
的WriteLine(\tpublic布尔删除(INT ID){\\\
\t // \\\
\t\t做something\\\
\t});
的WriteLine(\tpublic布尔更新(INT ID){\\\
\t // \\\
\t\t做something\\\
\t});
的WriteLine(});

}
#>


I am trying to generate the below code via T4.

using System;
using MyDAL;

namspace Test
{
// Select...
   public partial class MyCustomer
   {
      public int id { get ;set;}
      public string name { get ;set;}
      public string surname { get ;set;}

      public MyCustomer()
      {

      }

      public List<Customer> GetById(int id)
      {
         // do something...
      }
   }
        // Delete,Update,Save
   public partial class MyCustomer
   {
       public bool Save(new MyCustomer)
       {
         // do something...
       }

       public bool delete(int id)
       {
         // do something...
       }

       public bool Update(int id)
       {
         // do something...
       }
   }
}

The T4 template I have so far:


<#@ template language="C#"#>
<#@ assembly name="Microsoft.SqlServer.Management.Sdk.Sfc" #>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>



<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>

namespace DbClasses
{
    <#

    Server srv = new Server();
    Database db = new Database(srv,"NarDb");
    db.Refresh();
    foreach (Table tablo in db.Tables)
        {

             WriteLine("public class " +tablo.Name+"{");
            foreach (Column column in tablo.Columns)
                {
                    if(column.DataType.Name=="nvarchar")
                             WriteLine("public "+"string " +""+column.Name+";");
                    else
                         WriteLine("public "+"int " +""+column.Name+";");
                }
                WriteLine("}");



        }
   #>
}

解决方案

I'm not exactly sure what is the problem you are facing: but there is nothing wrong with your T4 code. The signature of the Save method in the second class has new and that's an error:

public bool Save(new MyCustomer)

should be

public bool Save(MyCustomer customer)

Also, GetById should return one element not a list.

I tried out the code you wrote without connecting to a database by writing dummy classes for Table, Column, etc and there is nothing wrong with the T4 code you wrote, except that, as per your requirements, the code your wrote doesn't generate partial classes, not does it create properties. It just creates a single non-partial class for each table with public fields (not properties). If you want to generate partial classes and properties (and methods) you're in the right direction (I added tabs and newlines to make it look pretty in the generated file, you can just press Ctrl+K, Ctrl+D there to reformat it though:

<#

foreach (Table tablo in tables)
{
//start class
WriteLine("public partial class My" +tablo.Name+"\n{");
foreach (Column column in tablo.Columns)
{
if(column.DataType.Name=="nvarchar")
WriteLine("\tpublic string " + column.Name+"{get; set;}");
else
WriteLine("\tpublic int " + column.Name+"{get; set;}");
}
//output the contructor
WriteLine("\tpublic My" + tablo.Name + "()\n\t{\n\t\t//constructor\n\t}" );
WriteLine("\tpublic List<" + tablo.Name + "> GetById(int id)\n\t{\n\t\t// do something\n\t}");
//end class
WriteLine("}");

//output comment and start class
WriteLine("//Delete,Update,Save");
WriteLine("public partial class My" +tablo.Name+"\n{");
WriteLine("\tpublic bool Save(My" + tablo.Name + " " + tablo.Name + ")\n\t{\n\t\t// do something\n\t}");
WriteLine("\tpublic bool Delete(int id)\n\t{\n\t\t// do something\n\t}");
WriteLine("\tpublic bool Update(int id)\n\t{\n\t\t// do something\n\t}");
WriteLine("}");

}
#>

这篇关于如何通过T4生成自定义类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆