开发者

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.

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.

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 开发者_Python百科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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