在MVC的Web应用程序,谁负责过滤的对象,视图或模型大集合? [英] In a MVC web application, who is responsible for filtering large collections of objects, view or model?

查看:133
本文介绍了在MVC的Web应用程序,谁负责过滤的对象,视图或模型大集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须建立在MVC设计的Web应用程序。

I have a web application built on an MVC design.

我有一个包含大量对象(论坛线程),我不能加载到内存一次的数据库。我现在想显示(部分)这个系列具有不同的过滤器生效(有点像用计算器按日期,票,标签等排序问题做了什么)。

I have a database which contains a large number of objects (forum threads) which I can't load into memory at once. I now want to display (part of) this collection with different filters in effect (kinda like what stackoverflow does with questions sorted by date, votes, tags etc).

我在哪里可以实现过滤逻辑?在我看来,这必须进入应用程序的模型部分,因为只有模型与数据库交互(在我的实现)。如果我做的视图过滤的一部分,那么该视图必须直接访问数据库来获取过滤的对象名单,对不对?我想避免这种情况,因为它暴露了数据库布局视图。但在同一时间,显示相同数据的不同视图的的应用程序的视图部分实现,因为他们是 - 不同的相同的看法数据。

Where do I implement the filtering logic? It seems to me that this must go into the model part of the application, as only models interact with the database (in my implementation). If I make the filtering a part of the view, then the view must access the database directly to get the list of filtered objects, right? I'd like to avoid this, because it exposes the database layout to the view. But at the same time, displaying different views of the same data should be implemented in the view part of the application, as they are just that -- different views of the same data.

那么,如何解决这个问题?难道我创建一个额外的模型,也就是说,FilteredThreadsList,并将它记得用,然后用FilteredView来显示FilteredThreadsList吐出线程​​列表过滤器?

So how do I resolve this? Do I create an additional model, say, FilteredThreadsList, and have it remember the filter to use, and then use a FilteredView to display the list of threads that FilteredThreadsList spits out?

还是我必须建立一个ThreadQueryier,使视图查询某些线程对象的数据库,这样我就可以在一个视图中的过滤逻辑不暴露后端数据库?

Or do I have to build a ThreadQueryier that allows views to query the database for certain thread objects, so I can have the filtering logic in a view without exposing the database backend?

推荐答案

从建筑的角度来看,在模式应具有code过滤。之所以如此,是因为在许多应用中code过滤是不平凡的,并在其领域逻辑的一个良好的数额。 (想想从股票列表过滤涨幅居前的)。从你的例子还有,它看起来是一样的,因为你可能要通过投票或按日期或标签,然后回答或无应答等进行过滤

From an architectural point of view, the model should be having the code for filtering. This is so, because in many applications the code for filtering is not trivial and has a good amount of domain logic in it. (Think of filtering top gainers from a list of stocks). From your example as well, it looks the same since you might want to filter by vote or by date or by tags and then by answered or unanswered etc.

在该处理实体的搜索/列表,并允许创建/读取/更新/实体,分页的删除,排序和过滤逻辑通常是非常通用的,并且可以在控制器基类来实现一些非常简单的应用程序由所有特定实体控制器类继承。

In some very simple applications that deal with search/list of entities and allows Create/Read/Update/Delete of an entity, the pagination, sorting and filtering logic is usually very generic and can be implemented in a controller base class that is inherited by all entity-specific controller classes.

底线是这样的:如果你的过滤逻辑是通用把它的控制器别人把它放在模型

The bottom line is this: if your filtering logic is generic put it in the controller else put it in the model.

这篇关于在MVC的Web应用程序,谁负责过滤的对象,视图或模型大集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