C# 将泛型抽象类实现到也是泛型的接口

C# 将泛型抽象类实现到也是泛型的接口,c#,.net,oop,generics,repository,C#,.net,Oop,Generics,Repository,我有一个抽象类: public abstract class Entity<T> where T : struct { public T ID { get; set; } ... other properties for modify } 公共抽象类实体,其中T:struct { 公共T ID{get;set;} …用于修改的其他属性 } 我想做的是在我的IRepository中实现这个类。我尝试的是: public interface IRepository&l

我有一个抽象类:

public abstract class Entity<T> where T : struct
{
    public T ID { get; set; }
    ... other properties for modify
}
公共抽象类实体,其中T:struct
{
公共T ID{get;set;}
…用于修改的其他属性
}
我想做的是在我的IRepository中实现这个类。我尝试的是:

public interface IRepository<T> where T : Entity<T> //Entity<T> doesn't make sense here i should use either T2 or what should i do?
public interface IRepository其中T:Entity//Entity在这里没有意义我应该使用T2或者我应该怎么做?
我也试着让它像这样工作:

public interface IRepository<T> where T : Entity<object>
公共接口i假设,其中T:Entity

实现这一目标的正确方法是什么

我不确定你想要实现什么,但以下是合法的;存储库与实体类具有相同的通用约束:

public interface IRepository<T> where T: struct
{
    Entity<T> GetEntityById(int id);
    ...
}

您可以定义以下抽象:

public abstract class Entity<TKey>
    where TKey : struct
{
    public TKey Id { get; set; }
}

public interface IRepository<TEntity, TKey>
    where TEntity : Entity<TKey>
    where TKey : struct
{
    IEnumerable<TEntity> GetAll();
    TEntity GetById(TKey id);
}
公共抽象类实体
其中TKey:struct
{
公钥Id{get;set;}
}
公共接口假定
其中tenty:实体
其中TKey:struct
{
IEnumerable GetAll();
TEntity GetById(TKey id);
}
然后作为一种用法,例如:

public class Repository<TEntity, TKey> : IRepository<TEntity, TKey>
    where TEntity : Entity<TKey>
    where TKey : struct
{
    DbContext db;
    public Repository(DbContext db)
    {
        this.db = db;
    }
    public IEnumerable<TEntity> GetAll()
    {
        return db.Set<TEntity>();
    }
    public TEntity GetById(TKey id)
    {
        return db.Set<TEntity>().FirstOrDefault(x => x.Id.Equals(id));
    }
}
公共类存储库:IRepository
其中tenty:实体
其中TKey:struct
{
DbContext数据库;
公共存储库(DbContext数据库)
{
这个.db=db;
}
公共IEnumerable GetAll()
{
返回db.Set();
}
公共TEntity GetById(TKey id)
{
返回db.Set().FirstOrDefault(x=>x.Id.Equals(Id));
}
}

如前所述,您的第一声喊叫是有意义的。所以这不是问题,这个设计有缺陷。请参阅我对这两个答案的评论,以了解原因。也许陈述一下你想要实现的目标,也许还有更好的方法。尝试过你的解决方案,它工作得很好(第二部分),但是
实体GetEntityById(int-id)是错误的。我尝试了
实体GetEntityById(intID)
,效果很好。
GetEntityById
应该返回
T
。这就是将T作为泛型参数的全部意义,否则,
IRepository
拥有
实体GetEntityById(int-id)
就足够了。由于与注释相同的原因而被否决。虽然这在语法上可行,但在语义上几乎没有用处。OP需要一个可用于满足特定限制的任何实体的存储库。在
GetById
期间,此存储库将适用于其
Id
属性固定了特定限制的任何实体(它必须是结构,但为什么?)。在调用
GetAll()
期间,它必须满足另一个限制。换句话说,我可以用完全不同的实体(
new)来关闭它Repository@CodingYoshi(1)
其中T:struct
是答案的一部分,因为它是问题的一部分。(2)上面的代码并不声称是存储库的真实/完整实现,但它正是OP想要的。(3)讨论存储库的正确实现与这个问题无关理论上,如果
Order
是struct,并且是
Id
属性的类型,并且没有违反任何规则,那么
Repository
对于此代码是可以接受的。我同意1和4。关于2:是,但你知道OP将如何使用它,因此一个好的答案将向OP说明缺陷。关于3:是和否。因此,更好的方法是询问OP知道他们试图实现什么,因为这种方法最终会打破这种模式。@Codingyosi这篇文章试图解决OP的编译时错误,并解决一些明显的语法错误+分享一个简单的例子。他不是问关于存储库的问题,所以讨论存储库是离题的(如果有人想谈论这个问题也没关系。)无论如何,在尊重你关于不喜欢这个答案的观点的同时,我不同意你的观点,我想说这个答案一点也不误导。它试图回答用户提出的问题。
public class Repository<TEntity, TKey> : IRepository<TEntity, TKey>
    where TEntity : Entity<TKey>
    where TKey : struct
{
    DbContext db;
    public Repository(DbContext db)
    {
        this.db = db;
    }
    public IEnumerable<TEntity> GetAll()
    {
        return db.Set<TEntity>();
    }
    public TEntity GetById(TKey id)
    {
        return db.Set<TEntity>().FirstOrDefault(x => x.Id.Equals(id));
    }
}