关于过滤和排序的简单存储库问题

发布于 2024-08-17 01:40:52 字数 1877 浏览 5 评论 0原文

我有一个来自存储库的产品列表。够简单的。现在我想添加过滤和排序。排序可能发生在存储库外部,因为在存储库内部执行排序不会提高效率(猜测)。我无法想象在存储库之外进行过滤,因为我们只想加载我们关心的记录。我认为您想要创建一个过滤器委托并将其传递到存储库。

以下代码是伪 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 注释是 此处

谢谢。

I have a list of products coming from a repository. Simple enough. Now I want to add filtering and sorting. Sorting could happen outside of the repository since there are no efficiency gains doing them inside the repository (guess). I couldn't imagine doing filtering outside of the repository since we only want to load records we care about. I would think you would want to create a filter delegate and pass it to the repository.

The below code is pseudo C# code. What would functioning code look like to sort / filter?

The process below is really centered around passing delegates to the repository to filter:

Product myProduct = Repo.GetProducts( filter );

(if this were MS MVC, this code would exist in the controller):

Setup a filter:

//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**

Setup a sort:

// 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);

Return a view model:

// 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);

As you can see, this code needs some help. I am really looking for sort and filter class code that could make this work, or links to outside sources. I assume sorting and filtering are a standard practice within DDD and design patterns, thus the question. Assume Product is a plain old e-commerce product ;) Rob Conery's Ajax notes are here

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南烟雨〆相思醉 2024-08-24 01:40:52

理想情况下,您可能希望在存储库内进行排序和过滤。如果您正在加载大型集合,这一点尤其重要,因为数据库很可能能够比您更有效地进行排序,并直接返回排序结果。

IQueryable接口是旨在干净地处理这个问题 - 它是实体框架、LINQ to SQL 和其他具有 LINQ 提供程序的数据库的基础。尝试支持(至少在某种程度上)IQueryable 的好处是,这可以让您以非常标准的方式使用存储库。例如,使用 IQueryable 的伪代码可能看起来更像:

IList<Product> myProducts = Repo.Products.Where( p => p.Category == theCategoryToFind ).OrderBy( p => p.Name );

或者,可选地,只是:

var products = from p in Repo.Products
               where p.Category == theCategory
               order by p.Name
               select p;

我建议查看一些面向 LINQ 的数据访问技术的工作原理 - 有相当多的技术一些开源选项,例如 Subsonic(以及许多商业选项)可能会提供有关如何进行操作的线索更好地设计它以提高可用性。

Ideally, you'll probably want to do the sorting and the filtering inside the repository. This is particularly important if you're loading large collections, since the DB will most likely be able to sort more efficiently than you can do, and return the sorted results directly.

The IQueryable<T> interface is designed to handle this cleanly - it's the basis for Entity Framework, LINQ to SQL, and other databases with a LINQ provider. The nice thing about trying to support (at least at some level) IQueryable<T> is this lets you use your repository in a very standard way. For example, your pseudocode, using IQueryable<T>, might look more like:

IList<Product> myProducts = Repo.Products.Where( p => p.Category == theCategoryToFind ).OrderBy( p => p.Name );

Or, optionally, just:

var products = from p in Repo.Products
               where p.Category == theCategory
               order by p.Name
               select p;

I recommend looking at how some of the LINQ-oriented data access technologies work - there are quite a few open source options, such as Subsonic (as well as many commercial options) that may provide clues as to how you can better design this for usability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文