程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 個人開發框架總結(三)

個人開發框架總結(三)

編輯:關於C#

BaseEditForm 提供數據添加與修改的窗體

屬性

Info:修改時,從BaseListForm傳來的信息對象

TypeID:類別Id

ParentID:信息的父對象

保護的方法

ShowErrorMessage:顯示信息提示

重載方法

CreateListInstance:創建IList對象

AddInfo:數據添加時處理

ModifyInfo:數據修改時處理

ValidateData:驗證數據

FillForm:將數據填充到窗體控件上

ClearForm:清理窗體控件的值

RegisterActiveControl:注冊要激活的控件,Control數組是TabControl每一個TabPage中默認獲得焦點的控件

以下給出一個示例代碼:

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                ATProductType da = (ATProductType)DAFactory.CreateDA(typeof(ATProductType));
                //填充分類列表
                tcbType.TableName = TProductType._TableName;
                tcbType.TextField = TProductType._Name;
                tcbType.IdField = TProductType._Id;
                tcbType.ParentIdField = TProductType._ParentId;
                tcbType.ChildCountField = TProductType._ChildCount;
                tcbType.FillTypes(da.DataHelper, null);
                tcbType.Value = TypeID;

                //單位
                ATUnit uda = (ATUnit)DAFactory.CreateDA(typeof(ATUnit));
                TUnits units = uda.Select(TUnit._CompanyId + " = " + ContextArgs.Instance.CompanyId);
                units.Insert(0, new TUnit());
                cboUnit.DisplayMember = "Name";
                cboUnit.DataSource = units;
                cboUnit.Text = UnitName;
                da.Dispose();
            }
            catch (System.Exception e1)
            {
                Utility.ShowErrorMessage(e1.Message);
            }
        }

        /// <summary>
        /// 創建一個實體集合
        /// </summary>
        /// <param name="type"></param>
        protected override void CreateListInstance(Type type)
        {
            base.CreateListInstance(typeof(TProducts));
        }

        /// <summary>
        /// 將實體信息填充到窗體控件
        /// </summary>
        /// <param name="source"></param>
        protected override void FillForm(BaseModel source)
        {
            TProduct info = source as TProduct;
            txtCode.Text = info.Code;
            txtName.Text = info.Name;
            ctbPrice.Decimal = info.Price;
            txtSpec.Text = info.Spec;
            UnitName = info.Unit;
            TypeID = info.ProductTypeId;
        }

        /// <summary>
        /// 清理窗體控件的值
        /// </summary>
        protected override void ClearForm()
        {
            txtCode.Text = "";
            txtName.Text = "";
            txtSpec.Text = "";
            cboUnit.Text = "";
            txtCode.Focus();
        }

        /// <summary>
        /// 添加新的實體信息
        /// </summary>
        /// <returns></returns>
        protected override BaseModel AddInfo()
        {
            try
            {
                ATProduct da = (ATProduct)DAFactory.CreateDA(typeof(ATProduct));

                #region 檢查以前刪除的
                QueryBuilder qb = new QueryBuilder();
                qb.Append(QueryRelation.And, QueryCompare.Equal, TProduct._IsDelete, true);
                qb.Append(QueryRelation.And, QueryCompare.Equal, TProduct._CompanyId, ContextArgs.Instance.CompanyId);
                qb.Append(QueryRelation.And, QueryCompare.Equal, TProduct._Code, txtCode.Text);

                TProduct oldInfo = da.Get(qb, null);
                if (oldInfo != null)
                {
                    DialogResult dresult = Utility.ShowQuestionMessageEx("歷史記錄中存在相同編碼的產品\"" + oldInfo.Name + "\",\n點擊\"是\"恢復原來的資料,點擊\"否\"刪除原來的資料並創建新的。");
                    if (dresult == DialogResult.Yes)
                    {
                        //恢復
                        oldInfo.IsDelete = false;
                        da.DataHelper.ExecuteNonQuery("UPDATE TProduct SET IsDelete = 0 WHERE " + qb.ToString());
                        return oldInfo;
                    }
                    else if (dresult == DialogResult.No)
                    {
                        //刪除
                        da.DataHelper.ExecuteNonQuery("DELETE FROM TProduct WHERE " + qb.ToString());
                    }
                    else
                    {
                        return null;
                    }
                }
                #endregion

                TProduct info = da.NewEntity;
                SetInfo(info);
                if (da.Create(info))
                {
                    CreateUnit();
                    return info;
                }
            }
            catch (System.Exception e)
            {
                Utility.ShowErrorMessage(e.Message);
            }
            return null;
        }

        /// <summary>
        /// 修改實體信息
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        protected override bool ModifyInfo(BaseModel source)
        {
            try
            {
                TProduct info = source as TProduct;
                SetInfo(info);
                ATProduct da = (ATProduct)DAFactory.CreateDA(typeof(ATProduct));
                if (da.Update(info))
                {
                    CreateUnit();
                    return true;
                }
            }
            catch (System.Exception e)
            {
                Utility.ShowErrorMessage(e.Message);
            }
            return false;
        }

        /// <summary>
        /// 驗證數據
        /// </summary>
        /// <returns></returns>
        protected override bool ValidateData()
        {
            bool ret = true;
            if (txtCode.Text.Length == 0)
            {
                ShowErrorMessage(txtCode, "請輸入編碼");
                ret = false;
            }
            if (txtName.Text.Length == 0)
            {
                ShowErrorMessage(txtName, "請輸入名稱");
                ret = false;
            }
            if (tcbType.Value == null)
            {
                ShowErrorMessage(tcbType, "請選擇類別");
                ret = false;
            }
            try
            {
                //檢查編碼
                ATProduct da = (ATProduct)DAFactory.CreateDA(typeof(ATProduct));
                TProduct refer = Info == null ? null : (TProduct)Info;
                if (da.IsExist(refer, TProduct._Code, txtCode.Text,
                    TProduct._CompanyId + " = " + ContextArgs.Instance.CompanyId + " AND " +
                    TProduct._IsDelete + " = 0"))
                {
                    ShowErrorMessage(txtCode, "編碼重復");
                    ret = false;
                }
            }
            catch (System.Exception e)
            {
                Utility.ShowErrorMessage(e.Message);
            }
            return ret;
        }

        /// <summary>
        /// 將窗體控件的值賦給實體
        /// </summary>
        /// <param name="info"></param>
        private void SetInfo(TProduct info)
        {
            info.Code = txtCode.Text;
            info.Name = txtName.Text;
            info.Price = ctbPrice.Decimal;
            info.ProductTypeId = int.Parse(tcbType.Value.ToString());
            info.Unit = cboUnit.Text;
            info.Spec = txtSpec.Text;
            info.CompanyId = ContextArgs.Instance.CompanyId;
        }

        //添加單位
        private void CreateUnit()
        {
            string name = cboUnit.Text;
            ATUnit uda = (ATUnit)DAFactory.CreateDA(typeof(ATUnit));
            if (!uda.IsExist(TUnit._Name, name, TUnit._CompanyId + " = " + ContextArgs.Instance.CompanyId))
            {
                TUnit unit = new TUnit();
                unit.CompanyId = ContextArgs.Instance.CompanyId;
                unit.Name = name;
                uda.Create(unit);
            }
}

下一節介紹 BaseQueryForm 及相關的一些配置

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved