cWeb開發框架,基於asp.net的cWeb應用開發平臺介紹(二)

cWeb是基於微軟的.Net Framework 4框架,數據庫是sql server 2008 r2。html

cWeb開發框架下載,點擊這裏去下載web

cWeb開發框架借鑑三層架構理論分爲三層,分別是:cData、cBN和cWeb。cData是數據層,cBN是業務處理層,cWeb是業務展現層。解決方案如圖示:sql

 

  

1、cData數據層數據庫

cData數據層,數據庫字段的映射類。在其餘開發框架中常見於datatable和dataview,也就是將數據庫表映射成datatable,dataview繼承datatable,並將其餘如視圖等的字段映射在dataview中。在相似於petshop等開發框架中分層可謂詳細,雖然如今硬件的發展能夠不用考慮繼承、映射、調用等等的運行時間,可是對於大數據量下即便很小的優化都會起到極大的做用,因此直接的就是高效率的,簡化後數據層都融合在了一塊兒。架構

data層數據示例:框架

using System;

namespace AA.cData
{
    /// <summary>
    /// t_info 的摘要說明。
    /// </summary>
    [Serializable]
    public class t_infoDV
    {
        /// <summary>
        /// t_infoDV
        /// </summary>
        public t_infoDV()
        {
        }

        //============================數據庫字段=================================//

        private long _ID = -1;
        /// <summary>
        /// ID
        /// </summary>
        public long ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }

        private string _t_info_title = null;
        /// <summary>
        /// t_info_title
        /// </summary>
        public string t_info_title
        {
            get
            {
                return _t_info_title;
            }
            set
            {
                _t_info_title = value;
            }
        }

        private DateTime _t_info_createtime = System.DateTime.MinValue;
        /// <summary>
        /// t_info_createtime
        /// </summary>
        public DateTime t_info_createtime
        {
            get
            {
                return _t_info_createtime;
            }
            set
            {
                _t_info_createtime = value;
            }
        }




        //============================擴展字段==================================//

        private string _t_info_title_like = null;
        /// <summary>
        /// t_info_title [模糊查詢]
        /// </summary>
        public string t_info_title_like
        {
            get
            {
                return _t_info_title_like;
            }
            set
            {
                _t_info_title_like = value;
            }
        }



    }
}

  

2、cBN業務處理層asp.net

cBN業務處理層,也能夠叫業務邏輯層。是數據層之上用來處理業務邏輯的方法,將除了web以外的全部業務處理均可以放在這個層裏,好比添加、修改、刪除、各類查詢等方法,在web層僅僅調用一下就能夠實現功能,也體現了業務邏輯通用原則,這樣一樣的業務處理就能夠重複調用,提升了開發效率,減小了代碼的冗長重複。函數

 BN層示例:大數據

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using cDB;
using AA.cData;

namespace AA.cBN
{
    /// <summary>
    /// t_info 的摘要說明。
    /// </summary>
    public class t_infoBN
    {
        DataInfo cDI;

        /// <summary>
        /// t_infoBN
        /// </summary>
        public t_infoBN()
        {
            cDI = new DataInfo();
        }


        #region [添加|修改|刪除]

        /// <summary>
        /// [添加]
        /// </summary>
        /// <param name="tDV"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public int Add(t_infoDV tDV, out string msg)
        {
            try
            {
                mCommand oCommand = new mCommand();

                oCommand.SqlTxt = "Insert into t_info(t_info_title,t_info_createtime)" +
                                    " values(@t_info_title,@t_info_createtime)";


                SqlParameter[] SqlItems = new SqlParameter[2];

                SqlItems[0] = new SqlParameter("@t_info_title", SqlDbType.NVarChar);
                SqlItems[0].Value = tDV.t_info_title;

                SqlItems[1] = new SqlParameter("@t_info_createtime", SqlDbType.DateTime);
                SqlItems[1].Value = tDV.t_info_createtime;




                oCommand.AddParameter(SqlItems);

                int iResult = cDI.iCommandSql(oCommand);

                if (iResult > -1)
                {
                    msg = "添加成功!";
                }
                else
                {
                    msg = "添加失敗!";
                }

                return iResult;

            }
            catch //(Exception ex)
            {
                //msg = ex.Message;
                msg = "添加失敗(異常)!";
                return -11;     //-11表示異常
            }
        }

