欢迎投稿

今日深度:

如何用T4模板生成数据实体(1)

如何用T4模板生成数据实体(1)


我们现有的项目没有采用任何ORM,所有的数据读取与操作都是基于存储过程的,在代码端使用 Enterprise Library 5 。 在 EntLib 和数据库之间,是基于我原来写的一个 T4 实体生成的模板,之前也没有详细的去整,反正能运行出结果就行了,总之,代码很乱。

最近一期项目告一段落,后续项目还没有上马,一手把这个部门建立起来的总监(经理)又离开了这个团队,我们几个老一批的员工也在思索着是否换换。趁着这个便当,我把这个东西在整出来,算是给我增加一个砝码吧。

什么是 T4 模板,自己去搜吧,怎么用也请自己搜吧。懂就懂,不懂我也懒得解释。

我将要贴出的T4模板是将 SQLServer 2008 的 Table, View , TableType, Procedure 解析为 C# 里的对应实体,形如下:

Table/View/TableType

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using System.Collections;  
  7. using System.ComponentModel;  
  8. using System.Runtime.Serialization;  
  9. namespace AsNum.MySecret.Entity.Database {  
  10.     /// <summary>  
  11.     /// Table : dbo.LoginPolicy  
  12.     /// 登陆策略  
  13.     /// 数据实体  
  14.     /// </summary>  
  15.     [Serializable]  
  16.     [DataContract]  
  17.     public class LoginPolicyEntity {  
  18.         /// <summary>  
  19.         /// 自动编号  
  20.         /// dbo.LoginPolicy.PolicyID  
  21.         /// 默认值  
  22.         /// </summary>  
  23.         public int PolicyID{  
  24.             get;set;  
  25.         }  
  26.         private int _UnfreezeTime = 30;  
  27.         /// <summary>  
  28.         /// 解冻时间  
  29.         /// dbo.LoginPolicy.UnfreezeTime  
  30.         /// 默认值((30))  
  31.         /// </summary>  
  32.         public int UnfreezeTime{  
  33.             get{  
  34.                 return _UnfreezeTime;  
  35.             }  
  36.             set{  
  37.                 _UnfreezeTime = value;  
  38.             }  
  39.         }  
  40.         private int _MaxFailedCount = 5;  
  41.         /// <summary>  
  42.         /// 最大失败次数  
  43.         /// dbo.LoginPolicy.MaxFailedCount  
  44.         /// 默认值((5))  
  45.         /// </summary>  
  46.         public int MaxFailedCount{  
  47.             get{  
  48.                 return _MaxFailedCount;  
  49.             }  
  50.             set{  
  51.                 _MaxFailedCount = value;  
  52.             }  
  53.         }  
  54.         private bool? _EnableLoginPolicy = true;  
  55.         /// <summary>  
  56.         /// 是否启用登陆策略  
  57.         /// dbo.LoginPolicy.EnableLoginPolicy  
  58.         /// 默认值((1))  
  59.         /// </summary>  
  60.         public bool? EnableLoginPolicy{  
  61.             get{  
  62.                 return _EnableLoginPolicy;  
  63.             }  
  64.             set{  
  65.                 _EnableLoginPolicy = value;  
  66.             }  
  67.         }  
  68.         private DateTime _CreateTime = new DateTime();  
  69.         /// <summary>  
  70.         ///  
  71.         /// dbo.LoginPolicy.CreateTime  
  72.         /// 默认值(getdate())  
  73.         /// </summary>  
  74.         public DateTime CreateTime{  
  75.             get{  
  76.                 return _CreateTime;  
  77.             }  
  78.             set{  
  79.                 _CreateTime = value;  
  80.             }  
  81.         }  
  82.         /// <summary>  
  83.         ///  
  84.         /// dbo.LoginPolicy.Creator  
  85.         /// 默认值  
  86.         /// </summary>  
  87.         public string Creator{  
  88.             get;set;  
  89.         }  
  90.     }  

Procedure

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using System.Collections;  
  7. using System.ComponentModel;  
  8. using System.Data.SqlClient;  
  9. using Microsoft.Practices.EnterpriseLibrary.Data;  
  10. using AsNum.MySecret.Entity.Database;  
  11. using AsNum.Common.Extend;  
  12. namespace AsNum.MySecret.DB {  
  13.     ///   
  14.     ///  
  15.     ///  
  16.     public partial class SPs {  
  17.         ///   
  18.         /// 发送消息  
  19.         ///   
  20.         ///   
  21.         /// @Title 消息标题  
  22.         /// @Ctx 消息内容  
  23.         /// @UserID 用户ID  
  24.         /// @FromIP 发消息的IP  
  25.         /// @IntranetIP 发消息的内网IP,用于扩展  
  26.         /// @IsPublic 是否公开  
  27.         /// @Receiver 消息接收者,表变量  
  28.         ///   
  29.         public static SqlCommand SendMsg(Database db, string title , string ctx , int? userID , long? fromIP , long? intranetIP , long? isPublic , List receiver){  
  30.             if(db == null)  
  31.                 throw new ArgumentNullException("db");  
  32.             SqlCommand sc = new SqlCommand("SendMsg");  
  33.             sc.CommandType = CommandType.StoredProcedure;  
  34.             db.AddParameter(sc , "@Title" , DbType.String , 100 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , title);  
  35.             db.AddParameter(sc , "@Ctx" , DbType.String , 500 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , ctx);  
  36.             db.AddParameter(sc , "@UserID" , DbType.Int32 , 4 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , userID);  
  37.             db.AddParameter(sc , "@FromIP" , DbType.Int64 , 8 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , fromIP);  
  38.             db.AddParameter(sc , "@IntranetIP" , DbType.Int64 , 8 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , intranetIP);  
  39.             db.AddParameter(sc , "@IsPublic" , DbType.Int64 , 8 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , isPublic);  
  40.             db.AddParameter(sc , "@Receiver" , DbType.Object , -1 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , receiver.ToDataTable());  
  41.             return sc;  
  42.         }  
  43.     }  

可以看到产生的存储过程调用方法并没有获取到参数的默认值,这个是因为(http://msdn.microsoft.com/en-us/library/ms176074.aspx):

SQL Server only maintains default values for CLR objects in this catalog view;

不过,有牛人做出来了,前提是存储过程没有加密:

http://www.codeproject.com/KB/database/FindDefaultValueSPParams.aspx


www.htsjk.Com true http://www.htsjk.com/shujukukf/17019.html NewsArticle 如何用T4模板生成数据实体(1) 我们现有的项目没有采用任何ORM,所有的数据读取与操作都是基于存储过程的,在代码端使用 Enterprise Library 5 。 在 EntLib 和数据库之间,是基于我原来写的...
相关文章
    暂无相关文章
评论暂时关闭