我试图创建一个模拟我IRepository
接口:列表<T>实现IQueryable的<T>
public interface IRepository<T> : ICollection<T>, IQueryable<T>
{
}
使用这种实现:
public class RepositoryFake<T> : List<T>, IRepository<T>
{
public Expression Expression
{
get
{
return this.AsQueryable().Expression;
}
}
public Type ElementType
{
get
{
return this.AsQueryable().ElementType;
}
}
public IQueryProvider Provider
{
get
{
return this.AsQueryable().Provider;
}
}
}
但是,当我使用它,我越来越StackOverflow
异常。如何正确实现此界面以便能够仅使用List
作为存储库?
用法很简单
[Test]
public void Test()
{
RepositoryFake<User> users = new RepositoryFake<User>();
users.Add(new User());
List<User> list = (from user in users
where user.Id == "5"
select user).ToList();
Assert.That(list, Is.Empty);
}
这里是例外的截图:
“但是,当我使用它” - 显示我们的代码?向我们展示堆栈跟踪? – cdhowie
你想做什么?我最好的猜测是你希望有一些魔法。行为是完全预期的。 – leppie