具有自定义列名称的 C# MVC 通用存储库
我怎样才能实现类似以下的目标?
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);
当然,在这种用法中,GenericRepository 中的 tbl.id 给了我一个错误。
解决方案
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);
How can I achieve something like the following?
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;
}
}
And I would like to use this as follows:
GenericRepository gr = new GenericRepository();
Category cat = gr.GetById<Category>(15);
Of course, in this usage, tbl.id
in the GenericRepository gives me an error.
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;
}
}
And apart from these, DON'T forget to define this somewhere in your model:
public partial class Category : IHasId { }
And the usage is:
Repository rep = new Repository();
Category cat = rep.GetById<Category>(15);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您收到此错误是因为您接受每个类
其中 T : class
。类没有该属性。创建一个抽象类或接口以确保此属性存在,并将
where T : class
更改为where T : IHasIdProperty
。You get this error because you accept every class
where T : class
. A class don't have that property.Create an abstract class or interface to make sure that this property exists and change
where T : class
towhere T : IHasIdProperty
.这里有一些问题 - 首先是您匹配的泛型类型是一个类,但类没有名为“id”的属性。您需要让您的 Category 类实现一个公开“id”属性的接口:
我不知道为什么您将“id”公开为 IGenericRepository 接口上的属性 - 当然这应该是传递给find 方法(如您的实现所示)。您还需要将对“GetById”方法的限制从:更改
为类似
使用我上面建议的接口。
There's a few problems here - the first is that the generic type you're matching is a class, but a class doesn't have a property called 'id'. You need to have your Category class implement an interface that exposes an 'id' property:
I don't know why you've exposed 'id' as a property on the IGenericRepository interface - surely this is supposed to be a parameter passed to the find method (as indicated by your implementation). You also need to change the restriction on the 'GetById' method from:
to something like
using the interface I've suggested above.