【发布时间】:2011-10-09 13:41:33
【问题描述】:
我正在尝试为我的IRepository 接口创建一个模拟:
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);
}
这里是异常截图:
【问题讨论】:
-
“但是当我使用它时”——给我们看代码?向我们展示堆栈跟踪?
-
你想做什么?我最好的猜测是你希望得到一些魔法。该行为完全符合预期。
标签: c# .net linq iqueryable