        /// <summary>
        /// [修改]
        /// </summary>
        /// <param name="tDV"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public int Edit(t_infoDV tDV, out string msg)
        {

            try
            {
                mCommand oCommand = new mCommand();

                oCommand.SqlTxt = "update t_info set " +
                                    "t_info_title=@t_info_title," +
                                    "t_info_createtime=@t_info_createtime " +
                                    "where ID=@ID";


                SqlParameter[] SqlItems = new SqlParameter[3];

                SqlItems[0] = new SqlParameter("@t_info_title", SqlDbType.NVarChar);
                SqlItems[0].Value = tDV.t_info_title;

                SqlItems[1] = new SqlParameter("@t_info_createtime", SqlDbType.DateTime);
                SqlItems[1].Value = tDV.t_info_createtime;

                SqlItems[2] = new SqlParameter("@ID", SqlDbType.BigInt);
                SqlItems[2].Value = tDV.ID;


                oCommand.AddParameter(SqlItems);

                int iResult = cDI.iCommandSql(oCommand);

                if (iResult > -1)
                {
                    msg = "修改爲功!";
                }
                else
                {
                    msg = "修改失敗!";
                }

                return iResult;

            }
            catch //(Exception ex)
            {
                //msg = ex.Message;
                msg = "添加失敗(異常)!";
                return -11;     //-11表示異常
            }
        }

        /// <summary>
        /// [刪除]
        /// </summary>
        /// <param name="tDV"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public int Del(t_infoDV tDV, out string msg)
        {
            try
            {
                mCommand oCommand = new mCommand();
                oCommand.SqlTxt = "delete t_info where ID=@ID";
                SqlParameter[] SqlItems = new SqlParameter[1];

                SqlItems[0] = new SqlParameter("@ID", SqlDbType.BigInt);
                SqlItems[0].Value = tDV.ID;

                oCommand.AddParameter(SqlItems);

                int iResult = cDI.iCommandSql(oCommand);

                if (iResult > -1)
                {
                    msg = "刪除成功!";
                }
                else
                {
                    msg = "刪除失敗!";
                }

                return iResult;
            }
            catch //(Exception ex)
            {
                //msg = ex.Message;
                msg = "刪除失敗(異常)!";
                return -11;     //-11表示異常
            }
        }
        #endregion


        #region [查詢]
        /// <summary>
        /// [返回數據分頁]
        /// </summary>
        /// <param name="tDV"></param>
        /// <param name="sqlWhere">where語句</param>
        /// <returns></returns>
        public DataTable GetDataList(t_infoDV tDV, string sqlWhere)
        {

            DataTable DTe = new DataTable();

            mCommand oCommand = new mCommand();

            StringBuilder SqlTxtPrefiex = new StringBuilder(" select ISNULL(count(ID),0) as Counts from View_t_info where 1 = 1 " + sqlWhere + " ");
            SqlParameter tSqlItem = null;


            if (tDV.t_info_title != null)
            {
                SqlTxtPrefiex.Append(" and t_info_title = @t_info_title");
                tSqlItem = new SqlParameter("@t_info_title", SqlDbType.NVarChar);
                tSqlItem.Value = tDV.t_info_title;
                oCommand.SqlParameters.Add(tSqlItem);
            }

            if (tDV.t_info_title_like != null)
            {
                SqlTxtPrefiex.Append(" and t_info_title like @t_info_title_like");
                tSqlItem = new SqlParameter("@t_info_title_like", SqlDbType.NVarChar);
                tSqlItem.Value = "%" + tDV.t_info_title_like + "%";
                oCommand.SqlParameters.Add(tSqlItem);
            }



            oCommand.SqlTxt = SqlTxtPrefiex.ToString();

            DTe = cDI.GetTable(oCommand);
            return DTe;
        }

