使用 MvcContrib Grid 之类的东西是否会导致代码可读性倒退?

发布于 2024-09-06 19:05:25 字数 956 浏览 9 评论 0原文

我的意思是,现在我们已经采取了所有这些行动,以使用现代模板引擎尽可能地将 html 标记与代码分离(在过去,程序员通常只是在 php 中连接字符串,这很糟糕。)

然后我看了一下 co -worker生成html表格的代码,看起来是这样的:

<% Html.Grid(Model).Columns(column => {
    column.For(x => Html.ActionLink("Edit", "Edit", new { id = x.Id })).Attributes(width => "30px").DoNotEncode();
    column.For(x => Html.ActionLink("Delete", "Delete", new { id = x.Id }, new { @class = "delete" })).Attributes(width => "95px").DoNotEncode();
    column.For(x => x.Id).Named("Code");
    column.For(x => x.Name).Named("Name").HeaderAttributes(align => "left");
    column.For(x => x.CPF).Named("CPF");
})
.Attributes(width => "100%", border => "0", cellpadding => "0", cellspacing => "0", @class => "data-table")
.Empty("No users found!")
.RowStart(row => string.Format("<tr class='row{0}'>", row.IsAlternate ? "-alternating" : ""))
.Render();
%>

他觉得很棒,我觉得很丑,所以我想了解更多人的意见。

I mean, now we have all this movement towards separating your html markup from your code as much as possible using modern template engines (in the old days programmers usually just kept concatenating strings in php, which was terrible.)

Then I look at a co-worker's code for generating an html table, and it looks like:

<% Html.Grid(Model).Columns(column => {
    column.For(x => Html.ActionLink("Edit", "Edit", new { id = x.Id })).Attributes(width => "30px").DoNotEncode();
    column.For(x => Html.ActionLink("Delete", "Delete", new { id = x.Id }, new { @class = "delete" })).Attributes(width => "95px").DoNotEncode();
    column.For(x => x.Id).Named("Code");
    column.For(x => x.Name).Named("Name").HeaderAttributes(align => "left");
    column.For(x => x.CPF).Named("CPF");
})
.Attributes(width => "100%", border => "0", cellpadding => "0", cellspacing => "0", @class => "data-table")
.Empty("No users found!")
.RowStart(row => string.Format("<tr class='row{0}'>", row.IsAlternate ? "-alternating" : ""))
.Render();
%>

He thinks it's awesome, I think it's quite ugly, so I'd like to know more people's opinion.

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

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

发布评论

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

