具有自定义列名称的 C# MVC 通用存储库

发布于 2024-11-25 09:47:02 字数 1332 浏览 1 评论 0原文

我怎样才能实现类似以下的目标?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黒涩兲箜 2024-12-02 09:47:03
public class IHasId
{
    public int id { get; set; }
}

public interface IGenericRepository<T>
{
    int id { get; }

    T GetById(int id);
}

public class GenericRepository<T> : IGenericRepository<T> where T : IHasId
{
    public int id
    {
        get { throw new NotImplementedException(); }
    }

    public T GetById(int id)
    {
        return from tbl in dataContext.GetTable<T> where tbl.id == tid select tbl;
    }
}
public class IHasId
{
    public int id { get; set; }
}

public interface IGenericRepository<T>
{
    int id { get; }

    T GetById(int id);
}

public class GenericRepository<T> : IGenericRepository<T> where T : IHasId
{
    public int id
    {
        get { throw new NotImplementedException(); }
    }

    public T GetById(int id)
    {
        return from tbl in dataContext.GetTable<T> where tbl.id == tid select tbl;
    }
}
只是一片海 2024-12-02 09:47:03

您收到此错误是因为您接受每个类 其中 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 to where T : IHasIdProperty.

撑一把青伞 2024-12-02 09:47:02

这里有一些问题 - 首先是您匹配的泛型类型是一个类,但类没有名为“id”的属性。您需要让您的 Category 类实现一个公开“id”属性的接口:

public interface IIdentity
{
    int identity { get; set; }
}

public class Category : IIdentity
{
    public int identity{ get; set; }
}

我不知道为什么您将“id”公开为 IGenericRepository 接口上的属性 - 当然这应该是传递给find 方法(如您的实现所示)。您还需要将对“GetById”方法的限制从:更改

where T : class

为类似

where T : IIdentity

使用我上面建议的接口。

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:

public interface IIdentity
{
    int identity { get; set; }
}

public class Category : IIdentity
{
    public int identity{ get; set; }
}

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:

where T : class

to something like

where T : IIdentity

using the interface I've suggested above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文