如果存储库实现的IQueryable&LT; T&GT;? [英] Should repositories implement IQueryable<T>?
问题描述
我在考虑某家机构两个IRepository接口,一个是IQueryable的后裔,一个包含IQueryable的。
I'm considering one of two IRepository interfaces, one that is a descendant of IQueryable and one that contains IQueryable.
这样的:
public interface IRepository<T> : IQueryable<T>
{
T Save(T entity);
void Delete(T entity);
}
或者这样:
public interface IRepository<T>
{
T Save(T entity);
void Delete(T entity);
IQueryable<T> Query();
}
LINQ用法是:
LINQ usage would be:
from dos
in ServiceLocator.Current.GetInstance<IRepository<DomainObject>>()
where dos.Id == id
select dos
或者
from dos
in ServiceLocator.Current.GetInstance<IRepository<DomainObject>>().Query
where dos.Id == id
select dos
我还挺喜欢第一个,但它是有问题的嘲笑。如何有其他人实现LINQable,mockable库?
I kinda like the first one, but it's problematic to mock. How have other people implemented LINQable, mockable repositories?
推荐答案
如果你想要一个有-A或IS-A关系依赖。
Depends on if you want a Has-A or an Is-A relationship.
第一个是is-a的关系。所述IRepository接口是IQueryable的接口。第二种是具有-一个。该IRepository有一个IQueryable接口。在写作本书的过程中,其实我喜欢第二个更好的那么首先,仅仅是因为在使用你的第二个IRepository,我可以给查询()方法,任何返回的IQueryable。对我来说,这是更灵活那么第一个实施。
The first one is an Is-A relationship. The IRepository interface is a IQueryable interface. The second is a has-a. The IRepository has an IQueryable interface. In the process of writing this, I actually like the second better then the first, simply because when use your second IRepository, I can give the Query() method ANYTHING that returns IQueryable. To me, that is more flexible then the first implementation.
这篇关于如果存储库实现的IQueryable&LT; T&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!