知识管理系列---3.程序实现
生活随笔
收集整理的這篇文章主要介紹了
知识管理系列---3.程序实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
系列引導:
知識管理系列----1.原型設計
知識管理系列----2.數據庫設計
? 本項目的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;}} View Code? ? ? ? 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 Modelprivate 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} } View Code? ? ? ? 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 : classwhere 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的GUIDDBNode_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的GUIDDBNode_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 更新 updatepublic 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 刪除 deletepublic 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} } View Code?
? ? 程序源代碼:程序源代碼
?
轉載于:https://www.cnblogs.com/xiaowangzi1987/p/8663563.html
總結
以上是生活随笔為你收集整理的知识管理系列---3.程序实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Groovy 设计模式 -- 抽象工厂
- 下一篇: C#中Equals和==的比较