设计问题
我正在使用 mvc2 构建一个站点,其结构如下:
Factory ->域.对象-> mvc2
一个工厂包含 logType 的枚举,它详细说明了已应用于对象的操作。 域请求该对象并将其提供给 MVC2 模型,该对象作为 IEnumerable Ilog 传递给视图,并且视图对其进行迭代。
我的问题是我希望视图根据日志类型创建链接,因此我在视图中有一个 switch 语句来创建此功能。 switch 语句正在利用存储库中的枚举,但这对我来说似乎有点错误:将存储库暴露给视图。
我应该将 Log 类型封装在域中对象的新 IList 中吗?或者可以在存储库中引用此枚举吗?
如果这些都不理想,那么最好的解决方案是什么?
感谢我可能得到的任何帮助。
I'm building a site using mvc2 and have a structure like follows:
Factory -> Domain.objects -> mvc2
One factory contains an enum for logType, which details the actions which have been applied to an object.
The domain requests the object and supplies it to a MVC2 model, this is passed to a view as IEnumerable Ilog, and the view iterates over it.
My issue is that I want the view to create a link based on the log type, so I have a switch statement in the view which creates this functionality. The switch statement is utilising the enum right back in the repository, but this seems a bit wrong to me: exposing the repository to the view.
Should I encapsulate the Log type in a new IList of objects in the domain? Or is it ok referencing this enum in the repository.
If neither of these are ideal, what is the best solution?
Thanks for any help I might get.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的解决方案是使用视图模型。视图模型是专门针对视图需求定制的类。因此,这是控制器操作的典型工作流程:
因此在这种情况下视图不了解您的任何域模型。这是一般情况。
现在,对于根据枚举值生成不同链接的特定情况,我认为在视图中编写 if 和 switch 会很丑陋。你没发现吗?因此,编写一个自定义 HTML 帮助程序来根据视图模型生成正确的链接会很棒,尤其是当您的视图如下所示时:
The best solution would be to use a view model. A view model is a class which is specially tailored to the needs of the view. So here's a typical workflow for a controller action:
So in this scenario the view has no knowledge of any of your domain models. That's in general.
Now for your particular case of generating different links based on the value of the enum I think that writing ifs and switches in a view makes in ugly. Don't you find? So writing a custom HTML helper that will generate the proper link based on the view model would be great especially when your view looks like this:
我的想法是,这是视图模型的重要用途之一,并将该信息包含在视图模型中。
My thought is that this is one of those great uses for a viewmodel and include that information in the viewmodel.