Should the MVP mode be modified like this?

Source: Internet
Author: User

The skill examples are inspired by the MVP mode, which allows you to edit customer data.

First, define the customer data interface.

Using system;

Namespace henxiao. winui
{
/* Define the customer data interface */
Public interface icustomer
{
/// <Summary>
/// Customer ID
/// </Summary>
Int cstid {Get; set ;}
/// <Summary>
/// Customer ID
/// </Summary>
String cstno {Get; set ;}
/// <Summary>
/// CUSTOMER NAME
/// </Summary>
String cstname {Get; set ;}
/// <Summary>
/// Gender
/// </Summary>
String cstsex {Get; set ;}
/// <Summary>
/// Date of birth
/// </Summary>
Datetime cstbirthday {Get; set ;}
/// <Summary>
/// Document Type
/// </Summary>
String cstcerttype {Get; set ;}
/// <Summary>
/// ID number
/// </Summary>
String cstcertno {Get; set ;}
/// <Summary>
/// Contact number
/// </Summary>
String csttele {Get; set ;}
/// <Summary>
/// Zip code
/// </Summary>
String cstzip {Get; set ;}
/// <Summary>
/// Email
/// </Summary>
String cstemail {Get; set ;}
/// <Summary>
/// Contact address
/// </Summary>
String cstaddr {Get; set ;}
/// <Summary>
/// Work unit
/// </Summary>
String cstcompany {Get; set ;}
/// <Summary>
/// Remarks
/// </Summary>
String cstexp {Get; set ;}
}
}

Then define the customer data business operation object

Using system;
Using system. Data;

namespace henxiao. winui
{< br> ///


/// business operation object of customer data
///
public class hxcustomerhelper
{< br> private icustomer customer = NULL;
private hxdatabase hxdb = NULL;
private string errormessage = string. empty;
//
// error message
//
Public String errormessage
{< br> get {return this. errormessage; }< BR >}

Public hxcustomerhelper (icustomer Cst)
{
This. Customer = CST;
}
/// <Summary>
/// Load data
/// </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>
/// Data verification
/// </Summary>
/// <Returns> </returns>
Private bool validate ()
{
If (string. isnullorempty (this. Customer. cstno ))
{
This. errormessage = "the customer ID cannot be blank! ";
Return false;
}
If (string. isnullorempty (this. Customer. cstname ))
{
This. errormessage = "the customer name cannot be blank! ";
Return false;
}
Return true;
}
/// <Summary>
/// Save data
/// </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
& Amp; Convert. toint32 (cstid. tostring ()> 0)
{
This. errormessage = "Duplicate customer ID! ";
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 ();
}
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.