开发者

Asp.net Mvc 2: Repository, Paging, and Filtering how to?

开发者 https://www.devze.com 2022-12-23 22:03 出处:网络
It makes sense to pass a filter object to the repository so it can limit what records return: var开发者_如何学JAVA myFilterObject = myFilterFactory.GetBlank();

It makes sense to pass a filter object to the repository so it can limit what records return:

var开发者_如何学JAVA myFilterObject = myFilterFactory.GetBlank();
myFilterObject.AddFilter( new Filter { "transmission", "eq", "Automatic"} );
var myCars = myRepository.GetCars(myfilterObject);

Key question: how would you implement paging and where? Any links on how to return a LazyList from a Repository as it would apply here? Would this be part of the filter object? Something like:

myFilterObject.AddFilter( new Filter { "StartAtRecord", "eq", "45"} );
myFilterObject.AddFilter( new Filter { "GetQuantity", "eq", "15"} );
var myCars = myRepository.GetCars(myfilterObject);

I assume the repository must implement filtering, otherwise you would get all records.


I implement paging/sorting in my service layer. I think some people would disagree with this, but it works great for me. Make sure your repository returns an IQueryable though.

public class ProductService
{
   private IRepository<Product> Products {get; set;}

public IEnumerable<ProductDto> GetProductsMatching(FilterCriteria criteria) { var products = Products.Query() .Where( // do filtering ) .OrderBy( // order by ) .Skip(criteria.PageSize * criteria.CurrentPage) .Take(criteria.PageSize); var dtos = products.Select( // do mapping ); return dtos; } }

How you return a LazyList/IQueryable depends on what ORM you are using. I am only familiar with NHibernate (use Linq to NHibernate) and Linq2Sql.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号