我要在这里展示我的经验不足,但嘿 - 就像任何开发者,我想学习。正确的接口使用/ LINQ到SQL
考虑到如下界面:
public interface IRepository
{
entityDB Database { get; set; }
IQueryable<T> All<T>() where T:class, new();
T Single<T>(Expression<Func<T, bool>> expression) where T : class, new();
IList<T> Find<T>(Expression<Func<T, bool>> expression) where T : class, new();
}
我将如何去建立一个资源库类,这样我可以有泛型类型T替换为一个强类型?
假设我有一个具有Photos实体和CameraSettings实体的照片组合站点,我该如何定义包含在具体类中的类型 - 因此L2S - 是什么?目前,当我实现类,它期望的格式如下:
public class PhotoRepository : IRepository
{
public override IQueryable<T> All<T>()
{
// code goes here...
}
public override T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)
{
// ...and here...
}
public override IList<T> Find<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)
{
// ... and let's not forget here.
}
}
难道我甚至要对这个正确的方式,或者我应该使用看起来像这样的接口:
public interface IRepository<T> where T:class
{
entityDB Database { get; set; }
IQueryable<T> All();
T Single<T>;
IList<T> Find();
}
我只是想让我的单元测试正常工作,并确信我没有学习坏习惯(或简单丑陋)的习惯。感谢正确的方向。
我不认为我需要。我同意Preben Huybrechts采取的方法。 – 2012-07-12 00:00:58