MVC2 部分视图 - 代码隐藏
我正在创建一个 MVC2 网站,它是在 Web 窗体中构建的网站的全新刷新版本。
由于某些功能的性质以及统计数据库及其结构,没有一种通用的方法来显示一些需要显示的信息。
因此,尽管在其他地方遵循 MVC 原则,但在一个功能中,需要为每个实例编码新的部分视图。这意味着已经创建了一个视图用户控件,并且我已将“代码隐藏”文件粘贴到其中,并扩展了 ViewUserControl 类...非常类似于 Web 窗体的执行方式。
这真的是错误的吗?或者有人可以提出一种更好的方法,只将模糊结构化的数据提供给部分视图,而不必将其全部放在那里?
谢谢!
I'm creating an MVC2 site which is a new, refreshed version of a site built in Web Forms.
Due to the nature of some of the features and the a database of statistics and their structure there is no one-size-fits-all way to display some of the information needed to be displayed.
Thus, despite following MVC principles elsewhere, in one feature a new partial view needs to be coded for each instance. This means that a View User Control has been made and I've stuck a "code behind" file into it, and extended the ViewUserControl class... much like the Web Forms way of doing it.
Is this really wrong, or can anyone suggest a better way of feeding only vaguely structured data to the partial view without having to put it all in there?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
丹尼尔,
在不知道全部细节的情况下,根据我对这个问题的理解,我会说“是”。基本上,如果数据的结构只是“模糊”,您应该检查共性并创建一个所有这些共享视图都将使用的界面。这样,您就可以获得重用的好处,而无需在类背后编写整体且可能不断增长的代码。
直到达到一定数量的重复(可能会出现几个版本)之前,这种优势可能并不明显。
如果你对此感到满意,它也将有助于更好的单元测试——这本身就是一个交易“制造者”。
Daniel,
with out knowing the full details and based on my understanding of the question, i'd say 'yes'. basically, if the data is only 'vaguely' structured, you should examine the commonalities and create an interface that all those shared views would use. that way, you get the benefit of reuse without having to code a monolithic and potentially ever-growing code behind class.
The advantages of this may not be apparent until you've hit a critical mass of repeats (which could a few versions down the line).
if you're comfortable with this, it'll also make for better unit testing as well - this could be in itself a deal 'maker'.