次のような方法を実現するにはどうすればよいですか?C#カスタム列名を持つ汎用リポジトリ
public interface IGenericRepository
{
int id { get; }
T GetById<T>() where T : class
}
public class GenericRepository : IGenericRepository
{
//Some code here
public T GetById<T>(int tid) where T : class
{
return from tbl in dataContext.GetTable<T> where tbl.id == tid select tbl;
}
}
そして、次のように私はこれを使用したいと思います。もちろん
GenericRepository gr = new GenericRepository();
Category cat = gr.GetById<Category>(15);
、この用法では、tbl.id
GenericRepositoryでは私にエラーを与えます。
SOLUTION
public class IHasId
{
public int id { get; set; }
}
public interface IGenericRepository
{
int id { get; }
T GetById<T>(int id) where T : IHasId;
}
public class GenericRepository : IGenericRepository
{
public int id
{
get { throw new NotImplementedException(); }
}
public T GetById<T>(int id) where T : IHasId
{
return from tbl in dataContext.GetTable<T> where tbl.id == tid select tbl;
}
}
そして、これらとは別に、あなたのモデルでは、このどこかで定義することを忘れないでください:
public partial class Category : IHasId { }
と使用方法は次のとおりです。
Repository rep = new Repository();
Category cat = rep.GetById<Category>(15);
くそー!私は最終的に問題を理解しました。これらのコードとは別に、私が使用する各エンティティの部分クラスを作成しなければなりませんでした。私がカテゴリテーブルを持っていると言うと、私はIBaseの継承を使ってパブリック部分カテゴリを作成しなければならなかった!ありがとう、相棒!! – Shaokan
Ihasied inheritance sorry :) – Shaokan