I have an interface (which is used by repositories) that has this member:
T FindById(TId id)
where T : class, IEntity
where
That can't work the usual way around because you can't convince the compiler of the TId
constraint after the fact. You can, however, reverse the sequence, i.e.
var obj = ById(id).Find<SomeType>();
Not as elegant, but it works. Implementation:
public Finder<TId> ById<TId>(TId id) where TId : IEquatable<TId>
{
return new Finder<TId>(this, id);
}
public struct Finder<TId> where TId : IEquatable<TId>
{
private readonly YourParent parent;
private readonly TId id;
internal Finder(YourParent parent, TId id)
{
this.id = id;
this.parent = parent;
}
public T Find<T>() where T : class, IEntity<TId>
{
return parent.FindById<T, TId>(id);
}
}
caveat: it is probably easier just to tell it both the parameter types explicitly.