2012-04-14 4 views
2

これは、顧客オブジェクトを呼び出し元に返すcreatecustomer関数です。 getCustomerDetailは、顧客オブジェクトのプロパティに値を設定するデータテーブルを返します。問題は、オブジェクトに変更があるたびに変更する必要があります。この問題を解決してCustomerオブジェクトを変更するだけで、コード全体を変更する作業が節約されます。どのようにC#のオブジェクトのこの部分を短縮するオブジェクト指向のコード?

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; 
    } 
+0

強くt yped、代わりに自動 'DataSet's? – Ryan

+0

誤って説明できますか? – Melvin

+0

Entity FrameworkやDapperのようなORMを使用しますか?おそらくこれはおそらく最も効果の高いオプションの1つで、なぜそれを変更するのでしょうか? –

答えて

1

いくつかのアイデア:

  • が自動的
  • 使用AutoMapperために、このコードを生成するEntity FrameworkまたはNHibernate
  • 使用CodeSmith Generatorのようなツールのようなオブジェクトリレーショナルモデル(ORM)ツールを使用し、あなたのオブジェクトを水和するための反射
+0

ありがとう、私はそれを見てみましょう.. – Melvin

0

Auotamapperを使用できます。

AutoMapper.Mapper.CreateMap<IDataReader, Objects.Customer>(); 
var results = AutoMapper.Mapper.Map<IDataReader, IList<Objects.Customer>>(dt.CreateDataReader()); 
関連する問題