        /// <summary>
        /// [返回數據]
        /// </summary>
        /// <param name="tDV"></param>
        /// <param name="column">顯示的列,默認爲【*】所有</param>
        /// <param name="sqlWhere">where語句</param>
        /// <param name="beginIndex">開始記錄</param>
        /// <param name="endIndex">結束記錄</param>
        /// <param name="Order">排序,如【order by ID desc】</param>
        /// <returns></returns>
        public DataTable GetDataList(t_infoDV tDV, string column, string sqlWhere, long beginIndex, long endIndex, string Order)
        {
            if (string.IsNullOrEmpty(column))
                column = " * ";

            mCommand oCommand = new mCommand();

            StringBuilder cSqlTxtPrefiex = new StringBuilder("select * from NoPagedTable WHERE rowIndex>=@beginIndex and rowIndex<=@endIndex");

            SqlParameter[] SqlItems = new SqlParameter[2];

            SqlItems[0] = new SqlParameter("@beginIndex", SqlDbType.BigInt);
            SqlItems[0].Value = beginIndex;

            SqlItems[1] = new SqlParameter("@endIndex", SqlDbType.BigInt);
            SqlItems[1].Value = endIndex;

            oCommand.SqlParameters.AddRange(SqlItems);


            if (Order == string.Empty)
            {
                Order = " order by ID DESC ";
            }

            StringBuilder SqlTxtPrefiex = new StringBuilder(
                "WITH NoPagedTable AS " +
                "(" +
                "SELECT ROW_NUMBER() OVER (" + Order + ") AS rowIndex," + column + " FROM View_t_info where 1 = 1 " + sqlWhere + " ");

            SqlParameter tSqlItem = null;

            if (tDV.t_info_title != null)
            {
                SqlTxtPrefiex.Append(" and t_info_title = @t_info_title");
                tSqlItem = new SqlParameter("@t_info_title", SqlDbType.NVarChar);
                tSqlItem.Value = tDV.t_info_title;
                oCommand.SqlParameters.Add(tSqlItem);
            }

            if (tDV.t_info_title_like != null)
            {
                SqlTxtPrefiex.Append(" and t_info_title like @t_info_title_like");
                tSqlItem = new SqlParameter("@t_info_title_like", SqlDbType.NVarChar);
                tSqlItem.Value = "%" + tDV.t_info_title_like + "%";
                oCommand.SqlParameters.Add(tSqlItem);
            }



            SqlTxtPrefiex.Append(")");

            oCommand.SqlTxt = SqlTxtPrefiex.ToString() + cSqlTxtPrefiex.ToString();

            return cDI.GetTable(oCommand);
        }

        /// <summary>
        /// [返回DataTable]
        /// </summary>
        /// <param name="tDV"></param>
        /// <param name="topNum">返回行數,默認【0】所有</param>
        /// <param name="column">顯示的列,默認爲【*】所有</param>
        /// <param name="sqlWhere">where語句</param>
        /// <param name="order">排序,如【order by ID desc】</param>
        /// <returns></returns>
        public DataTable GetDataTable(t_infoDV tDV, int topNum, string column, string sqlWhere, string order)
        {
            if (string.IsNullOrEmpty(column))
                column = " * ";

            string topNumSql = "";
            if (topNum > 0)
                topNumSql = " top " + topNum.ToString();

            DataTable DT = new DataTable();

            mCommand oCommand = new mCommand();

            StringBuilder SqlTxtPrefiex = new StringBuilder(" select " + topNumSql + " " + column + " from View_t_info where 1 = 1 " + sqlWhere + " ");
            SqlParameter tSqlItem = null;

            if (tDV.ID != -1)
            {
                SqlTxtPrefiex.Append(" and ID=@ID");
                tSqlItem = new SqlParameter("@ID", SqlDbType.BigInt);
                tSqlItem.Value = tDV.ID;
                oCommand.SqlParameters.Add(tSqlItem);
            }


            if (tDV.t_info_title != null)
            {
                SqlTxtPrefiex.Append(" and t_info_title = @t_info_title");
                tSqlItem = new SqlParameter("@t_info_title", SqlDbType.NVarChar);
                tSqlItem.Value = tDV.t_info_title;
                oCommand.SqlParameters.Add(tSqlItem);
            }

            if (tDV.t_info_title_like != null)
            {
                SqlTxtPrefiex.Append(" and t_info_title like @t_info_title_like");
                tSqlItem = new SqlParameter("@t_info_title_like", SqlDbType.NVarChar);
                tSqlItem.Value = "%" + tDV.t_info_title_like + "%";
                oCommand.SqlParameters.Add(tSqlItem);
            }



            if (string.IsNullOrEmpty(order))
                order = " order by ID desc ";

            SqlTxtPrefiex.Append(" " + order + " ");


            oCommand.SqlTxt = SqlTxtPrefiex.ToString();

            DT = cDI.GetTable(oCommand);
            return DT;
        }


