I'm implementing a repository and keep wondering about making it a bit more friendly to the user. Right now I have an IEntity
interface that specifies an Id
field:
public interface IEntity<T>
{
T Id { get; set; }
}
And my repository allows users to get an new instance by that id. Now the types it can handle need to implement the IEntity
interface, so I have a generic constraint on the repository Get
method:
public class Repository
{
public T Get<T, U>(U id) where T: IEntity<U>
{
// fake implementation, but T is required at compile time
var result = Activator.CreateInstance<T>();
result.Id = id;
return result;
}
}
There's an obvious relation between T
and U
and compiler understands it well enough to flag miss-usages, but not enough to enable type inference - each call to Get
requires specifying the generic parameters explicitly. I know there's no way around specifying T
, but how can I improve the method signature so that specifying U
is not required? Right now I have an overload for the most common usage:
public T Get<T>(int id) where T : IEntity<int>
{
return Get<T, int>(id);
}
I'm wondering if it is possible to somehow specify an open generic interface as a constraint or what would be a better method signature for the general case.