如何缩短这块C#对象的面向代码?

时间:2012-04-14 02:30:38

标签: c# oop optimization

这是我的createcustomer函数,它将客户对象返回给调用者, getCustomerDetail返回一个数据表,然后使用这些值填充客户对象属性。问题是当对象发生变化时,我必须再次修改它,如何解决这个问题,以便我只需要更改Customer对象,它就可以节省我修改整个代码的工作?

  public Objects.Customer createCustomer()
    {
        DataTable dt = Database.Master.Customer.getCustomerDetail(objCustomer.Custcode);

        objCustomer.Billaddress1 = dt.Rows[0]["Billaddress1"].ToString();
        objCustomer.Billaddress2 = dt.Rows[0]["Billaddress2"].ToString();
        objCustomer.Billaddress3 = dt.Rows[0]["Billaddress3"].ToString();
        objCustomer.Billcontact = dt.Rows[0]["Billcontact"].ToString();
        objCustomer.Billfaxno = dt.Rows[0]["Billfaxno"].ToString();
        objCustomer.Billpostalcode = dt.Rows[0]["Billpostalcode"].ToString();
        objCustomer.Billremarks = dt.Rows[0]["Billremarks"].ToString();
        objCustomer.Billtelno = dt.Rows[0]["Billtelno"].ToString();
        objCustomer.Custcode = dt.Rows[0]["Custcode"].ToString();
        objCustomer.Custname = dt.Rows[0]["Custname"].ToString();
        objCustomer.Doout = dt.Rows[0]["Doout"].ToString();
        objCustomer.Douom = dt.Rows[0]["Douom"].ToString();
        objCustomer.Inuom = dt.Rows[0]["Inuom"].ToString();
        objCustomer.Location = dt.Rows[0]["Location"].ToString();
        objCustomer.Outremarks1 = dt.Rows[0]["Outremarks1"].ToString();
        objCustomer.Outremarks2 = dt.Rows[0]["Outremarks2"].ToString();
        objCustomer.Outremarks3 = dt.Rows[0]["Outremarks3"].ToString();
        objCustomer.Pacout = dt.Rows[0]["Pacout"].ToString();
        objCustomer.Pacuom = dt.Rows[0]["Pacuom"].ToString();
        objCustomer.Perout = dt.Rows[0]["Perout"].ToString();
        objCustomer.Peruom = dt.Rows[0]["Peruom"].ToString();
        objCustomer.Shipaddress1 = dt.Rows[0]["Shipaddress1"].ToString();
        objCustomer.Shipaddress2 = dt.Rows[0]["Shipaddress2"].ToString();
        objCustomer.Shipaddress3 = dt.Rows[0]["Shipaddress3"].ToString();
        objCustomer.Shipcontact = dt.Rows[0]["Shipcontact"].ToString();
        objCustomer.Shipfaxno = dt.Rows[0]["Shipfaxno"].ToString();
        objCustomer.Shippostalcode = dt.Rows[0]["Shippostalcode"].ToString();
        objCustomer.Shipremaks = dt.Rows[0]["Shipremaks"].ToString();
        objCustomer.Shiptelno = dt.Rows[0]["BilladdresShiptelnos1"].ToString();
        objCustomer.Shortname = dt.Rows[0]["Shortname"].ToString();


        return objCustomer;
    }

2 个答案:

答案 0 :(得分:1)

一些想法:

答案 1 :(得分:0)

您可以使用Auotamapper

AutoMapper.Mapper.CreateMap<IDataReader, Objects.Customer>();
var results = AutoMapper.Mapper.Map<IDataReader, IList<Objects.Customer>>(dt.CreateDataReader());