Consider the following scenario:
public interface IEntity<TKey>
{
TKey { get; set; }
}
public interface IRepository<TEntity, TKey>
where TEntity : IEntity<TKey>
{
void Store(TEntity entity);
void Delete(TKey key);
}
Why do I need to expliclty add TKey
as a generic argument to IRepository
?
Can't the compiler deduce or infer it from TEntity
's type?
I'd like to achieve something like this:
public interface IRepository<TEntity>
where TEntity : IEntity<TKey>
{
void Store(TEntity entity);
void Delete(TKey key);
}
It's not like TKey
is only known at runtime:
IRepository<User> userRepo = new ConcreteRepository<User>();
Where User
implements IEntity<string>