1.邏輯層的方法應該與數據層的方法一一對應。邏輯層返回的結果都是用接口IResult封裝,用於項目轉換時,能減少變化的代碼量。

2.邏輯層都需要繼承 BaseLogic 類

  public  class BaseLogic
{
public T NewObject<T>() where T : class
{
T t = IOCHelper.GetInstance<T>();
return t;
} }

3.動軟代碼

<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".cs" #>
<#
TableHost host = (TableHost)(Host);
string ModelSpace = host.NameSpace+".Model."+ host.GetModelClass(host.TableName);
string DALSpace= host.NameSpace+".DAL."+ host.GetDALClass(host.TableName);
string TableName = host.GetModelClass(host.TableName).Split('_')[];
ColumnInfo identityKey=host.IdentityKey;
string returnValue = "void";
if (identityKey!=null)
{
returnValue = CodeCommon.DbTypeToCS(identityKey.TypeName);
}
#> using System;
using CommonLibrary.Log;
using CommonLibrary.Base;
using CommonContract.Entity;
using CommonContract.Condition;
using System.Collections.Generic;
using MIT.Application.Dao;
using CommonLibrary.CommonResult; namespace MIT.Application.Logic
{
public class <#= TableName #>Logic : BaseLogic
{
#region 獲取所有數據
public IResult<List<<#= TableName #>Entity>> GetList ()
{
IResult<List<<#= TableName #>Entity>> result = new Result<List<<#= TableName #>Entity>>();
List<<#= TableName #>Entity> list = null;
try
{
list = NewObject<<#= TableName #>Dao>().GetList();
result.SetSuccessOrFail(true);
result.SetResult(list);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "<#= TableName #>表 獲取所有數據異常");
result.SetResultDescription("获取列表出错");
}
return result;
}
#endregion #region 通過緩存獲取所有數據
public IResult<List<<#= TableName #>Entity>> GetListByCache ()
{
IResult<List<<#= TableName #>Entity>> result = new Result<List<<#= TableName #>Entity>>();
List<<#= TableName #>Entity> list = null;
try
{
list = NewObject<<#= TableName #>Dao>().GetListByCache();
result.SetSuccessOrFail(true);
result.SetResult(list);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "<#= TableName #>表 通過緩存獲取所有數據異常");
result.SetResultDescription("获取列表出错");
}
return result;
}
#endregion #region 通過條件獲取所有數據
public IResult<List<<#= TableName #>Entity>> GetListByCondition (string queryJson)
{
IResult<List<<#= TableName #>Entity>> result = new Result<List<<#= TableName #>Entity>>();
List<<#= TableName #>Entity> list = null;
try
{
list = NewObject<<#= TableName #>Dao>().GetListByCondition(queryJson);
result.SetSuccessOrFail(true);
result.SetResult(list);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "<#= TableName #>表 通過條件獲取所有數據異常");
result.SetResultDescription("获取列表出错");
}
return result;
}
#endregion #region 通過分页獲取數據
public IResult<List<<#= TableName #>Entity>> GetListByPage (PageInfo pageInfo, string queryJson)
{
IResult<List<<#= TableName #>Entity>> result = new Result<List<<#= TableName #>Entity>>();
List<<#= TableName #>Entity> list = null;
try
{
list = NewObject<<#= TableName #>Dao>().GetListByPage(pageInfo,queryJson);
result.SetSuccessOrFail(true);
result.SetResult(list);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "<#= TableName #>表 通過分页獲取數據列表異常");
result.SetResultDescription("获取列表出错");
}
return result;
}
#endregion #region 通過主鍵獲取實體
public IResult<<#= TableName #>Entity> GetEntity (string keyValue)
{
IResult<<#= TableName #>Entity> result = new Result<<#= TableName #>Entity>();
<#= TableName #>Entity Entity = null;
try
{
Entity = NewObject<<#= TableName #>Dao>().GetEntity(keyValue);
result.SetSuccessOrFail(true);
result.SetResult(Entity);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "<#= TableName #>表 通過主鍵獲取實體異常");
result.SetResultDescription("获取數據出错");
}
return result;
}
#endregion #region 通過主鍵删數據
public IResult RemoveForm (string keyValue)
{
IResult result = new Result();
try
{
int res = NewObject<<#= TableName #>Dao>().RemoveForm(keyValue); result.SetSuccessOrFail(res > );
string msg = res > ? "刪除成功" : "刪除失敗";
result.SetResultDescription(msg);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "<#= TableName #>表 通過主鍵删數據異常");
result.SetResultDescription("删數數據出错");
}
return result;
}
#endregion #region 通過條件删數據
public IResult RemoveFormByCondition (string queryJson)
{
IResult result = new Result();
try
{
int res = NewObject<<#= TableName #>Dao>().RemoveFormByCondition(queryJson);
result.SetSuccessOrFail(res > );
string msg = res > ? "刪除成功" : "刪除失敗";
result.SetResultDescription(msg);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "<#= TableName #>表 通過條件删數據異常");
result.SetResultDescription("删數數據出错");
}
return result;
}
#endregion #region 批量删除數據
public IResult RemoveFormByKeys (string strkeyValue)
{
IResult result = new Result();
try
{
string[] strArr=strkeyValue.Split(',');
int[] keyValues=new int[strArr.Length];
for(int i=;i<strArr.Length;i++)
{
keyValues[i]=Convert.ToInt32(strArr[i]);
}
int res = NewObject<<#= TableName #>Dao>().RemoveFormByKeys(keyValues);
result.SetSuccessOrFail(res > );
string msg = res > ? "刪除成功" : "刪除失敗";
result.SetResultDescription(msg);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "<#= TableName #>表 批量删除數據異常");
result.SetResultDescription("删數數據出错");
}
return result;
}
#endregion #region 保存數據(新增、修改)
public IResult SaveForm (string keyValue, <#= TableName #>Entity Entity)
{
IResult result = new Result();
try
{
int res = NewObject<<#= TableName #>Dao>().SaveForm(keyValue,Entity);
string msg = res > ? "保存成功" : "保存失敗";
if (string.IsNullOrEmpty(keyValue))
{
msg = res > ? "新增成功" : "新增失敗";
}
result.SetResultDescription(msg);
result.SetSuccessOrFail(res > );
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "<#= TableName #>表 保存數據(新增、修改)異常");
result.SetResultDescription("操作失敗");
}
return result;
}
#endregion }
}

