我定义了以下接口:
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();
} 据我所见,该方法返回一个实现IList<T>的IEnumerable<T>,因此这显然可以工作吗?我怎样才能实现我想要的?
发布于 2014-08-29 15:07:49
您不应该使用泛型参数TCollection。使用泛型参数的一种方式是,该接口的用户应该能够定义该类型是什么,在这种情况下,该方法返回的类型是什么。
显然这对你来说是个问题。方法的实现需要无条件地返回一个IEnumerable,而不是调用方指定的未知类型。只要删除这个通用的参数就可以实现这一点。
public interface IReadOnlyRepositoryBase<TEntity, TKey>
where TEntity : EntityBase<TKey>
{
IQueryable<TEntity> GetAll();
}https://stackoverflow.com/questions/25571005
复制相似问题