2012-04-22 63 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