MVP 模式是否应该这样修改?

本事例灵感来源MVP 模式,使用客户资料编辑操作。

首先定义客户资料数据接口。

using System;

namespace HenXiao.WinUI
{
    /*定义客户资料接口*/
    public interface ICustomer
    {
        /// <summary>
        /// 客户ID
        /// </summary>
        int CstId{get;set;}
        /// <summary>
        /// 客户编号
        /// </summary>
        string CstNo{get;set;}
        /// <summary>
        /// 客户名称
        /// </summary>
        string CstName{get;set;}
        /// <summary>
        /// 性别
        /// </summary>
        string CstSex { get;set;}
        /// <summary>
        /// 出生日期
        /// </summary>
        DateTime CstBirthday { get;set;}
        /// <summary>
        /// 证件类型
        /// </summary>
        string CstCertType { get;set;}
        /// <summary>
        /// 证件号码
        /// </summary>
        string CstCertNo { get;set;}
        /// <summary>
        /// 联系电话
        /// </summary>
        string CstTele{get;set;}
        /// <summary>
        /// 邮政编码
        /// </summary>
        string CstZip{get;set;}
        /// <summary>
        /// 电子邮箱
        /// </summary>
        string CstEmail { get;set;}
        /// <summary>
        /// 联系地址
        /// </summary>
        string CstAddr{get;set;}
        /// <summary>
        /// 工作单位
        /// </summary>
        string CstCompany { get;set;}
        /// <summary>
        ///备注
        /// </summary>
        string CstExp{get;set;}
    }
}

然后定义客户资料业务操作对象

using System;
using System.Data;

namespace HenXiao.WinUI
{
    /// <summary>
    /// 客户资料业务操作对象
    /// </summary>
    public class HxCustomerHelper
    {
        private ICustomer customer = null;
        private HxDatabase hxDB = null;
        private string errorMessage = string.Empty;
        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrorMessage
        {
            get { return this.errorMessage; }
        }

        public HxCustomerHelper(ICustomer cst)
        {
            this.customer = cst;
        }
        /// <summary>
        /// 加载数据
        /// </summary>
        public void DataLoad()
        {
            hxDB = new HxDatabase();
            try
            {
                hxDB.AddParam("@CstId",this.customer.CstId);
                DataTable dtData = hxDB.ExecuteDataTable("Select * from Tab_Customer where CstId=@CstId");
                if (dtData.Rows.Count > 0)
                {
                    this.customer.CstNo = dtData.Rows[0]["CstNo"].ToString();
                    this.customer.CstName = dtData.Rows[0]["CstName"].ToString();
                    this.customer.CstSex = dtData.Rows[0]["CstSex"].ToString();
                    if (dtData.Rows[0]["CstBirthday"] != DBNull.Value)
                        this.customer.CstBirthday = Convert.ToDateTime(dtData.Rows[0]["CstBirthday"].ToString());
                    this.customer.CstCertType = dtData.Rows[0]["CstCertType"].ToString();
                    this.customer.CstCertNo = dtData.Rows[0]["CstCertNo"].ToString();
                    this.customer.CstTele = dtData.Rows[0]["CstTele"].ToString();
                    this.customer.CstZip = dtData.Rows[0]["CstZip"].ToString();
                    this.customer.CstAddr = dtData.Rows[0]["CstAddr"].ToString();
                    this.customer.CstCompany = dtData.Rows[0]["CstCompany"].ToString();
                    this.customer.CstExp = dtData.Rows[0]["CstExp"].ToString();
                }
            }
            catch (Exception ex)
            {
                this.errorMessage = ex.Message;
            }
            finally
            {
                hxDB.Dispose();
            }
        }
        /// <summary>
        /// 数据验证
        /// </summary>
        /// <returns></returns>
        private bool Validate()
        {
            if (string.IsNullOrEmpty(this.customer.CstNo))
            {
                this.errorMessage = "客户编号不能为空!";
                return false;
            }
            if (string.IsNullOrEmpty(this.customer.CstName))
            {
                this.errorMessage = "客户名称不能为空!";
                return false;
            }
            return true;
        }
        /// <summary>
        /// 数据保存
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            if (this.Validate() == false)
                return false;
            else
            {

                hxDB = new HxDatabase();
                try
                {
                    hxDB.AddParam("@CstId", this.customer.CstId);
                    hxDB.AddParam("@CstNo", this.customer.CstNo);
                    object cstId = hxDB.ExecuteScalar("Select CstId from Tab_Customer where CstId<>@CstId and CstNo=@CstNo");
                    if(cstId != null
                        && string.IsNullOrEmpty(cstId.ToString()) == false
                        && Convert.ToInt32(cstId.ToString()) > 0)
                    {
                        this.errorMessage = "客户编号重复!";
                        return false;
                    }
                    hxDB.AddParam("@CstId", this.customer.CstId);
                    DataTable dtData = hxDB.ExecuteDataTable("Select * from Tab_Customer where CstId=@CstId");
                    DataRow dr = null;
                    if (dtData.Rows.Count > 0)
                    {
                        dr = dtData.Rows[0];
                    }
                    else
                    {
                        dr = dtData.NewRow();
                        dr["CstCreatedOn"] = DateTime.Now;
                        dtData.Rows.Add(cstId.ToString());
                    }

                    dtData.Rows[0]["CstNo"] = this.customer.CstNo;
                    dtData.Rows[0]["CstName"] = this.customer.CstName;
                    dtData.Rows[0]["CstSex"] = this.customer.CstSex;
                    if (this.customer.CstBirthday != DateTime.MinValue)
                        dtData.Rows[0]["CstBirthday"] = this.customer.CstBirthday;

                    dtData.Rows[0]["CstCertType"] = this.customer.CstCertType;
                    dtData.Rows[0]["CstCertNo"] = this.customer.CstCertNo;
                    dtData.Rows[0]["CstTele"] = this.customer.CstTele;
                    dtData.Rows[0]["CstZip"] =this.customer.CstZip;
                    dtData.Rows[0]["CstAddr"] =this.customer.CstAddr;
                    dtData.Rows[0]["CstCompany"] = this.customer.CstCompany;
                    dtData.Rows[0]["CstExp"]  = this.customer.CstExp;

                    hxDB.Update(dtData,"tab_Customer");
                    return true;
                }
                catch (Exception ex)
                {
                    this.errorMessage = ex.Message;
                    return false;
                }
                finally
                {
                    hxDB.Dispose();
                }
            }
        }
    }
}

原文地址:https://www.cnblogs.com/henxiao25/p/1736964.html