        #endregion


    }
}

  

3、cWeb業務展現層優化

cWeb業務展現層,用於業務邏輯展現的層,調用BN層的函數直接顯示在web頁面中。

web層示例:

infolist.aspx.cs代碼以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

using AA.cData;
using AA.cBN;
using cPage;

namespace AA.cWeb.manage.info
{
    public partial class infolist : System.Web.UI.Page
    {
        cUser cu = new cUser();

        public int p = 1;   //頁碼


        protected void Page_Load(object sender, EventArgs e)
        {
            #region [判斷是否登陸]
            if (!cu.isLogin())
            {
                cGlobal.frameGoUrl(this, "../../default.aspx");
                return;
            }
            #endregion

            #region 分頁得到頁碼
            string _p = Request.QueryString["p"];

            if (string.IsNullOrEmpty(_p))
                _p = "1";

            if (cGlobal.IsIntType(_p))
            {
                p = Convert.ToInt32(_p);
            }
            else
            {
                cGlobal.showBoxBack(this, "頁碼不正確!");
                return;
            }
            #endregion

            #region [判斷是具備訪問權限]
            if (!cDataCommon.userPagePower(cu.userID, "0202"))
            {
                cGlobal.showBoxBack(this, "您沒有權限訪問!");
                return;
            }
            #endregion

            #region [得到在【系統設置=》角色管理=》權限設置】中點擊某一菜單後,「菜單功能權限設置」設置的功能]
            string userPageFunctionPower = cDataCommon.userPageFunctionPower(cu.userID, "0202");
            //判斷是否具備「添加」功能
            if (userPageFunctionPower.IndexOf("|add|") > -1)
                btnAdd.Visible = true;
            else
                btnAdd.Visible = false;
            #endregion

            
            if (!Page.IsPostBack)
            {

                dataBind();
            }

        }

        protected void dataBind()
        {
            string _url = "infolist.aspx?p=[#p#]";
            int _pageNum = 10;                              // 中間頁碼數量
            int _pageSize = 10;                             //每頁記錄數
            int _beginIndex = 0;
            int _endIndex = 0;

            t_infoBN tBN = new t_infoBN();
            t_infoDV tDV = new t_infoDV();


            DataTable rcDT = tBN.GetDataList(tDV, "");

            lblpage.Text = mPage.getPage(Convert.ToInt32(rcDT.Rows[0][0].ToString()), _pageSize, _pageNum, p, out _beginIndex, out _endIndex, _url);

            DataTable DT = tBN.GetDataList(tDV, "", "", _beginIndex, _endIndex, "");

            rlist.DataSource = DT;
            rlist.DataBind();

        }
        

        protected void rlist_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex > -1)
            {
                ((LinkButton)e.Item.FindControl("lnkbtnDel")).Attributes["onclick"] = "return confirm('確認要刪除嗎?');";
            }
        }

        protected void rlist_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                try
                {
                    long _ID = Convert.ToInt64(((HiddenField)(e.Item.FindControl("hfID"))).Value);

                    t_sys_logBN tBN = new t_sys_logBN();
                    t_sys_logDV tDV = new t_sys_logDV();

                    tDV.ID = _ID;

                    string msg = "";
                    int iResult = -2;

                    iResult = tBN.Del(tDV, out msg);


                    if (iResult >= 0)   //0,執行完成,>1,執行完成影響的記錄數
                    {
                        cGlobal.showBoxGo(this, "刪除成功!", Request.Url.ToString());
                    }
                    else
                    {
                        cGlobal.showBoxBack(this, "刪除失敗!");
                    }

                }
                catch
                {
                    cGlobal.showBoxBack(this, "數據格式不正確(異常)");
                }
            }
        }

    }
}

  

更詳細的介紹和cWeb源代碼,去這裏下載

 

原帖地址:cWeb開發框架,基於asp.net的cWeb應用開發平臺介紹(二)

bubufx提供,禁止轉載。

相關文章
相關標籤/搜索