Asp.net Mvc 2: Repository, Paging, and Filtering how to?
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.
精彩评论