I'm refactoring all my repository interfaces of various types. Most of them contain very similar methods like Add, Update but some have methods which only makes sense for a specific type. This is a best practices question.
I thought about using generics to straighten things up.
public interface IRepository<T>
{
T Get(int id);
void Add(T x);
}
But now for the specific methods. I could ofcourse "subclass" the interface, but then I'm not better off than before. I would have code like:
IUserRepository<User> users;
One neat way would be if I could have multiple constraints like:
public partial interface IRepository<T>
{
T Get(int id);
void Add(T x);
}
public partial interface IRepository<T> where T: User
{
T Get(Guid id);
}
public partial interface IRepository<T> where T: Order
{
T Get(string hash);
}
But the compiler complains about conflicting inheritance. Annother way would be contraints on the methods:
public partial interface IRepository<T>
{
T Get(int id);
void Add(T x);
T Get(Guid id) where T: User;
T Get(string hash) where T: Order;
}
But that's not quite the way these this work is it. Compiler fathoms not my intentions and wants a type definition on the method, of course.
Right now I just have methods that throw NotImplemented. Ugly.
I'm looking for a solution that will make me kick myself.