我有一个来自存储库的产品列表。很简单。现在我想添加过滤和排序。排序可能发生在存储库之外,因为在存储库内进行排序没有效率提升(猜测)。我无法想象在存储库之外进行过滤,因为我们只想加载我们关心的记录。我认为您会想要创建一个过滤器委托并将其传递给存储库。
下面的代码是伪 C# 代码。排序/过滤的功能代码是什么样的?
下面的过程实际上集中在将委托传递给存储库以进行过滤:
Product myProduct = Repo.GetProducts( filter );
(如果这是 MS MVC,此代码将存在于控制器中):
设置过滤器:
//TODO: Need filter class definition
var filter = new Filter(); // made up object class for now
filter.AddCondition( field, operator, value); // do this for each filter condition
filter.AddCondition( Product.Name, contains, "Hammer"); // Product.Name ?? (Example)
Product myProducts = Repo.GetProducts( filter ); // the Product call **FILTER**
设置排序:
// TODO: Need sort class definition
var sort = new Sort(); // another made up object class for now
sort.AddOrder( field, priority, sequenceUp) // Sequence enum is ascending/descending
sort.AddOrder( Product.Name, 1, ascending) // (Example) **SORT**
myProducts.AddSort(sort);
返回一个视图模型:
// Next part strips off unnecessary fields for view (Presentation model)
// So we are not sending a huge data model to the view (hopefully good)
// TODO: Replace string with Service? function to extract a miniProduct from Product
MiniProduct myMinis = MakeMiniProductsFrom( myProducts); // Service?
// Determine response type (XML, JSON, HTML View) and return appropriate data
// use "x-requested-by" as per Rob Conery noted below
if (Request.IsAjaxRequest()) return Json(myMinis);
else return View(myMinis);
如您所见,此代码需要一些帮助。我真的在寻找可以使这项工作的排序和过滤类代码,或指向外部资源的链接。我假设排序和过滤是 DDD 和设计模式中的标准做法,因此是个问题。假设 Product 是一个普通的旧电子商务产品;) Rob Conery 的 Ajax 笔记在这里
谢谢。