4.C#代碼

using System;
using CommonLibrary.Log;
using CommonLibrary.Base;
using CommonContract.Entity;
using System.Collections.Generic;
using CommonLibrary.CommonResult;
using MIT.Application.Dao.BaseManager;
using CommonContract.Condition; namespace MIT.Application.Logic.BaseManager
{
public class UserLogic : BaseLogic
{
#region 獲取所有數據
public IResult<List<UserEntity>> GetList(UserEntity Entity)
{
IResult<List<UserEntity>> result = new Result<List<UserEntity>>();
List<UserEntity> list = null;
try
{
list = NewObject<UserDao>().GetList();
result.SetSuccessOrFail(true);
result.SetResult(list);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "UserInfo表 獲取所有數據異常");
result.SetResultDescription("获取列表出错");
}
return result;
}
#endregion #region 通過緩存獲取所有數據
public IResult<List<UserEntity>> GetListByCache(UserEntity Entity)
{
IResult<List<UserEntity>> result = new Result<List<UserEntity>>();
List<UserEntity> list = null;
try
{
list = NewObject<UserDao>().GetListByCache();
result.SetSuccessOrFail(true);
result.SetResult(list);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "UserInfo表 通過緩存獲取所有數據異常");
result.SetResultDescription("获取列表出错");
}
return result;
}
#endregion #region 通過條件獲取所有數據
public IResult<List<UserEntity>> GetListByCondition(string queryJson)
{
IResult<List<UserEntity>> result = new Result<List<UserEntity>>();
List<UserEntity> list = null;
try
{
list = NewObject<UserDao>().GetListByCondition(queryJson);
result.SetSuccessOrFail(true);
result.SetResult(list);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "UserInfo表 通過條件獲取所有數據異常");
result.SetResultDescription("获取列表出错");
}
return result;
}
#endregion #region 通過分页獲取數據
public IResult<List<UserEntity>> GetListByPage(PageInfo pageInfo, string queryJson)
{
IResult<List<UserEntity>> result = new Result<List<UserEntity>>();
List<UserEntity> list = null;
try
{
list = NewObject<UserDao>().GetListByPage(pageInfo, queryJson);
result.SetSuccessOrFail(true);
result.SetResult(list);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "UserInfo表 通過分页獲取數據列表異常");
result.SetResultDescription("获取列表出错");
}
return result;
}
#endregion #region 通過主鍵獲取實體
public IResult<UserEntity> GetEntity(string keyValue)
{
IResult<UserEntity> result = new Result<UserEntity>();
UserEntity Entity = null;
try
{
Entity = NewObject<UserDao>().GetEntity(keyValue);
result.SetSuccessOrFail(true);
result.SetResult(Entity);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "UserInfo表 通過主鍵獲取實體異常");
result.SetResultDescription("获取數據出错");
}
return result;
}
#endregion #region 通過主鍵删數據
public IResult RemoveForm(string keyValue)
{
IResult result = new Result();
try
{
int res = NewObject<UserDao>().RemoveForm(keyValue); result.SetSuccessOrFail(res > );
string msg = res > ? "刪除成功" : "刪除失敗";
result.SetResultDescription(msg);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "UserInfo表 通過主鍵删數據異常");
result.SetResultDescription("删數數據出错");
}
return result;
}
#endregion #region 通過條件删數據
public IResult RemoveFormByCondition(string queryJson)
{
IResult result = new Result();
try
{
int res = NewObject<UserDao>().RemoveFormByCondition(queryJson);
result.SetSuccessOrFail(res > );
string msg = res > ? "刪除成功" : "刪除失敗";
result.SetResultDescription(msg);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "UserInfo表 通過條件删數據異常");
result.SetResultDescription("删數數據出错");
}
return result;
}
#endregion #region 批量删除數據
public IResult RemoveFormByKeys(string strkeyValue)
{
IResult result = new Result();
try
{
string[] strArr = strkeyValue.Split(',');
int[] keyValues = new int[strArr.Length];
for (int i = ; i < strArr.Length; i++)
{
keyValues[i] = Convert.ToInt32(strArr[i]);
}
int res = NewObject<UserDao>().RemoveFormByKeys(keyValues);
result.SetSuccessOrFail(res > );
string msg = res > ? "刪除成功" : "刪除失敗";
result.SetResultDescription(msg);
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "UserInfo表 批量删除數據異常");
result.SetResultDescription("删數數據出错");
}
return result;
}
#endregion #region 保存數據(新增、修改)
public IResult SaveForm(string keyValue, UserEntity Entity)
{
IResult result = new Result();
try
{
int res = NewObject<UserDao>().SaveForm(keyValue,Entity);
string msg = res > ? "保存成功" : "保存失敗";
if (string.IsNullOrEmpty(keyValue))
{
msg = res > ? "新增成功" : "新增失敗";
}
result.SetResultDescription(msg);
result.SetSuccessOrFail(res > );
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "UserInfo表 保存數據(新增、修改)異常");
result.SetResultDescription("操作失敗");
}
return result;
}
#endregion #region 修改用户状态
public IResult UpdateState(string keyValue, int Status)
{
IResult result = new Result();
try
{
UserEntity Entity = new UserEntity();
Entity.UserId = keyValue.ToInt();
Entity.EnabledMark = Status; int res = NewObject<UserDao>().UpdateState(Entity);
string msg = res > ? "啟用成功" : "啟用失敗";
if (Status == )
{
msg = res > ? "禁用成功" : "禁用失敗";
}
result.SetResultDescription(msg);
result.SetSuccessOrFail(res > );
}
catch (Exception ex)
{
LogHelper.WriteError(ex, "UserInfo表 修改用户状态異常");
result.SetResultDescription("操作失敗");
}
return result;
}
#endregion }
}
05-21 11:57