我们现有的项目没有采用任何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 {
///
[Serializable]
[DataContract]
public class LoginPolicyEntity {
///
public int PolicyID{
get;set;
}
private int _UnfreezeTime = 30;
///
public int UnfreezeTime{
get{
return _UnfreezeTime;
}
set{
_UnfreezeTime = value;
}
}
private int _MaxFailedCount = 5;
///
public int MaxFailedCount{
get{
return _MaxFailedCount;
}
set{
_MaxFailedCount = value;
}
}
private bool? _EnableLoginPolicy = true;
///
public bool? EnableLoginPolicy{
get{
return _EnableLoginPolicy;
}
set{
_EnableLoginPolicy = value;
}
}
private DateTime _CreateTime = new DateTime();
///
public DateTime CreateTime{
get{
return _CreateTime;
}
set{
_CreateTime = value;
}
}
///
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 消息接收者,表变量
///