📄 builderbll.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using LTP.Utility;
using LTP.IDBO;
using LTP.CodeHelper;
namespace LTP.BuilderBLLComm
{
/// <summary>
/// 业务层代码组件
/// </summary>
public class BuilderBLL : IBuilder.IBuilderBLL
{
#region 私有变量
protected string _key = "ID";//默认第一个主键字段
protected string _keyType = "int";//默认第一个主键类型
NameRule namerule = new NameRule();
#endregion
#region 公有属性
private List<ColumnInfo> _fieldlist;
private List<ColumnInfo> _keys;
private string _namespace; //顶级命名空间名
private string _modelspace;
private string _modelname;//model类名
private string _bllname;//bll类名
private string _dalname;//dal类名
private string _modelpath;
private string _bllpath;
private string _factorypath;
private string _idalpath;
private string _iclass;
private string _dalpath;
private string _dalspace;
private bool isHasIdentity;
private string dbType;
/// <summary>
/// 选择的字段集合
/// </summary>
public List<ColumnInfo> Fieldlist
{
set { _fieldlist = value; }
get { return _fieldlist; }
}
/// <summary>
/// 主键或条件字段列表
/// </summary>
public List<ColumnInfo> Keys
{
set { _keys = value; }
get { return _keys; }
}
/// <summary>
/// 顶级命名空间名
/// </summary>
public string NameSpace
{
set { _namespace = value; }
get { return _namespace; }
}
/*============================*/
/// <summary>
/// 实体类的命名空间
/// </summary>
public string Modelpath
{
set { _modelpath = value; }
get { return _modelpath; }
}
/// <summary>
/// Model类名
/// </summary>
public string ModelName
{
set { _modelname = value; }
get { return _modelname; }
}
/// <summary>
/// 实体类的整个命名空间 + 类名,即等于 Modelpath+ModelName
/// </summary>
public string ModelSpace
{
get { return Modelpath + "." +ModelName; }
}
/*============================*/
/// <summary>
/// 业务逻辑层的命名空间
/// </summary>
public string BLLpath
{
set { _bllpath = value; }
get { return _bllpath; }
}
/// <summary>
/// BLL类名
/// </summary>
public string BLLName
{
set { _bllname = value; }
get { return _bllname; }
}
/*============================*/
/// <summary>
/// 数据层的命名空间
/// </summary>
public string DALpath
{
set { _dalpath = value; }
get { return _dalpath; }
}
public string DALName
{
set { _dalname = value; }
get { return _dalname; }
}
/// <summary>
/// 数据层的命名空间+ 类名,即等于 DALpath + DALName
/// </summary>
public string DALSpace
{
get { return DALpath + "." + DALName; }
}
/*============================*/
/// <summary>
/// 工厂类的命名空间
/// </summary>
public string Factorypath
{
set { _factorypath = value; }
get { return _factorypath; }
}
/// <summary>
/// 接口的命名空间
/// </summary>
public string IDALpath
{
set { _idalpath = value; }
get { return _idalpath; }
}
/// <summary>
/// 接口名
/// </summary>
public string IClass
{
set { _iclass = value; }
get { return _iclass; }
}
/*============================*/
/// <summary>
/// 是否有自动增长标识列
/// </summary>
public bool IsHasIdentity
{
set { isHasIdentity = value; }
get { return isHasIdentity; }
}
public string DbType
{
set { dbType = value; }
get { return dbType; }
}
/// <summary>
/// 主键标识字段
/// </summary>
public string Key
{
get
{
foreach (ColumnInfo key in _keys)
{
_key = key.ColumnName;
_keyType = key.TypeName;
if (key.IsIdentity)
{
_key = key.ColumnName;
_keyType = CodeCommon.DbTypeToCS(key.TypeName);
break;
}
}
return _key;
}
}
private string KeysNullTip
{
get
{
if (_keys.Count == 0)
{
return "//该表无主键信息,请自定义主键/条件字段";
}
else
{
return "";
}
}
}
#endregion
#region 构造函数
public BuilderBLL()
{
}
public BuilderBLL(List<ColumnInfo> keys, string modelspace)
{
_modelspace = modelspace;
Keys = keys;
foreach (ColumnInfo key in _keys)
{
_key = key.ColumnName;
_keyType = key.TypeName;
if (key.IsIdentity)
{
_key = key.ColumnName;
_keyType = CodeCommon.DbTypeToCS(key.TypeName);
break;
}
}
}
#endregion
#region 业务层方法
/// <summary>
/// 得到整个类的代码
/// </summary>
public string GetBLLCode(bool Maxid, bool Exists, bool Add, bool Update, bool Delete, bool GetModel,bool GetModelByCache, bool List)
{
StringPlus strclass = new StringPlus();
strclass.AppendLine("using System;");
strclass.AppendLine("using System.Data;");
strclass.AppendLine("using System.Collections.Generic;");
if (GetModelByCache)
{
strclass.AppendLine("using LTP.Common;");
}
strclass.AppendLine("using " + Modelpath + ";");
if ((Factorypath != "")&&(Factorypath !=null))
{
strclass.AppendLine("using " + Factorypath + ";");
}
if ((IDALpath != "")&&(IDALpath !=null))
{
strclass.AppendLine("using " + IDALpath + ";");
}
strclass.AppendLine("namespace " + BLLpath );
strclass.AppendLine("{" );
strclass.AppendSpaceLine(1, "/// <summary>" );
strclass.AppendSpaceLine(1, "/// 业务逻辑类" + BLLName + " 的摘要说明。");
strclass.AppendSpaceLine(1, "/// </summary>" );
strclass.AppendSpaceLine(1, "public class " + BLLName);
strclass.AppendSpaceLine(1, "{" );
if ((IClass != "") && (IClass != null))
{
strclass.AppendSpaceLine(2, "private readonly " + IClass + " dal=" + "DataAccess.Create" + ModelName + "();");
}
else
{
strclass.AppendSpaceLine(2, "private readonly " + DALSpace + " dal=" + "new " + DALSpace + "();");
}
strclass.AppendSpaceLine(2, "public " + BLLName + "()");
strclass.AppendSpaceLine(2, "{}" );
strclass.AppendSpaceLine(2, "#region 成员方法" );
#region 方法代码
if (Maxid)
{
if (Keys.Count > 0)
{
foreach (ColumnInfo obj in Keys)
{
if (CodeCommon.DbTypeToCS(obj.TypeName) == "int")
{
if (obj.IsPK)
{
strclass.AppendLine(CreatBLLGetMaxID() );
break;
}
}
}
}
}
if (Exists)
{
strclass.AppendLine(CreatBLLExists() );
}
if (Add)
{
strclass.AppendLine(CreatBLLADD() );
}
if (Update)
{
strclass.AppendLine(CreatBLLUpdate() );
}
if (Delete)
{
strclass.AppendLine(CreatBLLDelete() );
}
if (GetModel)
{
strclass.AppendLine(CreatBLLGetModel() );
}
if (GetModelByCache)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -