简体   繁体   English

在存储库基类上使用泛型

[英]Using Generics on a Repository base class

I have the following model where I just modified my IEntity interface to take a generic type since I have multiple identity types. 我有以下模型,由于我有多个标识类型,因此我刚刚修改了IEntity接口以采用通用类型。

public interface IEntity<T>
{
    T Id { get; set; }
}

public class Product : IEntity<int>
{
    public int Id { get; set; }
}

Now I have to modify my IRepository and IProductRepository to reflect this change but I am not sure how. 现在,我必须修改IRepository和IProductRepository以反映此更改,但是我不确定如何更改。

public interface IRepository<TEntity> where TEntity : IEntity
{
    void Add(TEntity entity);

    void Delete(TEntity entity);
}

public interface IProductRepository : IRepository<Product>
{

}

Anyone care to push me in the right direction? 有人愿意将我推向正确的方向吗?

Thanks 谢谢

You need to add a second generic parameter: 您需要添加第二个通用参数:

public interface IRepository<TEntity, TID> where TEntity : IEntity<TID>
{
    void Add(TEntity entity);

    void Delete(TEntity entity);
}


public interface IProductRepository : IRepository<Product, int>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM