2011-01-08 28 views
1

私は再び申し訳ないが、私の最初の質問のこの質問継続: このインタフェースを検討:戻り値の型

interface IRepository<T,U> where T:class where U:class 
{ 
    IEnumerable<U> SelectAll(); 

    bool Insert(T item); 

    IEnumerable<T> FindAll(Func<T, bool> exp); 
} 

を、私はこのインタフェースを実装します。

public class Repository : IRepository<Customer,Select4ColumnsOfCustomers> 
{ 

    #region IRepository<Customer,Select4ColumnsOfCustomers> Members 

    public IEnumerable<Select4ColumnsOfCustomers> SelectAll() 
    { 
     throw new NotImplementedException(); 
    } 

    public bool Insert(Customer item) 
    { 
     throw new NotImplementedException(); 
    } 

    public IEnumerable<Customer> FindAll(Func<Customer, bool> exp) 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 
} 

public class Select4ColumnsOfCustomers 
{ 
    public int CustomerID { get; set; } 
    public string Address { get; set; } 
    public string City { get; set; } 
    public string Phone { get; set; } 
} 

私がしたいですnorthwindデータベースのCustomerテーブルのちょうど4列を返します。 Ok.this仕事が、私は、私はSを宣言する必要があり、他のタイプを返す他の方法で、Uを追加したい場合は、M、W、...インターフェイスで、私はこのコードを記述する必要があり、それの実装で:

public class Repository : IRepository<Customer,RetType1,RetType2,RetType3,....> 

これは良くありません。これに代わるものは何ですか?返り値の型にはvarを書くことができますか?戻り値の型はプレースホルダですか? ありがとう

+0

この種のものには、「Datatable」またはORMを使用することを検討してください。 – nan

答えて

3

あなたのリポジトリは複数のインターフェイスを実装できます。あなたはこのコードを一般化することができるように

public class Repository : IRepository<TResult1, T1>, IRepository<Tresult2, T2>, etc... 

あなたのSelectAllメソッドは、ORMを使用することを検討して

TResult SelectAll<TResult>(); 

しかしアンジェイが正しいとのような汎用的なものにする必要があります。 NHiberanteEntity Frameworkをチェックしてください。

+0

EFで私に例を教えてもらえますか? – Arian

+0

@Nima、私は実際にEFで多くの経験がありません。 NHibernateでは、私が通常行ってきたことは、CRUD操作と特定のクエリのための一連のQueryクラスを扱うリポジトリクラスを作成することです。 – Vadim