MVC 逻辑、视图和助手 - 一个具体的故事

发布于 2024-10-05 18:42:56 字数 1109 浏览 5 评论 0原文

我和一位同事正在讨论如何在页面上生成链接。我们应该使用 html 助手还是在视图中保留非常简单的视图逻辑?

对于这个项目,我们使用 Castle Monorail 和 NVelocity 视图引擎。我将感谢任何考虑以下两种选择并提出意见的人。

在这个故事中,该链接当前仅在单个页面上使用。

选项 1 - 使用 Helper

帮助程序代码

 var action = snail.IsActive ? "ConfirmDeactivate" : "ConfirmActivate";
   var routeValues = new Dictionary<string, string>
                      {
                       {"action", action},
                       {"querystring", "id=" + snail.ID}
                      };
   var href = UrlHelper.For(routeValues);

   var link = new XElement("a");
   link.SetAttributeValue("href", href);
   link.SetValue(action.Substring(7));

   return link.ToString();

然后在视图中,我们只需像这样调用助手:

<li>$Html.SnailActivationSwitchLink($item)</li>

选项 2 - 全部在视图中

#if($snail.IsActive)
  <a href="$Url.For("%{action='ConfirmDeactivate', querystring='id=$snail.ID'}")">Deactivate</a>
  #else
   <a href="$Url.For("%{action='ConfirmActivate', querystring='id=$snail.ID'}")">Activate</a>
  #end

Me and a colleague are discussing how we would generate a link on a page. Should we use html helpers or keep very simple view logic in the view?

For this project we are using Castle Monorail and the NVelocity view engine. I would be grateful to anyone who considers both options below and gives their opinions.

In this story, the link is currently only used on a single page.

Option 1 - With Helper

Helper Code

 var action = snail.IsActive ? "ConfirmDeactivate" : "ConfirmActivate";
   var routeValues = new Dictionary<string, string>
                      {
                       {"action", action},
                       {"querystring", "id=" + snail.ID}
                      };
   var href = UrlHelper.For(routeValues);

   var link = new XElement("a");
   link.SetAttributeValue("href", href);
   link.SetValue(action.Substring(7));

   return link.ToString();

And then in the view, we just call the helper like so:

<li>$Html.SnailActivationSwitchLink($item)</li>

Option 2 - All in the View

#if($snail.IsActive)
  <a href="$Url.For("%{action='ConfirmDeactivate', querystring='id=$snail.ID'}")">Deactivate</a>
  #else
   <a href="$Url.For("%{action='ConfirmActivate', querystring='id=$snail.ID'}")">Activate</a>
  #end

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

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

发布评论

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

评论(3

葬シ愛 2024-10-12 18:42:56

对我来说,我更喜欢选项1。在helper上做逻辑更方便,也更优雅。

For me I prefer option 1. It is more convenient to do the logic on the helper and more elegant.

看海 2024-10-12 18:42:56

我会强烈建议/使用选项 1 - 无论您正在开发的视图数量有多少,

  • 让您的视图变得愚蠢。 - 对于单个视图或 100 个视图并不重要。

I will strogly suggest/use option 1 - irrespective of number of views you are developing

  • Make your view Dumb. - doesn matter for a a single view or 100 views.
爱情眠于流年 2024-10-12 18:42:56

那么选项 1 得到了很多人的支持——是时候唱反调了!

应用 DRY 等概念的主要原因之一是使应用程序更容易更改/更易于维护。那么:

  • 如果链接中的文本稍后需要更改怎么办?或者我想在其中放置图像而不是文本?我真的需要重建应用程序以删除链接中的拼写错误吗?

  • 更糟糕的是,如果不同页面上的链接文本需要不同怎么办?然后我是否为每个页面添加另一个辅助方法?

  • 假设是我做出了改变。如果我要求我的标记专家更改图像的链接,或者向 a 元素添加一个类以对其进行样式设置,该怎么办?他非常乐意编辑视图文件,但如果他看到助手调用,他将不知道 HTML 来自哪里以及在哪里更改它。即使他找到了帮助程序类,他是否知道如何更改 C# 代码来执行他需要的操作?他所做的只是改变一些文字!为什么它必须如此困难?

  • 我可以通过在帮助器上添加两个方法参数并传入每个链接的文本来缓解上述一些问题,但如果文本是图像标签怎么办?我现在有了需要在助手调用中转义的 HTML - 这看起来会非常混乱。我仍然无法编辑链接元素的属性,我是否应该为此添加一个/多个参数?它在哪里停止?

本质上,我认为我可以向大多数人展示选项 2,他们会理解它并能够修改它。漂亮的纯 HTML,带有一些易于遵循的条件逻辑。选项 1 增加了我不需要的复杂性和抽象性。

Lots of support for Option 1 then - time to play devil's advocate!

One of the main reasons for applying concepts like DRY is to make the application easier to change/more maintainable. So:

  • What if the text in the link needs to change later? Or I want to put an image in there instead of the text? Do I really want to have to rebuild the application to remove a typo in a link?

  • Worse than that, what if the link text needs to be different on different pages? Do I then add another helper method for each page?

  • That's assuming it's me that's making the change. What if I ask my markup guru to change the link to an image, or add a class to the a element to style it up? He's perfectly happy to edit view files, but if he saw the helper call he'd have no idea where the HTML was coming from and where to change it. Even if he found the helper class would he know how to change the C# code to do what he needs? All he's trying to do is change a bit of text! Why does it have to be so difficult?

  • I could alleviate some of the above issues by adding two method parameters on the helper, and passing in the text for each link, but what if the text was an image tag? I've now got HTML that I need to escape in my helper call - that's going to look pretty messy. I still couldn't edit the link element's attributes either, should I add a parameter/parameters for that as well? Where does it stop?

In essence I'll argue that I can show Option 2 to most people and they'll understand it and be able to modify it. Nice plain HTML with some easy to follow conditional logic. Option 1 adds complexity and abstration that I don't need.

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