カスタムコレクションクラスからオブジェクトを返すときの最適なパターンは何ですか?C#でのカスタムコレクションに関する質問
public class Customercollection: Collection<Customer>
{
public Collection<Customer> FindCustomers()
{
//calls DAL and gets a Collection of customers
Collection<Customer> customers = DAL.GetCustomers();
return customers;
}
}
さて、この方法の代替バージョン:
public class Customer
{
//properties
//methods
}
は、それから私は、顧客のコレクションクラスを持っている:私は、Customerクラスを持っている
:私の問題を説明するために、ここでは一例であり、次のようになります。
public class Customercollection: Collection<Customer>
{
public Collection<Customer> FindCustomers()
{
//calls DAL and gets a Collection of customers
Collection<Customer> customers = DAL.GetCustomers();
foreach(Customer c in customers)
this.Add(c);
return this;
}
}
どちらの方が良いアプローチですか?そして、上記の2つの方法よりも優れた方法がありますか?
私のDALはCollectionのようなDTOのコレクションを返しています。私のBLメソッドでは、私はcoll実際のビジネスオブジェクトの操作は、 コレクション dtoList = DAL.GetCustomers();のような からです。 コレクション得意先=新しいコレクション(); foreach(dtoList内のCustomerDTO dto) { カスタマー顧客=新規顧客(dto); //コンストラクタをコピーして、オブジェクトのプロパティを埋め込みます。 customers.Add(customer); } 返品カスタマー: –
Raghav
@Raghav - 最新の回答をご覧ください。 –
返信いただきありがとうございます!このリンクで一つの小さな疑問、: は「私たちは、コレクションを使用することをお勧め: http://blogs.msdn.com/kcwalina/archive/2005/09/26/474010.aspx Cwalinaは、という彼のコメントで言及します、ReadOnlyCollection 、またはKeyedCollection 出力とプロパティとインターフェイスの場合 IEnumerable 、ICollection 、IList の入力の場合 " IList <>ではなくCollection <>を返さなければならないのでしょうか?これについてあなたの意見を知りたいだけです。 –
Raghav