2012-04-22 60 views
0

我在类GenericRepository此方法:C#,GenericRepository,服务层

public virtual IEnumerable<TEntity> Get(
      Expression<Func<TEntity, bool>> filter = null, 
      Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, 
      string includeProperties = "") {...} 

而在服务层我有:

IAdsRepository _adsRepository; 

     public AdsService(IAdsRepository adsRepository) 
     { 
      _adsRepository= adsRepository; 
     } 

public IEnumerable<Ads> GetAllAds(....) 
     { 
      return _adsRepository.GetAll(....); 
     } 

(我有一个存储库,以指定genericRepository)

有人对如何传递给Get()方法的参数有一个想法?

非常感谢你,

回答

0

第一个参数Expression<Func<TEntity, bool>> filter需要一个过滤函数,它接受一个TEntity作为参数,并返回一个布尔值,如果实体应列入其结果是真实的。例如,x => x.Value > 7可以被传递到具有Get返回所有TEntities与Value大于7

第二参数接受一个I​​Queryable作为参数,并返回一个IOrderedQueryable(即,其中排序顺序被定义)例如x => x.OrderBy(y => y.Value)将命令结果通过Value

例如,然后;

repository.Get(x => x.Value > 7, x => x.OrderBy(y => y.Value)); 

将返回所有实体比7 Value更大时,值排序。

+0

感谢您的回答,有没有什么办法来推广我的服务(GetAll(...)),因为在控制器中我想指定GetAll的参数(...) – 2012-04-22 17:12:22

+0

@saisne我不确定我理解这个问题,你没有提到GetAll应该返回什么或者你想要它的参数。 – 2012-04-22 20:51:21