系列引导:
知识管理系列----1.原型设计
本项目的ORM框架采用LINQ的核心。所有的增删改查均基于LINQ语法实现。
按照ORM基本的三层框架:
SQL底层核心:
/// <summary> /// 查询SQL语句,并将查询结果实例化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="func"></param> /// <param name="param"></param> /// <returns></returns> public static IEnumerable<T> SelectReader<T>(string sql, Func<System.Data.Common.DbDataReader, T> func, params System.Data.Common.DbParameter[] param) where T:class { using (SqlConnection con = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(sql, con); cmd.Connection.Open(); cmd.Parameters.Clear(); if (param != null && param.Length > 0) { cmd.Parameters.AddRange(param); } SqlDataReader dr = cmd.ExecuteReader(); var list = new List<T>(); while (dr.Read()) { T t = func(dr); if (t != null) list.Add(t); } dr.Close(); return list; } }
Model层:(以g_Node为例)
using System; using JackCSoft.DAL; namespace JackCSoft.Model { /// <summary> /// g_Node:实体类(属性说明自动提取数据库字段的描述信息) /// </summary> [Serializable] [SelectContext] [ExecuteContext] public partial class g_Node { public g_Node() { } #region Model private int _id = 10000; private string _nodename; private int _nodetype = 0; private int? _create_user; private string _create_ymd; private string _create_hms; private int? _update_user; private string _update_ymd; private string _update_hms; private string _guid = "newid"; /// <summary> /// /// </summary> public int ID { set { _id = value; } get { return _id; } } /// <summary> /// /// </summary> public string NodeName { set { _nodename = value; } get { return _nodename; } } /// <summary> /// /// </summary> public int NodeType { set { _nodetype = value; } get { return _nodetype; } } /// <summary> /// /// </summary> public int? create_user { set { _create_user = value; } get { return _create_user; } } /// <summary> /// /// </summary> public string create_ymd { set { _create_ymd = value ; } get { return _create_ymd; } } /// <summary> /// /// </summary> public string create_hms { set { _create_hms = value; } get { return _create_hms; } } /// <summary> /// /// </summary> public int? update_user { set { _update_user = value; } get { return _update_user; } } /// <summary> /// /// </summary> public string update_ymd { set { _update_ymd = value; } get { return _update_ymd; } } /// <summary> /// /// </summary> public string update_hms { set { _update_hms = value; } get { return _update_hms; } } /// <summary> /// /// </summary> public string guid { set { _guid = value; } get { return _guid; } } #endregion Model } }
BLL层:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Collections; using JackCSoft.Model; using JackCSoft.DAL; using JackCSoft.CommonLib; using System.Linq.Expressions; using System.Data.SqlClient; using JackCSoft.DBUtility; namespace JackCSoft.BLL { public class BLL_NodeManager { CommonLib.TypePropertyOperation typeProperOperation=new TypePropertyOperation(); BLL_g_Node instance_Node = BLL_g_Node.handler; BLL_g_NodeOrder instance_NodeOrder = BLL_g_NodeOrder.handler; #region 查询 select /// <summary> /// 获取顶层node的信息 /// </summary> /// <returns></returns> public IEnumerable<g_Node> GetTopNodeInfo() { var obj = new JackCSoft.Model.g_Node(); var where = new JackCSoft.Model.g_Node() { ID = 0, NodeType = Convert.ToInt16(NodeTypeEnumer.TopNode) //顶层Node类型 }; return instance_Node.Select(obj, s => s, where, b => new { b.ID, b.NodeType }); } /// <summary> /// 获取顶层node的信息 /// </summary> /// <returns></returns> public IEnumerable<TResult> GetTopNodeInfo<TResult>(Func<g_Node,TResult> func) where TResult:class { var obj = new JackCSoft.Model.g_Node(); var where = new JackCSoft.Model.g_Node() { ID = 0, NodeType = Convert.ToInt16(NodeTypeEnumer.TopNode) //顶层Node类型 }; return instance_Node.Select(obj, s => func(s), where, b => new { b.ID, b.NodeType }); } /// <summary> /// 根据指定条件查询Node信息 /// </summary> /// <returns></returns> public IEnumerable<TResult> GetNodeInfoByWhere<TResult,TWhere>(Func<g_Node, TResult> func,g_Node entity,Func<g_Node,TWhere> funcWhere) where TResult : class where TWhere:class { var obj = new JackCSoft.Model.g_Node(); return instance_Node.Select(obj, s => func(s),entity,p=>funcWhere(p)); } /// <summary> /// 根据GUID查询node信息 /// </summary> /// <param name="guid"></param> /// <returns></returns> public IEnumerable<g_Node> GetNodeInfoByGUID(string guid) { var obj = new JackCSoft.Model.g_Node(); var where = new JackCSoft.Model.g_Node() { guid=guid }; return instance_Node.Select(obj, s => s, where, p => new { p.guid}); } /// <summary> /// 查询该节点类型的最大值 /// </summary> /// <param name="enumer"></param> /// <returns></returns> public g_Node GetMaxNodeIDByNodeType(int enumer) { var obj = new JackCSoft.Model.g_Node(); var where = new JackCSoft.Model.g_Node() { NodeType=enumer }; IEnumerable<g_Node> result=instance_Node.Select(obj, s => s, where, p => new { p.NodeType }).OrderByDescending(m=>m.ID); return (result != null && result.Count() > 0) ? result.ToArray()[0] : null; } /// <summary> /// 查询max id /// </summary> /// <param name="enumer"></param> /// <returns></returns> public g_Node GetMaxNodeID() { var obj = new JackCSoft.Model.g_Node(); IEnumerable<g_Node> result = instance_Node.Select(s=>s).OrderByDescending(m => m.ID); return (result != null && result.Count() > 0) ? result.ToArray()[0] : null; } public DataTable GetHtmlContentByID(int id) { return DAL_Node.GetHtmlContentByID(id); } #endregion #region 插入 insert /// <summary> /// 往g_Node插入节点信息,并在g_NodeOrder表插入节点顺序信息 /// </summary> /// <returns></returns> public bool InsertNode<TNode>(g_Node t,Func<g_Node,TNode> func,int parentNodeID) where TNode:class { //插入g_Node子信息 bool _insertOk = instance_Node.Insert(t, s => func(s)); if (_insertOk) { //查询已经插入的子节点信息 在插入g_NodeOrder表 Expression<Func<g_Node,object>> selectEntity=s=>new {s.guid,s.ID}; g_Node obj = new g_Node(); IEnumerable<object> _resultList = instance_Node.Select(obj, s => selectEntity.Compile()(s), t, p => func(p)); if (_resultList != null&&_resultList.ToArray().Count()>0) { //插入g_NodeOrder数据表数据 g_NodeOrder nodeOrder = new g_NodeOrder { //g_NodeOrder的DBNode_GUID对应g_Node的GUID DBNode_GUID = Convert.ToString(typeProperOperation.GetPropertyByNameType("guid",_resultList.ToArray()[0])), //本节点的的ID编号 对应的是g_Node的ID字段 NodeOrderLoacte=Convert.ToInt32(typeProperOperation.GetPropertyByNameType("ID", _resultList.ToArray()[0])), //父级节点表g_Node 的ID字段 ParentNodeID=parentNodeID }; _insertOk = false; Func<g_NodeOrder,object> fun=s=>new {s.DBNode_GUID,s.NodeOrderLoacte,s.ParentNodeID}; _insertOk=instance_NodeOrder.Insert(nodeOrder, s => fun(s)); return _insertOk == true ? true : false; } else { return false; } } else { return false; } } /// <summary> /// 往g_Node插入节点信息,并在g_NodeOrder表插入节点顺序信息 /// </summary> /// <returns></returns> public bool InsertNode<TNode>(g_Node t, Func<g_Node, TNode> func, int parentNodeID,out string guid) where TNode : class { //插入g_Node子信息 bool _insertOk = instance_Node.Insert(t, s => func(s)); if (_insertOk) { //查询已经插入的子节点信息 在插入g_NodeOrder表 Expression<Func<g_Node, object>> selectEntity = s => new { s.guid, s.ID }; g_Node obj = new g_Node(); IEnumerable<object> _resultList = instance_Node.Select(obj, s => selectEntity.Compile()(s), t, p => func(p)); if (_resultList != null && _resultList.ToArray().Count() > 0) { //插入g_NodeOrder数据表数据 g_NodeOrder nodeOrder = new g_NodeOrder { //g_NodeOrder的DBNode_GUID对应g_Node的GUID DBNode_GUID = Convert.ToString(typeProperOperation.GetPropertyByNameType("guid", _resultList.ToArray()[0])), //本节点的的ID编号 对应的是g_Node的ID字段 NodeOrderLoacte = Convert.ToInt32(typeProperOperation.GetPropertyByNameType("ID", _resultList.ToArray()[0])), //父级节点表g_Node 的ID字段 ParentNodeID = parentNodeID }; _insertOk = false; Func<g_NodeOrder, object> fun = s => new { s.DBNode_GUID, s.NodeOrderLoacte, s.ParentNodeID }; _insertOk = instance_NodeOrder.Insert(nodeOrder, s => fun(s)); //返回执行结果 guid = Convert.ToString(typeProperOperation.GetPropertyByNameType("guid", _resultList.ToArray()[0])); return _insertOk == true ? true : false; } else { guid = ""; return false; } } else { guid = ""; return false; } } #endregion #region 更新 update public bool UpdateNode(string nodeName,string guid) { g_Node node = new g_Node { NodeName=nodeName }; g_Node where=new g_Node { guid=guid }; return instance_Node.Update(node, s => new { s.NodeName }, where, p => new { p.guid}); } #endregion #region 删除 delete public void DeleteTreeViewNodeByID(int id) { DataTable idDt = DAL_NodeOrder.SelectIDByParentID(id); if (idDt != null && idDt.Rows.Count > 0) { bool _deleteOK = true; //查询是否存在html等文件信息 DataTable dt = DAL_AddressUrl.GetAddressByParentID(id); if (dt != null && dt.Rows.Count > 0) //存在HTML文件 { //g_AddressUrl删除信息 g_Content删除信息 if (DAL_AddressUrl.DeleteAddressByParentID(id) && DAL_Content.DeleteContentByParentID(id) && DAL_Node.DeleteNodeByID(id)) { _deleteOK = true; } else { _deleteOK = false; } } //删除节点信息 if (_deleteOK) { if (DAL_Node.DeleteNodeByID(id) && DAL_NodeOrder.DeleteNodeOrderByID(id)) { for (int i = 0; i < idDt.Rows.Count; i++) { DeleteTreeViewNodeByID(Convert.ToInt32(idDt.Rows[i]["ID"])); } } } } else { DAL_Node.DeleteNodeAndChildByID(id); if (DAL_NodeOrder.SelectIDByCurrentNodeID(id) != null) { DAL_NodeOrder.DeleteNodeOrderAndChildByID(id); } } //return false; } #endregion } }
程序源代码:程序源代码