如何用T4模板生成数据实体(1)
我们现有的项目没有采用任何ORM,所有的数据读取与操作都是基于存储过程的,在代码端使用 Enterprise Library 5 。 在 EntLib 和数据库之间,是基于我原来写的一个 T4 实体生成的模板,之前也没有详细的去整,反正能运行出结果就行了,总之,代码很乱。
最近一期项目告一段落,后续项目还没有上马,一手把这个部门建立起来的总监(经理)又离开了这个团队,我们几个老一批的员工也在思索着是否换换。趁着这个便当,我把这个东西在整出来,算是给我增加一个砝码吧。
什么是 T4 模板,自己去搜吧,怎么用也请自己搜吧。懂就懂,不懂我也懒得解释。
我将要贴出的T4模板是将 SQLServer 2008 的 Table, View , TableType, Procedure 解析为 C# 里的对应实体,形如下:
Table/View/TableType
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using System.Collections;
- using System.ComponentModel;
- using System.Runtime.Serialization;
- namespace AsNum.MySecret.Entity.Database {
- /// <summary>
- /// Table : dbo.LoginPolicy
- /// 登陆策略
- /// 数据实体
- /// </summary>
- [Serializable]
- [DataContract]
- public class LoginPolicyEntity {
- /// <summary>
- /// 自动编号
- /// dbo.LoginPolicy.PolicyID
- /// 默认值
- /// </summary>
- public int PolicyID{
- get;set;
- }
- private int _UnfreezeTime = 30;
- /// <summary>
- /// 解冻时间
- /// dbo.LoginPolicy.UnfreezeTime
- /// 默认值((30))
- /// </summary>
- public int UnfreezeTime{
- get{
- return _UnfreezeTime;
- }
- set{
- _UnfreezeTime = value;
- }
- }
- private int _MaxFailedCount = 5;
- /// <summary>
- /// 最大失败次数
- /// dbo.LoginPolicy.MaxFailedCount
- /// 默认值((5))
- /// </summary>
- public int MaxFailedCount{
- get{
- return _MaxFailedCount;
- }
- set{
- _MaxFailedCount = value;
- }
- }
- private bool? _EnableLoginPolicy = true;
- /// <summary>
- /// 是否启用登陆策略
- /// dbo.LoginPolicy.EnableLoginPolicy
- /// 默认值((1))
- /// </summary>
- public bool? EnableLoginPolicy{
- get{
- return _EnableLoginPolicy;
- }
- set{
- _EnableLoginPolicy = value;
- }
- }
- private DateTime _CreateTime = new DateTime();
- /// <summary>
- ///
- /// dbo.LoginPolicy.CreateTime
- /// 默认值(getdate())
- /// </summary>
- public DateTime CreateTime{
- get{
- return _CreateTime;
- }
- set{
- _CreateTime = value;
- }
- }
- /// <summary>
- ///
- /// dbo.LoginPolicy.Creator
- /// 默认值
- /// </summary>
- public string Creator{
- get;set;
- }
- }
- }
Procedure
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using System.Collections;
- using System.ComponentModel;
- using System.Data.SqlClient;
- using Microsoft.Practices.EnterpriseLibrary.Data;
- using AsNum.MySecret.Entity.Database;
- using AsNum.Common.Extend;
- namespace AsNum.MySecret.DB {
- ///
- ///
- ///
- public partial class SPs {
- ///
- /// 发送消息
- ///
- ///
- /// @Title 消息标题
- /// @Ctx 消息内容
- /// @UserID 用户ID
- /// @FromIP 发消息的IP
- /// @IntranetIP 发消息的内网IP,用于扩展
- /// @IsPublic 是否公开
- /// @Receiver 消息接收者,表变量
- ///
- public static SqlCommand SendMsg(Database db, string title , string ctx , int? userID , long? fromIP , long? intranetIP , long? isPublic , List receiver){
- if(db == null)
- throw new ArgumentNullException("db");
- SqlCommand sc = new SqlCommand("SendMsg");
- sc.CommandType = CommandType.StoredProcedure;
- db.AddParameter(sc , "@Title" , DbType.String , 100 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , title);
- db.AddParameter(sc , "@Ctx" , DbType.String , 500 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , ctx);
- db.AddParameter(sc , "@UserID" , DbType.Int32 , 4 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , userID);
- db.AddParameter(sc , "@FromIP" , DbType.Int64 , 8 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , fromIP);
- db.AddParameter(sc , "@IntranetIP" , DbType.Int64 , 8 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , intranetIP);
- db.AddParameter(sc , "@IsPublic" , DbType.Int64 , 8 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , isPublic);
- db.AddParameter(sc , "@Receiver" , DbType.Object , -1 , ParameterDirection.Input , true , 0 , 0 , String.Empty , DataRowVersion.Default , receiver.ToDataTable());
- return sc;
- }
- }
- }
可以看到产生的存储过程调用方法并没有获取到参数的默认值,这个是因为(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
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。