【发布时间】:2014-08-29 15:01:58
【问题描述】:
我定义了以下接口:
public interface IReadOnlyRepositoryBase<TEntity, TKey, TCollection>
where TEntity : EntityBase<TKey>
where TCollection: IEnumerable<TEntity>
{
TCollection GetAll();
}
public interface IReadOnlyRepository<TEntity, TKey> :
IReadOnlyRepositoryBase<TEntity, TKey, IEnumerable<TEntity>>
where TEntity : EntityBase<TKey>
{ }
// there is also "ILazyReadOnlyRepository" where TCollection
// is IQueryable<T>..
现在我无法在我的实现中返回IEnumerable<TEntity>,因为IEnumerable<TEntity> 似乎无法转换为TCollection。
// basic repository impl for NHibernate
public abstract class NHibernateReadOnlyRepositoryBase<TEntity, TKey, TCollection>
: IReadOnlyRepositoryBase<TEntity, TKey, TCollection>
where TEntity : EntityBase<TKey>
where TCollection : IEnumerable<TEntity>
{
public TCollection GetAll()
{
// doesn't work...
return _session.QueryOver<TEntity>().List();
}
据我所见,该方法返回一个实现IEnumerable<T> 的IList<T>,那么这显然应该有效吗?我怎样才能实现我想要的?
【问题讨论】:
-
这应该如何工作?
TCollection也可以是List<TEntity>,在这种情况下,您会将IList<TEntity>转换为List<TEntity>。那是不可能的。
标签: c# linq generics collections repository