2009-08-03 20 views
4

カスタムコレクションクラスからオブジェクトを返すときの最適なパターンは何ですか?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つの方法よりも優れた方法がありますか?

答えて

15

は私が第三のアプローチを提案します:

編集:私は以下のOPのコメントを反映するために、このコード例を更新しました

public class Customer 
{ 
    public static ICollection<Customer> FindCustomers() 
    { 
     Collection<Customer> customers = new Collection<Customer>(); 

     foreach (CustomerDTO dto in DAL.GetCustomers()) 
      customers.Add(new Customer(dto)); // Do what you need to to create the customer 

     return customers; 
    } 
} 

時間のほとんどは、カスタムコレクションは必要ありません - 私は、これは、これらの例1であると仮定しています。また、これらのメソッドの開発者発見を助けるので、ユーティリティメソッドをタイプ(この場合はCustomerタイプ)に追加することもできます。 (これはもっと味わい深いものです。これは静的メソッドなので、例えばCustomerUtilityまたはCustomerHelperのような任意のタイプに自由に入れることができます)。

私の最後の提案は、FindCustomers()からインターフェイスタイプを返すことで、将来的に実装の変更がより柔軟になるようにすることです。明らかにDAL.GetCustomers()IList<T>を実装した型を返す必要がありますが、(特にデータ層のような別の層の)APIメソッドでもインタフェース型を返す必要があります。

+0

私のDALはCollection のようなDTOのコレクションを返しています。私のBLメソッドでは、私はcoll実際のビジネスオブジェクトの操作は、 コレクション dtoList = DAL.GetCustomers();のような からです。 コレクション得意先=新しいコレクション(); foreach(dtoList内のCustomerDTO dto) { カスタマー顧客=新規顧客(dto); //コンストラクタをコピーして、オブジェクトのプロパティを埋め込みます。 customers.Add(customer); } 返品カスタマー: – Raghav

+0

@Raghav - 最新の回答をご覧ください。 –

+0

返信いただきありがとうございます!このリンクで一つの小さな疑問、: は「私たちは、コレクションを使用することをお勧め: http://blogs.msdn.com/kcwalina/archive/2005/09/26/474010.aspx Cwalinaは、という彼のコメントで言及します、ReadOnlyCollection 、またはKeyedCollection 出力とプロパティとインターフェイスの場合 IEnumerable 、ICollection 、IList の入力の場合 " IList <>ではなくCollection <>を返さなければならないのでしょうか?これについてあなたの意見を知りたいだけです。 – Raghav

2

あなたが本当にCustomerCollectionクラスにこれらのメソッドをしたい場合、私はしかし、私はあなたのCustomerCollectionクラスが冗長であると主張するだろうと消費者が直接行くことができます

public static ICollection<Customer> GetAllCustomers() 

または

public void FillWithAllCustomers() 

を示唆しています顧客オブジェクトのコレクションを取得する場合は、DALに送信します。

3

私の意見では、どちらもちょっと奇妙で混乱しています。 Collectionクラスを拡張するときには、クラスがコレクションであることを暗示しているので、データが含まれています。あなたはあなたの最初の例ではコレクションを拡張

public class Customercollection: Collection<Customer> 
{ 
} 

public class Customer 
{ 
    public static CustomerCollection FindCustomers() 
    { 
     return DAL.GetCustomers(); 
    } 
} 
1

:私はあなたが最初のケースでは、この方法は、静的作るとき、それはほとんど意味をなさないだろうと思います顧客を保管するためのカスタムコレクション。代わりに、コレクション<お客様>を返しています。私の提案は次のとおりです。

Collection<Customer> customers = Customers.FindCustomers(); 

あなたがFindCustomersを呼び出す場合二回あなたは、リスト内の各顧客を2回取得しますので、あなたの第二の例は、また少し奇妙である。このように使用

public static class Customers 
{ 

    public static Collection<Customer> FindCustomers() 
    { 
    //calls DAL and gets a Collection of customers 
    Collection<Customer> customers = DAL.GetCustomers(); 

    return customers; 
    } 
} 

0

いますが、使用することはありません。さらに別の方法は次のようになり

public class Customercollection: Collection<Customer> 
{ 

    public static Collection<Customer> FindCustomers() 
    { 
    //calls DAL and gets a Collection of customers 
    Collection<Customer> customers = DAL.GetCustomers(); 

    return customers; 
    } 
} 
1

私はFindCustomersメソッドをDALクラスに置くか、メソッドを保持するFinderクラスを作成します。可能であれば、より多くのファインダーメソッドが必要になるでしょう。あなたはこのような何かあれば

0

何を:あなたのコンストラクタでリストを使用して

public class CustomerCollection: Collection<Customer> 
{ 
    public CustomerCollection: : base(new List<Customer>()) 
    {} 

    public static IList<Customer> FindCustomers() 
    { 
    //return them from DAL 
    } 
} 

を独自の実装を記述することなく、自分のクラスの一覧での便利なメソッドを使用できるようになります。

public static Collection<T> ToCollection(this IEnumerable<T> seq) { 
    return new Collection<T>(seq.ToList()); 
} 

をし、このようにそれを使用します:

0

私はアンドリューの提案に、この拡張メソッドを追加したい

public static Collection<Customer> FindCustomers() { 
    return DAL.GetCustomers().Select(dto => new Customer(dto)).ToCollection(); 
} 

それとも、インターフェイスタイプを返すについてアンドリューのアドバイスで行く場合は、

public static IList<Customer> FindCustomers() { // or ICollection 
    return DAL.GetCustomers().Select(dto => new Customer(dto)).ToList(); 
}