您将如何重构这个 Asp.net MVC 2 Html Helper?
快问。
您将如何重构这个 Asp.net MVC 2 HtmlHelper? 具体来说,在这种情况下使用 TagBuilder 类是否有意义?
public static MvcHtmlString BusinessDisplayContacts(this HtmlHelper helper, string phone, string cellPhone,
string fax, string website, string email, bool hideEmail)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<ul>");
if (!string.IsNullOrEmpty(phone)) {
sb.AppendLine("<li class=\"tel\">");
sb.AppendLine("<span class=\"type\">Work</span>:");
sb.AppendFormat("<span class=\"value\">{0}</span>",phone);
sb.AppendLine("</li>");
}
if (!string.IsNullOrEmpty(cellPhone)) {
sb.AppendLine("<li class=\"tel\">");
sb.AppendLine("<span class=\"type\">Cell</span> Phone:");
sb.AppendFormat("<span class=\"value\">{0}</span>",cellPhone);
sb.AppendLine("</li>");
}
if (!string.IsNullOrEmpty(fax)) {
sb.AppendLine("<li class=\"tel\">");
sb.AppendLine("<span class=\"type\">Fax</span>:");
sb.AppendFormat("<span class=\"value\">{0}</span>",fax);
sb.AppendLine("</li>");
}
if (!string.IsNullOrEmpty(website)) {
sb.AppendFormat("<li><a class=\"url\" href=\"{0}\">{0}</a></li>",website);
}
if (!hideEmail && !string.IsNullOrEmpty(email)) {
sb.AppendFormat("<li><a class=\"email\" href=\"mailto:{0}\">{0}</a></li>",email);
}
sb.AppendLine("</ul>");
if (sb.Length < 10)
{
return MvcHtmlString.Create("");
}
else {
return MvcHtmlString.Create(sb.ToString());
}
}
提前致谢。
更新:
感谢所有的建设性意见。 最后,我决定按照 @queen3 的建议将上述代码移至强类型部分视图中。
Quick question.
How would you refactor this Asp.net MVC 2 HtmlHelper?
Specifically would it make sense to use the TagBuilder class in this scenario?
public static MvcHtmlString BusinessDisplayContacts(this HtmlHelper helper, string phone, string cellPhone,
string fax, string website, string email, bool hideEmail)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<ul>");
if (!string.IsNullOrEmpty(phone)) {
sb.AppendLine("<li class=\"tel\">");
sb.AppendLine("<span class=\"type\">Work</span>:");
sb.AppendFormat("<span class=\"value\">{0}</span>",phone);
sb.AppendLine("</li>");
}
if (!string.IsNullOrEmpty(cellPhone)) {
sb.AppendLine("<li class=\"tel\">");
sb.AppendLine("<span class=\"type\">Cell</span> Phone:");
sb.AppendFormat("<span class=\"value\">{0}</span>",cellPhone);
sb.AppendLine("</li>");
}
if (!string.IsNullOrEmpty(fax)) {
sb.AppendLine("<li class=\"tel\">");
sb.AppendLine("<span class=\"type\">Fax</span>:");
sb.AppendFormat("<span class=\"value\">{0}</span>",fax);
sb.AppendLine("</li>");
}
if (!string.IsNullOrEmpty(website)) {
sb.AppendFormat("<li><a class=\"url\" href=\"{0}\">{0}</a></li>",website);
}
if (!hideEmail && !string.IsNullOrEmpty(email)) {
sb.AppendFormat("<li><a class=\"email\" href=\"mailto:{0}\">{0}</a></li>",email);
}
sb.AppendLine("</ul>");
if (sb.Length < 10)
{
return MvcHtmlString.Create("");
}
else {
return MvcHtmlString.Create(sb.ToString());
}
}
Thanks in advance.
UPDATE:
Thank for all of the constructive comments.
In the end I've decided to move the above code into a strongly typed partial view as per @queen3's suggestion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现人们经常遗漏的一件事是使用 C# 逐字字符串来完成类似的事情...例如
可以将其制作成
更具可读性的方式。
另一件事:我会将所有这些字符串 + bool 放入一个对象中,例如
ContactInfo
或其他内容,将助手的签名更改为BusinessDisplayContacts(this HtmlHelper helper, ContactInfo info)
- 这样您就可以添加/删除/修改电话号码和条件,而不会破坏现有代码。One thing I see people missing a lot, is using C# verbatim strings for stuff like that... e.g.
can be made into
which is way more readable.
Another thing: I would put all those strings + bool inside an object, like
ContactInfo
or something, changing the signatur of your helper toBusinessDisplayContacts(this HtmlHelper helper, ContactInfo info)
- this way you will be able to add/remove/modify phone numbers and conditions without breaking existing code.