Tutorial :The IRepository interface


I am reading the source code of subsonic 3. in the file IRepository.cs I found the following:

public interface IRepository<T>  {      IQueryable<T> GetAll();      PagedList<T> GetPaged<TKey>(Func<T, TKey> orderBy, int pageIndex, int pageSize);       ...many other lines      bool Load<T>(T item, Expression<Func<T, bool>> expression) where T : class, new();      bool Load<T>(T item, string column, object value) where T : class, new();  }  

Notice the Load methods are defined as generic and their generic type names are the same as the generic type of the interface, which cause complier warnings.

My questions is: Are the Load methods really intended to be generic or it was a mistake? If the methods were intended to be generic, should we change the type name from "T" to something different like "E" in order to make the compiler happy?


They're not supposed to be different - Load is supposed to work on the repos "type" so you can remove the definition there (I assume that's what you're doing)


They have constraints, unlike the class-level T, so I'd guess they're supposed to be different.


Looks to me like the class and new constraints should be at the class level - not the method level.

Otherwise, yeah - you just defined 2 different T constraints which is just confusing as hell, since I could do this:

IRepository<int> intRepository = new RepositoryImpl<int>();     object o;  intRepository.Load<string>(o, "column", "value");  

Having a IRepository<int> Load<string> seems rather odd to me.

It's possible that T must only be a class and new for the Load methods, but not the others. In that case, you may want something like:

interface IRepository<T> {     IQueryable<T> GetAll();     bool Load<TClass>(TClass item, string column, object value) where TClass : class, T, new();  }  

which isn't exactly the same, since TClass could just inherit from T - but is the closest constraint I can think of to produce that result.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »