IRepository接口

时间:2009-07-16 06:30:55

标签: subsonic3

我正在阅读亚音速3的源代码。 在IRepository.cs文件中,我发现了以下内容:

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();
}

请注意,Load方法定义为通用,其泛型类型名称与接口的泛型类型相同,这会导致编译器警告。

我的问题是:Load方法真的是通用的还是错误的?如果这些方法是通用的,我们是否应该将类型名称从“T”更改为不同的类似“E”以使编译器满意?

3 个答案:

答案 0 :(得分:1)

它们不应该是不同的 - Load应该在repos“type”上工作,所以你可以删除那里的定义(我假设你正在做的事情)

答案 1 :(得分:0)

他们有限制,不像班级T,所以我猜他们应该是不同的。

答案 2 :(得分:0)

在我看来,classnew约束应该在类级别 - 而不是方法级别。

否则,是的 - 你刚刚定义了2个不同的T约束,这只是令人困惑的地狱,因为我可以这样做:

IRepository<int> intRepository = new RepositoryImpl<int>(); 

object o;
intRepository.Load<string>(o, "column", "value");

拥有IRepository<int> Load<string>对我来说似乎很奇怪。

可能对于class方法,T必须只是newLoad,而不是其他方法。在这种情况下,您可能需要以下内容:

interface IRepository<T> {
   IQueryable<T> GetAll();
   bool Load<TClass>(TClass item, string column, object value) where TClass : class, T, new();
}
由于TClass只能从<{1}} 继承,所以

产生这种结果。