开发者

Using Generics on a Repository base class

开发者 https://www.devze.com 2022-12-28 19:47 出处:网络
I have the following model where I just modified my IEntity interface to take a generic type since I have multiple identity types.

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

精彩评论

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