自己动手实现简易代码生成器、采用文本模板文件生成服务层、服务层接口代码的做法参考
2011-01-23 19:43 通用C#系统架构 阅读(3485) 评论(17) 编辑 收藏 举报最近受到 单程列车 http://www.cnblogs.com/zhaojingjing/ 的启发,让我做一个模板文件来生成代码,效果会很好,我就接纳了人家善意的提醒,周六晚上就改进了一下代码生成器,折腾了一个多小时,一个简易的按模板替换的代码生成器功能实现好了,非常简单好用,现在分享给大家。
模板文件里用了 namespace #Company#.#Project#.Service, #?# 进行替换的思路,设置了文本模板文件,然后用代码生成器进行后台的代码,方便高效一些,代码质量也有保障一些。读取文件的功能函数参考如下:
string file = Application.StartupPath + "\\Templates\\IService.txt";
string code = GetTemplate(file);
code = ReplaceTemplate(code);
this.txtCode.SettxtContent("c#", code);
string code = GetTemplate(file);
code = ReplaceTemplate(code);
this.txtCode.SettxtContent("c#", code);
private string GetTemplate(string file)
{
string code = string.Empty;
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (StreamReader streamReader = new StreamReader(fileStream, Encoding.Default))
{
code = streamReader.ReadToEnd();
}
return code;
}
{
string code = string.Empty;
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (StreamReader streamReader = new StreamReader(fileStream, Encoding.Default))
{
code = streamReader.ReadToEnd();
}
return code;
}
模板文件的替换参考如下:
private string ReplaceTemplate(string code)
{
code = code.Replace("#Author#", this.txtAuthor.Text);
code = code.Replace("#ClassName#", this.txtClassName.Text);
code = code.Replace("#Code#", this.txtCode.Text);
code = code.Replace("#Company#", this.txtCompany.Text);
code = code.Replace("#DateCreated#", this.txtDateCreated.Text);
code = code.Replace("#Project#", this.txtProject.Text);
code = code.Replace("#YearCreated#", this.txtYearCreated.Text);
return code;
}
{
code = code.Replace("#Author#", this.txtAuthor.Text);
code = code.Replace("#ClassName#", this.txtClassName.Text);
code = code.Replace("#Code#", this.txtCode.Text);
code = code.Replace("#Company#", this.txtCompany.Text);
code = code.Replace("#DateCreated#", this.txtDateCreated.Text);
code = code.Replace("#Project#", this.txtProject.Text);
code = code.Replace("#YearCreated#", this.txtYearCreated.Text);
return code;
}
程序的运行效果如下:
按模板生成的代码主要功能部分是这2个按钮:
按模板替换的只要有以下2个模板文件,模板文件以文本的方式存了代码文件。
服务程序接口模板文件的参考如下: IService.txt
代码
//------------------------------------------------------------
// All Rights Reserved , Copyright (C) #YearCreated# , #Company# , Ltd.
//------------------------------------------------------------
using System.Data;
using System.ServiceModel;
using System.Collections.Generic;
namespace #Company#.#Project#.IService
{
using DotNet.Model;
using DotNet.Utilities;
/// <summary>
/// I#ClassName#Service
/// 服务层接口
///
/// 修改纪录
///
/// #DateCreated# 版本:1.0 #Author# 创建文件。
///
/// 版本:1.0
///
/// <author>
/// <name>#Author#</name>
/// <date>#DateCreated#</date>
/// </author>
/// </summary>
[ServiceContract]
public interface I#ClassName#Service
{
/// <summary>
/// 添加实体
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="entity">实体</param>
/// <param name="statusCode">返回状态码</param>
/// <param name="statusMessage">返回状态信息</param>
/// <returns>主键</returns>
[OperationContract]
string Add(BaseUserInfo userInfo, #ClassName#Entity entity, out string statusCode, out string statusMessage);
/// <summary>
/// 获取列表
/// </summary>
/// <param name="userInfo">操作员</param>
/// <returns>数据表</returns>
[OperationContract]
DataTable GetDT(BaseUserInfo userInfo);
/// <summary>
/// 获取实体
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="id">主键</param>
/// <returns>实体</returns>
[OperationContract]
#ClassName#Entity GetEntity(BaseUserInfo userInfo, string id);
/// <summary>
/// 编辑
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="entity">实体</param>
/// <param name="statusCode">返回状态码</param>
/// <param name="statusMessage">返回状态信息</param>
/// <returns>影响行数</returns>
[OperationContract]
int Update(BaseUserInfo userInfo, #ClassName#Entity entity, out string statusCode, out string statusMessage);
/// <summary>
/// 获取数据列表
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="ids">主键</param>
/// <returns>数据表</returns>
[OperationContract]
DataTable GetDTByIds(BaseUserInfo userInfo, string[] ids);
/// <summary>
/// 批量保存
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="entites">实体列表</param>
/// <returns>影响行数</returns>
[OperationContract]
int BatchSave(BaseUserInfo userInfo, List<#ClassName#Entity> entites);
/// <summary>
/// 删除
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="id">主键</param>
/// <returns>数据表</returns>
[OperationContract]
int Delete(BaseUserInfo userInfo, string id);
/// <summary>
/// 批量删除
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="ids">主键数组</param>
/// <returns>影响行数</returns>
[OperationContract]
int BatchDelete(BaseUserInfo userInfo, string[] ids);
/// <summary>
/// 批量做删除标志
/// </summary>
/// <param name="userInfo">用户</param>
/// <param name="ids">主键数组</param>
/// <returns>影响行数</returns>
[OperationContract]
int SetDeleted(BaseUserInfo userInfo, string[] ids);
}
}
// All Rights Reserved , Copyright (C) #YearCreated# , #Company# , Ltd.
//------------------------------------------------------------
using System.Data;
using System.ServiceModel;
using System.Collections.Generic;
namespace #Company#.#Project#.IService
{
using DotNet.Model;
using DotNet.Utilities;
/// <summary>
/// I#ClassName#Service
/// 服务层接口
///
/// 修改纪录
///
/// #DateCreated# 版本:1.0 #Author# 创建文件。
///
/// 版本:1.0
///
/// <author>
/// <name>#Author#</name>
/// <date>#DateCreated#</date>
/// </author>
/// </summary>
[ServiceContract]
public interface I#ClassName#Service
{
/// <summary>
/// 添加实体
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="entity">实体</param>
/// <param name="statusCode">返回状态码</param>
/// <param name="statusMessage">返回状态信息</param>
/// <returns>主键</returns>
[OperationContract]
string Add(BaseUserInfo userInfo, #ClassName#Entity entity, out string statusCode, out string statusMessage);
/// <summary>
/// 获取列表
/// </summary>
/// <param name="userInfo">操作员</param>
/// <returns>数据表</returns>
[OperationContract]
DataTable GetDT(BaseUserInfo userInfo);
/// <summary>
/// 获取实体
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="id">主键</param>
/// <returns>实体</returns>
[OperationContract]
#ClassName#Entity GetEntity(BaseUserInfo userInfo, string id);
/// <summary>
/// 编辑
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="entity">实体</param>
/// <param name="statusCode">返回状态码</param>
/// <param name="statusMessage">返回状态信息</param>
/// <returns>影响行数</returns>
[OperationContract]
int Update(BaseUserInfo userInfo, #ClassName#Entity entity, out string statusCode, out string statusMessage);
/// <summary>
/// 获取数据列表
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="ids">主键</param>
/// <returns>数据表</returns>
[OperationContract]
DataTable GetDTByIds(BaseUserInfo userInfo, string[] ids);
/// <summary>
/// 批量保存
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="entites">实体列表</param>
/// <returns>影响行数</returns>
[OperationContract]
int BatchSave(BaseUserInfo userInfo, List<#ClassName#Entity> entites);
/// <summary>
/// 删除
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="id">主键</param>
/// <returns>数据表</returns>
[OperationContract]
int Delete(BaseUserInfo userInfo, string id);
/// <summary>
/// 批量删除
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="ids">主键数组</param>
/// <returns>影响行数</returns>
[OperationContract]
int BatchDelete(BaseUserInfo userInfo, string[] ids);
/// <summary>
/// 批量做删除标志
/// </summary>
/// <param name="userInfo">用户</param>
/// <param name="ids">主键数组</param>
/// <returns>影响行数</returns>
[OperationContract]
int SetDeleted(BaseUserInfo userInfo, string[] ids);
}
}
服务程序模板文件的参考如下:Service.txt
代码
//------------------------------------------------------------
// All Rights Reserved , Copyright (C) #YearCreated# , #Company# , Ltd.
//------------------------------------------------------------
using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
namespace #Company#.#Project#.Service
{
using DotNet.Business;
using DotNet.DbUtilities;
using DotNet.Model;
using DotNet.Utilities;
using DotNet.IService;
/// <summary>
/// #ClassName#Service
/// 服务层
///
/// 修改纪录
///
/// #DateCreated# 版本:1.0 #Author# 创建文件。
///
/// 版本:1.0
///
/// <author>
/// <name>#Author#</name>
/// <date>#DateCreated#</date>
/// </author>
/// </summary>
public class #ClassName#Service : System.MarshalByRefObject, I#ClassName#Service
{
/// <summary>
/// 业务数据库连接
/// </summary>
private readonly string BusinessDbConnection = BaseSystemInfo.BusinessDbConnection;
/// <summary>
/// 添加实体
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="entity">实体</param>
/// <param name="statusCode">返回状态码</param>
/// <param name="statusMessage">返回状态信息</param>
/// <returns>主键</returns>
public string Add(BaseUserInfo userInfo, #ClassName#Entity entity, out string statusCode, out string statusMessage)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
string returnValue = string.Empty;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
returnValue = manager.AddEntity(entity);
// returnValue = manager.Add(entity, out statusCode);
statusMessage = manager.GetStateMessage(statusCode);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return returnValue;
}
/// <summary>
/// 获取列表
/// </summary>
/// <param name="userInfo">操作员</param>
/// <returns>数据表</returns>
public DataTable GetDT(BaseUserInfo userInfo)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
DataTable dataTable = new DataTable(#ClassName#Table.TableName);
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
// 获得列表
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
dataTable = manager.GetDT(#ClassName#Table.FieldDeletionStateCode, 0, #ClassName#Table.FieldSortCode);
dataTable.TableName = #ClassName#Table.TableName;
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return dataTable;
}
/// <summary>
/// 获取实体
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="id">主键</param>
/// <returns>实体</returns>
public #ClassName#Entity GetEntity(BaseUserInfo userInfo, string id)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
#ClassName#Entity entity = null;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
entity = manager.GetEntity(id);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return entity;
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="entity">实体</param>
/// <param name="statusCode">返回状态码</param>
/// <param name="statusMessage">返回状态信息</param>
/// <returns>影响行数</returns>
public int Update(BaseUserInfo userInfo, #ClassName#Entity entity, out string statusCode, out string statusMessage)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
int returnValue = 0;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
returnValue = manager.UpdateEntity(entity);
// returnValue = manager.Update(entity, out statusCode);
statusMessage = manager.GetStateMessage(statusCode);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return returnValue;
}
/// <summary>
/// 获取数据列表
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="ids">主键</param>
/// <returns>数据表</returns>
public DataTable GetDTByIds(BaseUserInfo userInfo, string[] ids)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
DataTable dataTable = new DataTable(#ClassName#Table.TableName);
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
dataTable = manager.GetDT(#ClassName#Table.FieldId, ids, #ClassName#Table.FieldSortCode);
dataTable.TableName = #ClassName#Table.TableName;
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return dataTable;
}
/// <summary>
/// 批量保存
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="entites">实体列表</param>
/// <returns>影响行数</returns>
public int BatchSave(BaseUserInfo userInfo, List<#ClassName#Entity> entites)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
int returnValue = 0;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
// returnValue = manager.BatchSave(entites);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return returnValue;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="id">主键</param>
/// <returns>数据表</returns>
public int Delete(BaseUserInfo userInfo, string id)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
int returnValue = 0;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
returnValue = manager.Delete(id);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return returnValue;
}
/// <summary>
/// 批量删除
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="ids">主键数组</param>
/// <returns>影响行数</returns>
public int BatchDelete(BaseUserInfo userInfo, string[] ids)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
int returnValue = 0;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
// 开始数据库事务
dbHelper.BeginTransaction();
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
returnValue = manager.BatchDelete(ids);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
// 递交数据库事务
dbHelper.CommitTransaction();
}
catch (Exception ex)
{
// 撤销数据库事务
dbHelper.RollbackTransaction();
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return returnValue;
}
/// <summary>
/// 批量做删除标志
/// </summary>
/// <param name="userInfo">用户</param>
/// <param name="ids">主键数组</param>
/// <returns>影响行数</returns>
public int SetDeleted(BaseUserInfo userInfo, string[] ids)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
int returnValue = 0;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
returnValue = manager.SetDeleted(ids);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return returnValue;
}
}
}
// All Rights Reserved , Copyright (C) #YearCreated# , #Company# , Ltd.
//------------------------------------------------------------
using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
namespace #Company#.#Project#.Service
{
using DotNet.Business;
using DotNet.DbUtilities;
using DotNet.Model;
using DotNet.Utilities;
using DotNet.IService;
/// <summary>
/// #ClassName#Service
/// 服务层
///
/// 修改纪录
///
/// #DateCreated# 版本:1.0 #Author# 创建文件。
///
/// 版本:1.0
///
/// <author>
/// <name>#Author#</name>
/// <date>#DateCreated#</date>
/// </author>
/// </summary>
public class #ClassName#Service : System.MarshalByRefObject, I#ClassName#Service
{
/// <summary>
/// 业务数据库连接
/// </summary>
private readonly string BusinessDbConnection = BaseSystemInfo.BusinessDbConnection;
/// <summary>
/// 添加实体
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="entity">实体</param>
/// <param name="statusCode">返回状态码</param>
/// <param name="statusMessage">返回状态信息</param>
/// <returns>主键</returns>
public string Add(BaseUserInfo userInfo, #ClassName#Entity entity, out string statusCode, out string statusMessage)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
string returnValue = string.Empty;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
returnValue = manager.AddEntity(entity);
// returnValue = manager.Add(entity, out statusCode);
statusMessage = manager.GetStateMessage(statusCode);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return returnValue;
}
/// <summary>
/// 获取列表
/// </summary>
/// <param name="userInfo">操作员</param>
/// <returns>数据表</returns>
public DataTable GetDT(BaseUserInfo userInfo)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
DataTable dataTable = new DataTable(#ClassName#Table.TableName);
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
// 获得列表
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
dataTable = manager.GetDT(#ClassName#Table.FieldDeletionStateCode, 0, #ClassName#Table.FieldSortCode);
dataTable.TableName = #ClassName#Table.TableName;
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return dataTable;
}
/// <summary>
/// 获取实体
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="id">主键</param>
/// <returns>实体</returns>
public #ClassName#Entity GetEntity(BaseUserInfo userInfo, string id)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
#ClassName#Entity entity = null;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
entity = manager.GetEntity(id);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return entity;
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="entity">实体</param>
/// <param name="statusCode">返回状态码</param>
/// <param name="statusMessage">返回状态信息</param>
/// <returns>影响行数</returns>
public int Update(BaseUserInfo userInfo, #ClassName#Entity entity, out string statusCode, out string statusMessage)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
int returnValue = 0;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
returnValue = manager.UpdateEntity(entity);
// returnValue = manager.Update(entity, out statusCode);
statusMessage = manager.GetStateMessage(statusCode);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return returnValue;
}
/// <summary>
/// 获取数据列表
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="ids">主键</param>
/// <returns>数据表</returns>
public DataTable GetDTByIds(BaseUserInfo userInfo, string[] ids)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
DataTable dataTable = new DataTable(#ClassName#Table.TableName);
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
dataTable = manager.GetDT(#ClassName#Table.FieldId, ids, #ClassName#Table.FieldSortCode);
dataTable.TableName = #ClassName#Table.TableName;
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return dataTable;
}
/// <summary>
/// 批量保存
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="entites">实体列表</param>
/// <returns>影响行数</returns>
public int BatchSave(BaseUserInfo userInfo, List<#ClassName#Entity> entites)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
int returnValue = 0;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
// returnValue = manager.BatchSave(entites);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return returnValue;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="id">主键</param>
/// <returns>数据表</returns>
public int Delete(BaseUserInfo userInfo, string id)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
int returnValue = 0;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
returnValue = manager.Delete(id);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return returnValue;
}
/// <summary>
/// 批量删除
/// </summary>
/// <param name="userInfo">操作员</param>
/// <param name="ids">主键数组</param>
/// <returns>影响行数</returns>
public int BatchDelete(BaseUserInfo userInfo, string[] ids)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
int returnValue = 0;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
// 开始数据库事务
dbHelper.BeginTransaction();
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
returnValue = manager.BatchDelete(ids);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
// 递交数据库事务
dbHelper.CommitTransaction();
}
catch (Exception ex)
{
// 撤销数据库事务
dbHelper.RollbackTransaction();
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return returnValue;
}
/// <summary>
/// 批量做删除标志
/// </summary>
/// <param name="userInfo">用户</param>
/// <param name="ids">主键数组</param>
/// <returns>影响行数</returns>
public int SetDeleted(BaseUserInfo userInfo, string[] ids)
{
// 写入调试信息
#if (DEBUG)
int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
int returnValue = 0;
IDbHelper dbHelper = DbHelperFactory.GetHelper();
try
{
dbHelper.Open(BusinessDbConnection);
#ClassName#Manager manager = new #ClassName#Manager(dbHelper, userInfo);
returnValue = manager.SetDeleted(ids);
BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
}
catch (Exception ex)
{
BaseExceptionManager.LogException(dbHelper, userInfo, ex);
throw ex;
}
finally
{
dbHelper.Close();
}
// 写入调试信息
#if (DEBUG)
BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
return returnValue;
}
}
}
将权限管理、工作流管理做到我能力的极致,一个人只能做好那么很少的几件事情。