评论(4

甜味拾荒者 2024-09-13 19:05:26

乍一看很难看,但我认为它相当简洁并且考虑了很多逻辑。例如,标准 MVC 输出可以执行与此类似的操作(没有标记)

    <table>
    <% dim index as integer = 0
      if model.count > 0 then %>
       <tr>
        <th>Edit</th>
        <th>Delete</th>
        <th>Code</th>
        <th align="left">Name</th>
        <th>CFP</th>
       for each item in model
         if index mod 2 = 0 then%>
           <tr>
         <%else%>
           <tr class ="alternate">
         <%end if%>
           <td><%= html.actionLink("Edit", "Edit", New With {.id = item.id})%></td>
           <td><%= html.actionLink("Delete", "Delete", New With {.id = item.id})%></td>
           <td><%: item.id%></td>
           <td><%: item.Name%></td>
           <td><%: item.CPF%></td>
         <% index += 1
            Next%>
    <%Else%>
        <tr><td>No Rows Found</td></tr>
    <%end if%>

</table>

,而且我很确定我也错过了其中的一些内容。传统上,我必须跟踪行数或以某种方式知道它是否是交替行,有一种情况是我没有记录并且通常有很多 <% %><% %><% %> code> 标签来处理这些代码块。

Its ugly on first glance, but I think its rather concise and takes care of a lot of logic. For example, a standard MVC output to do something close to this (without the markup)

    <table>
    <% dim index as integer = 0
      if model.count > 0 then %>
       <tr>
        <th>Edit</th>
        <th>Delete</th>
        <th>Code</th>
        <th align="left">Name</th>
        <th>CFP</th>
       for each item in model
         if index mod 2 = 0 then%>
           <tr>
         <%else%>
           <tr class ="alternate">
         <%end if%>
           <td><%= html.actionLink("Edit", "Edit", New With {.id = item.id})%></td>
           <td><%= html.actionLink("Delete", "Delete", New With {.id = item.id})%></td>
           <td><%: item.id%></td>
           <td><%: item.Name%></td>
           <td><%: item.CPF%></td>
         <% index += 1
            Next%>
    <%Else%>
        <tr><td>No Rows Found</td></tr>
    <%end if%>

</table>

And I am pretty sure I missed a few things in there as well. Going traditional, I have to keep track of a row count or some way to know if it is an alternating row or not, have a case when I have no records and generally a lot of <% %> tags to handle those code blocks.

獨角戲 2024-09-13 19:05:25

对于设计师来说,这是代码可读性的倒退,这里没有两种意见。

从开发人员的角度来看,这将取决于。有些人喜欢,有些人不喜欢。就我个人而言,我喜欢它,并且与更标准的 foreach 技术相比更喜欢它。为什么?

您从一个简单的 和一个 foreach 开始。然后有些用户说您需要处理交替的行样式。首先添加 ifs。然后另一个用户说您需要能够按给定列进行排序。您可以使用另一个 ifs 来处理这个问题。第三个用户要求您处理分页 =>您的视图中还有另一个 ifsforeach 。你最终会得到意大利面条

结论:用正确的枪瞄准正确的目标。对于简单的表,传统方法效果很好,但是一旦你开始做更高级的事情,就使用助手。

For a designer it's a step backwards in code readability, there are no two opinions here.

From a developer standpoint it will depend. Some like it others don't. Personally I like it and prefer it compared to the more standard foreach technique. Why?

You start with a simple <table> and a foreach. Then some user says that you need to handle alternating row styles. You start by adding ifs. Then another user says that you need to be able to order by given column. You handle this with yet another ifs. A third user asks you to handle paging => yet another ifs and foreach in your view. You end up with spaghetti.

Conclusion: use the right gun for the right target. For simple tables the conventional approach works nicely, but once you start doing more advanced stuff use helpers.

浮生未歇 2024-09-13 19:05:25

MVC Contrib Grid 看起来很可怕,因为它拥有大量的功能。它是一个瑞士陆军级 HTML 扩展,可以为所有人做所有事情。正如其他人指出的那样,手工操作更加困难或更笨拙。我记得回到桌面时,编写同样笨拙的代码来配置网格和电子表格控件以获得正确的效果。

但是,如果您的需求比较温和,则只需编写自己的需求即可。 Phil Haack 有一篇关于 基于代码的转发器。还有一些更轻的 网格视图助手可能会为您提供帮助。这些功能并不那么强大,但可能更适合您应用程序中的某些工作。

MVC Contrib Grid looks scary just because it has tons and tons of capability. Its a swiss army class HTML extension doing all things for all people. As others noted doing it by hand is even harder or clumsier. I remember back on the desktop, writing equally clumsy code to configure grid and spreadsheet controls to get the right effects.

However, iff your needs are more modest, just write your own. Phil Haack had a great article on a code based repeater. There are also some lighter Grid View Helpers out there that might assist you. These arent as powerful, but might be more suitable to certain jobs in your application.

落日海湾 2024-09-13 19:05:25

我同意它不是很漂亮,但是如果您开始定义表的外观约定,您可以开始重构扩展方法来清理它。然后它甚至会干掉你的表定义。这是一个示例(减去实际的扩展方法)。

<% Html.Grid(Model).Columns(column => {
    InsertEdit(column).Width(30);
    InsertDelete(column).Width(95);
    column.For(x => x.Id).Named("Code");
    column.For(x => x.Name).Named("Name").LeftAlignHeader();
    column.For(x => x.CPF).Named("CPF");
})
.ApplyDefaultStyle()
.Class("data-table")
.Empty("No users found!")
.DefaultAlternatingStyle()
.Render();
%>

I'll agree that it isn't very pretty, but if you start defining conventions for how a table should look, you could start to refactor to extension methods to clean this up a lot. Then it even DRYs up your table definitions. Here's an example (minus the actual extensionthods).

<% Html.Grid(Model).Columns(column => {
    InsertEdit(column).Width(30);
    InsertDelete(column).Width(95);
    column.For(x => x.Id).Named("Code");
    column.For(x => x.Name).Named("Name").LeftAlignHeader();
    column.For(x => x.CPF).Named("CPF");
})
.ApplyDefaultStyle()
.Class("data-table")
.Empty("No users found!")
.DefaultAlternatingStyle()
.Render();
%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文