如何使用 MVC 避免标签汤?

发布于 2024-12-23 08:39:23 字数 1224 浏览 3 评论 0原文

我读这篇文章是为了证明 MVC 相对于非 MVC(如常规旧 php)的合理性(不使用 MVC,甚至可以使用经典的 asp,尽管很痛苦):

http://www.codinghorror.com/blog/2008/07/web-development-as-tag-soup.html

我找不到答案。我认为获得标签汤是不可避免的。是的,我知道 MVC 将模型和控制器分开,但是当你到达视图时,一切都变得丑陋了。我可以读取发出的 html,与标签汤一样好甚至更好。

我不会使用单元测试,所以这并不是一个重要的优势。我不知道如何才能避免丑陋的视图,现在无论我如何让它枯萎 mvc 或只是发出 html。

我认为使用所有奇怪的编码(而且它就是代码)来维护视图并不比使用 response.write "" 更容易。

处理 ASP.NET MVC“标签汤”

示例 作者:Arnis(无意冒犯他或其他任何人),修复了问题中可怕的代码,但对我来说,这仍然看起来很糟糕,或者至少不是我所期望的。对我来说,这些尖括号也可能是 <% %>

我喜欢像 codeigniter 这样的东西,它确实是我见过的最干净的,但它仍然不是我所期望的。我想我希望 MVC 中存在一些魔力,让一切变得美丽。显然,除非一个人真的非常小心,否则没有比使用经典 asp 更好的了,因为它与视图相关。

这主要是关于视图。不是关于哪种语言更适合什么或谁的模板引擎是最好的(它们都有相同的标记混合倾向)。

相信我。我希望让 MVC 与我的合作开发人员一起工作,所以我根本不反对将它作为一种范例。我不能仅仅因为每个人都在做某件事或类似的事情就让他们同意某件事。

感谢您的任何评论。我必须能够证明这些事情的合理性,虽然我了解 MVC 和我所得到的东西,但这种观点让很多事情看起来像是浪费时间。

编辑:一切似乎都是针对特定的框架而不是计划。我看到了一些见解,但最终似乎除了纪律之外别无他法。谢谢大家的回答。

I was reading this in my quest to justify MVC over non-mvc like regular old php (not using MVC, even classic asp could be used, albeit painfully):

http://www.codinghorror.com/blog/2008/07/web-development-as-tag-soup.html

And I can't find the answer. I think it is inevitable to get tag soup. Yes, I know MVC separates the model and controller, but when you get to the view, everything just goes hideous. I can read emitted html just as good or better than tag soup.

I won't be using unit testing, so it's not that important an advantage. I'm not sure how I can ever avoid an ugly view, now matter how I get it wither mvc or just emitting html.

I do not see it any easier to maintain a view with all the bizarre coding (and it is code) than using response.write "<table>".

example: Dealing with ASP.NET MVC "tag soup"

The answer by Arnis (no offense to him or anyone else), fixes the horrible code in the question, but to me that still looks bad or at least not what I expect. To me those angle brackets might just as well have been <% %> or <?php ?>.

I like things like codeigniter and it's really the cleanest I've seen but it's still not what I expect. I think I was hoping some magic was present in MVC that made everything beautiful. Apparently, unless one is really really careful, there no better off than they were with classic asp, as it relates to a view.

This is mostly about the view. Not about which language is better for what or who's template engine is the greatest (they all have the same markup mixup tendencies).

Believe me. I want to make MVC work with my co-developers, so I'm not railing against it as a paradigm at all. I can't get them to agree to something just because everyone's doing it or something like that.

Thanks for any comments. I have to be able to justify these things, and while I understand MVC and what I am getting, the view makes a lot of it seem like a big waste of time.

edit: Everything seems to be geared toward a particular framework instead of plan. I see some insight but in the end it seems there is no way other than discipline. Thank you all for your answers.

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

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

发布评论

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

评论(7

一页 2024-12-30 08:39:23

看看如何使用 Razor 视图引擎,包含在 MVC 3 中。还要尝试将所有逻辑保留在 Controller 类中,并根据 View 中显示的内容构建模型

  • Razor 是避免标签混乱的一种明显方法,因为不需要任何 <%%> 标签 - 只需一个@ 在代码之前,视图引擎会计算出 C# 结束位置和 HTML 开始位置。

    @Model.Name;
    

    即使是循环和 if 语句在使用 Razor 和神奇的 @ 字符的 .cshtml 文件中看起来仍然很性感。

    @if(shouldDisplayDiv) {
        
    显示 Div!
    } @foreach(Model.Friends 中的用户 user) { }

    默认情况下,Razor 还会为您处理 HTML 编码,因此您的视图不会充满 Html.Encode 调用。 (注意:如果您需要输出 HTML,您可以使用 Html.Raw 辅助方法)。

  • 将逻辑放入控制器 理想情况下将消除视图中对大代码块的需要。尝试让模型对象包含视图的所有动态数据,就像视图中显示的那样。目标是在您的视图中完全没有任何 C# 代码(毫无意义,但如果这是目标,看看您能达到多接近它!)。

  • 部分视图可以很好地分隔视图的不同部分(但尽量不要使用它们太多)。您还可以将不同的模型对象传递给每个部分视图,我发现这对于一些大循环或类似 flair< /a>.

  • HTML 助手也非常有用(感谢 subkamran)。这里与上面提到的分部视图有类似的概念,但 HtmlHelpers 略有不同,因为您指定方法的参数(及其类型),这与您可以传递单个 Model 对象的分部视图相反。这是一个 如何实现它们的好例子。同样,这些在您的 cshtml 代码中看起来非常整洁。

     
    @Html.SomeMethod(Model, "String", 5)
    ;
  • 客户端 MVC 是另一种选择,如果您正在开发 AJAX 密集型 Web 应用程序,这是一个强烈的建议。按照控制器中的逻辑,您将使用诸如 Backbone.js 之类的客户端 MVC 框架以整洁的方式对 HTML 进行模板化,并使用 jQuery .ajax( ) 与您的控制器来回交谈。这是分离表示层的绝佳实践,为您留下一些漂亮的视图标记!

我坚持这些小准则,它对我来说就像一种魅力。漂亮、干净的 HTML 标记,偶尔带有 @ 字符。非常容易维护(至少视图是!)。

编辑:请注意,所有这些要点都包含在 ASP.NET MVC 3 中,并且就 Microsoft 而言都是“最佳实践”。无需安装任何额外的框架、插件或插件即可遵守这些准则。

Have a look at using the Razor view engine, which is included in MVC 3. Also try to keep all your logic in the Controller class and build a Model based on what is displayed in the View.

  • Razor is one obvious way to avoid tag soup as there's no need for any <% and %> tags - simply an @ before your code and the view engine works out where the C# ends and the HTML starts.

    <span class="name">@Model.Name</span>
    

    Even loops and if statements still look sexy in a .cshtml file with Razor and that magic @ character.

    @if(shouldDisplayDiv) {
        <div id="mydiv">Div is displayed!</div>
    }
    
    
    @foreach(User user in Model.Friends) {
        <a href="@user.Url"><img src="@user.ImageUrl" title="@user.Name" /></a>
    }
    

    Razor also handles HTML encoding for you by default, so your view won't be full of Html.Encode calls. (Note: if you need to output HTML, you can use the Html.Raw helper method).

  • Putting your logic into the Controller will ideally remove the need for large code blocks in the view. Try to get the model objects to contain all the dynamic data for the view, exactly as it will be displayed in the view. Aim to not have any C# code in your view at all (pointless, but if that's the target, see how close you can get to it!).

  • Partial views can nicely separate different parts of your view (but try not to use them too much). You can also pass a different model object to each partial view, which I find can be handy for some large loops, or a little something like a flair.

  • HTML Helpers are also very useful (thanks subkamran). There's a similar concept here to the partial views mentioned above, but HtmlHelpers are slightly different because you specify the method's parameters (and their types), as opposed to partial views which you may pass a single Model object. Here is a good example of how to implement them. Again, these can look very neat in your cshtml code.

     <div class="specialdiv">@Html.SomeMethod(Model, "String", 5)</div>
    
  • Client-side MVC is another option, and a strong suggestion if you are developing AJAX-heavy web applications. Following the logic in the controller you would use a client-side MVC framework like Backbone.js to template HTML in a tidy manner, and use jQuery .ajax() to talk back and forth with your controller. It's a great practice for separating your presentation layer, leaving you with some beautiful View markup!

I stick to these little guidelines and it works like a charm for me. Nice, clean HTML markup with the occasional @ character. Very easy to maintain (well at least the Views are!).

EDIT: Please note that all of these points are included in ASP.NET MVC 3 and are all 'best practices' are far as Microsoft are concerned. There is no need to install any extra frameworks, plugins or addons to stick to these guidelines.

汐鸠 2024-12-30 08:39:23

MVC 受到关注是有原因的。虽然视图中确实添加了一些标签,但如果逻辑在其所属的控制器中正确处理,那么它会更清晰。

了解 MVC 的真正含义也很重要: http://en.wikipedia.org/wiki /模型-视图-控制器。您从中获得的优点是更干净的分离和易于替换。

考虑一下这一点。您有一位客户希望您编写一个同时支持传统浏览器和移动浏览器的应用程序。使用 MVC 模式,让控制器检测平台并更改呈现的视图非常容易。如果操作正确,将一个视图替换为另一个视图应该是一个非常简单的过程。

我有 7 年在 ASP.NET Forms 中编写应用程序的经验。一旦我切换到 MVC 并开始理解 MVC,我就意识到我再也不会回去了。视图更清晰,调试更简单,逻辑更明显。我编写的最后一个应用程序使用 MVC 和 jQuery,每天有 3000 个用户,并且已成为我们现在编写的所有网站的模型网站。

我们的客户要求我们为我们的网站添加移动支持。因为我们在实现中选择了MVC,所以我们花了1周的时间才添加对移动设备的全面支持。如果我们在 ASP.NET 表单中完成此操作,我们不可能如此快速地完成此操作并如此有效地利用代码。

虽然示例代码来自 http://www. codinghorror.com/blog/2008/07/web-development-as-tag-soup.html 看起来确实很糟糕,你看过 ASP.NET GridView 吗? HTML 太糟糕了。您提供的示例还显示有人可以做一些工作来清理他们的观点。以下是 gridview 与带有 Razor 的 MVC 的比较:

GridView:

<asp:datagrid id="voucherGrid" runat="server" CssClass="dg" CellPadding="2" AutoGenerateColumns="False" DataKeyField="cx_nbr" 
        Width="800px" AllowPaging="True" AllowSorting="True" PageSize="20" OnPageIndexChanged="voucherGrid_PageIndexChanged" 
        OnSortCommand="voucherGrid_SortCommand" OnItemDataBound="voucherGrid_ItemDataBound">
        <SelectedItemStyle CssClass="dgSelectItem"></SelectedItemStyle>
        <AlternatingItemStyle CssClass="dgAlternateItem"></AlternatingItemStyle>
        <ItemStyle CssClass="dgNormalItem"></ItemStyle>
        <HeaderStyle ForeColor="White" CssClass="dgHeader"></HeaderStyle>

        <Columns>
            <asp:TemplateColumn HeaderText="Image">
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
                <ItemTemplate>
                    <asp:HyperLink id="voucherImageLink" Target="_blank" runat="server">Image</asp:HyperLink>                                               
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:BoundColumn DataField="cx_voucher_nbr" SortExpression="cx_voucher_nbr" HeaderText="Call #">
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_pkup_datetime" SortExpression="cx_pkup_datetime" HeaderText="Date" DataFormatString="{0:MM/dd/yyyy}">
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_pass_name" SortExpression="cx_pass_name" HeaderText="Passenger">
                <ItemStyle Wrap="False"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_pkup_address" SortExpression="cx_pkup_address" HeaderText="Pick-Up">
                <ItemStyle Wrap="False"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_dest_address" SortExpression="cx_dest_address" HeaderText="Destination">
                <ItemStyle Wrap="False"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_trip_miles" SortExpression="cx_trip_miles" HeaderText="Miles" DataFormatString="{0:N2}">
                <ItemStyle HorizontalAlign="Right"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_pkup_datetime" SortExpression="cx_pkup_datetime" HeaderText="Time" DataFormatString="{0:t}">
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_vch_wait_time_amt" SortExpression="cx_vch_wait_time_amt" HeaderText="Wait" DataFormatString="{0:C}">
                <ItemStyle HorizontalAlign="Right"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_vch_other_amt" SortExpression="cx_vch_other_amt" HeaderText="Other" DataFormatString="{0:C}">
                <ItemStyle HorizontalAlign="Right"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_vch_admin_charge_amt" SortExpression="cx_vch_admin_charge_amt" HeaderText="Admin Charge" DataFormatString="{0:C}">
                <ItemStyle HorizontalAlign="Right"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_vch_fare_amt" SortExpression="cx_vch_fare_amt" HeaderText="Rate" DataFormatString="{0:C}">
                <ItemStyle HorizontalAlign="Right"></ItemStyle>
            </asp:BoundColumn>
        </Columns>

        <PagerStyle ForeColor="White" CssClass="dgPager" Mode="NumericPages"></PagerStyle>
    </asp:datagrid>

Razor:

<table id="voucherGrid" class="dg" style="width: 800px;">
   <th class="dgHeader">
      <td>Image</td>
      <td>Call #</td>
      <td>Date</td>
      <td>Passenger</td>
      <td>Pick-Up</td>
      <td>Destination</td>
      <td>Miles</td>
      <td>Time</td>
      <td>Wait</td>
      <td>Other</td>
      <td>Admin Charge</td>
      <td>Rate</td>
   </th>
   @foreach(var voucher in Model.Vouchers) {
   <tr>
      <td>@voucher.Image</td>
      <td>@voucher.CallNum</td>
      <td>@voucher.Date</td>
      <td>@voucher.Passenger</td>
      <td>@voucher.PickUp</td>
      <td>@voucher.Destination</td>
      <td>@voucher.Miles</td>
      <td>@voucher.Time</td>
      <td>@voucher.Wait</td>
      <td>@voucher.Other</td>
      <td>@voucher.AdminCharge</td>
      <td>@voucher.Rate</td>
   </tr>
   }
</table>

你告诉我哪个看起来更容易理解?对我来说,处理 html 标签和一些额外的 @ 或 <% 标签要干净得多。

您还提到不需要进行单元测试。我会重新考虑这个想法。单元测试对于在生产站点上发生问题之前发现问题非常有用。

There is a reason why MVC is gaining traction. While it's true that there are some tags added to the view, it is by far cleaner if the logic is correctly handled in the controller where it should belong.

It's also important to understand what MVC really is: http://en.wikipedia.org/wiki/Model-view-controller. The advantages that you get from it are cleaner separation and easy substitution.

Consider this. You have a customer that wants you to write an application that supports both traditional browsers and mobile browsers. With the MVC pattern, it's really easy to have the controller detect the platform and change the view that is rendered. If done correctly, swapping out one view for another should be a very simple process.

I have 7 years of experience writing application in ASP.NET Forms. Once I switched to MVC and begun to understand MVC, I realized I would never go back. The views are cleaner, debugging is simpler and the logic is more obvious. The last application I wrote uses MVC and jQuery, has 3000 users a day and has become the model site for which all our sites are now written.

Our customer asked us to add mobile support to our site. Because we choose MVC in our implementation, it took us 1 week to add full support to mobile devices. There is no way we could have done it that quickly and utilize the code so efficiently had we done it in ASP.NET Forms.

While the sample code from http://www.codinghorror.com/blog/2008/07/web-development-as-tag-soup.html does look horrible, have you ever looked at an ASP.NET GridView? The HTML is atrocious. The sample you provided also shows someone who could do some work to clean up their views. Here is a comparison of a gridview vs MVC with Razor:

GridView:

<asp:datagrid id="voucherGrid" runat="server" CssClass="dg" CellPadding="2" AutoGenerateColumns="False" DataKeyField="cx_nbr" 
        Width="800px" AllowPaging="True" AllowSorting="True" PageSize="20" OnPageIndexChanged="voucherGrid_PageIndexChanged" 
        OnSortCommand="voucherGrid_SortCommand" OnItemDataBound="voucherGrid_ItemDataBound">
        <SelectedItemStyle CssClass="dgSelectItem"></SelectedItemStyle>
        <AlternatingItemStyle CssClass="dgAlternateItem"></AlternatingItemStyle>
        <ItemStyle CssClass="dgNormalItem"></ItemStyle>
        <HeaderStyle ForeColor="White" CssClass="dgHeader"></HeaderStyle>

        <Columns>
            <asp:TemplateColumn HeaderText="Image">
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
                <ItemTemplate>
                    <asp:HyperLink id="voucherImageLink" Target="_blank" runat="server">Image</asp:HyperLink>                                               
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:BoundColumn DataField="cx_voucher_nbr" SortExpression="cx_voucher_nbr" HeaderText="Call #">
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_pkup_datetime" SortExpression="cx_pkup_datetime" HeaderText="Date" DataFormatString="{0:MM/dd/yyyy}">
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_pass_name" SortExpression="cx_pass_name" HeaderText="Passenger">
                <ItemStyle Wrap="False"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_pkup_address" SortExpression="cx_pkup_address" HeaderText="Pick-Up">
                <ItemStyle Wrap="False"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_dest_address" SortExpression="cx_dest_address" HeaderText="Destination">
                <ItemStyle Wrap="False"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_trip_miles" SortExpression="cx_trip_miles" HeaderText="Miles" DataFormatString="{0:N2}">
                <ItemStyle HorizontalAlign="Right"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_pkup_datetime" SortExpression="cx_pkup_datetime" HeaderText="Time" DataFormatString="{0:t}">
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_vch_wait_time_amt" SortExpression="cx_vch_wait_time_amt" HeaderText="Wait" DataFormatString="{0:C}">
                <ItemStyle HorizontalAlign="Right"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_vch_other_amt" SortExpression="cx_vch_other_amt" HeaderText="Other" DataFormatString="{0:C}">
                <ItemStyle HorizontalAlign="Right"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_vch_admin_charge_amt" SortExpression="cx_vch_admin_charge_amt" HeaderText="Admin Charge" DataFormatString="{0:C}">
                <ItemStyle HorizontalAlign="Right"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn DataField="cx_vch_fare_amt" SortExpression="cx_vch_fare_amt" HeaderText="Rate" DataFormatString="{0:C}">
                <ItemStyle HorizontalAlign="Right"></ItemStyle>
            </asp:BoundColumn>
        </Columns>

        <PagerStyle ForeColor="White" CssClass="dgPager" Mode="NumericPages"></PagerStyle>
    </asp:datagrid>

Razor:

<table id="voucherGrid" class="dg" style="width: 800px;">
   <th class="dgHeader">
      <td>Image</td>
      <td>Call #</td>
      <td>Date</td>
      <td>Passenger</td>
      <td>Pick-Up</td>
      <td>Destination</td>
      <td>Miles</td>
      <td>Time</td>
      <td>Wait</td>
      <td>Other</td>
      <td>Admin Charge</td>
      <td>Rate</td>
   </th>
   @foreach(var voucher in Model.Vouchers) {
   <tr>
      <td>@voucher.Image</td>
      <td>@voucher.CallNum</td>
      <td>@voucher.Date</td>
      <td>@voucher.Passenger</td>
      <td>@voucher.PickUp</td>
      <td>@voucher.Destination</td>
      <td>@voucher.Miles</td>
      <td>@voucher.Time</td>
      <td>@voucher.Wait</td>
      <td>@voucher.Other</td>
      <td>@voucher.AdminCharge</td>
      <td>@voucher.Rate</td>
   </tr>
   }
</table>

You tell me which looks easier to understand? To me it's a lot cleaner to deal with html tags and a few extra @ or <% tags.

You also mentioned there being no need for unit testing. I would reconsider that thought. Unit testing can be tremendously useful in finding issues before they happen on a production site.

不回头走下去 2024-12-30 08:39:23

那里又回来了。一次又一次。原始 HTML。梦衣者。 ASP。微软Word。 ASP.NET、MVC.NET。我们似乎能够触及光谱的两端,但中间没有最佳位置。

归根结底,我们最希望说的是“好吧,至少所有的疯狂都被隔离在视野中”。我说“希望”是因为根据我的经验,说“面向对象”比用 OO 所暗示的所有软件原则优点来实现面向对象要容易得多。

从长远来看,标签汤不是问题。这绝对不是 MVC 上下文中的问题。问题在于令人震惊的编码。 MVC 部分像连体三胞胎一样融合在一起,清楚地表明软件设计和编码原则方面的无能,这是迄今为止更大的犯罪。

在 MVC 范式中编码时,像 Ruby on Rails 或 MVC.NET 这样的 MVC 框架将有助于提高编码效率;它本身并不会让你编码得更快。如果您不知道自己在做什么,那么它肯定不会阻止代码维护的噩梦。

There and back again. Again and again. Raw HTML. Dreamwearver. ASP. MS Word. ASP.NET, MVC.NET. We seem to be able to hit either end of the spectrum but there is no sweet spot in the middle.

In the final analysis perhaps the most we can hope to say is "well, at least all the madness is isolated in the view." I say "hope" because from my experience it's a lot easier to say "object oriented" than to do object oriented with all the software principled goodness that OO implies.

In the long run tag soup is not the problem. It absolutely is not the problem in the context of MVC. Appalling coding is the problem. MVC parts, fused together like Siamese triplets, clearly demonstrating incompetence in software design and coding principles is by far the greater crime.

An MVC framework like Ruby on Rails or MVC.NET will help coding efficiency when coding in the MVC paradigm; it won't make you code faster per se. And it certainly will not prevent a code maintenance nightmare if you don't know what the hell you're doing.

你的呼吸 2024-12-30 08:39:23
  1. 使用razr视图引擎(我的偏好,我认为它看起来更整洁)。 response.write "" 很容易编写。但是您是否在仅控制视图并且可以轻松交换或更改而无需触及任何服务逻辑的文件/类中执行此操作?这让我想到了#2。

  2. 与您的团队讨论关注点分离并决定每个逻辑片段的位置。依赖接触点在哪里,您的所有逻辑是否都集中在 DLL 中,而对视图/控制器一无所知?从一开始就决定并把它写在某个地方。
  3. 将观点保留为观点!
  4. 不要在视图中执行任何操作!
  5. 一旦您到达视图,您就只是在观看!
  6. 我有没有提到过观点就是这样?

我不会嫉妒任何人使用 MVP 或任何其他范例。但是,如果您想尝试 MVC,请正确执行,您会发现重构和维护代码变得更加容易。

我的2分钱

  1. Use razr view engine (My preference, i think it looks neater). response.write "<table>" is easy enough to write. But are you doing that in a file/class that ONLY controls the view and could be easily swapped or changed without touching any service logic? That brings me to #2.
  2. Discuss seperation of concerns with your group and decide where each piece of logic lives. Where are the dependency touchpoints, is all your logic grouped up in DLL which have no knowledge of views/controllers? Decide from the beggining and write it down somehwhere.
  3. Keep views as views!
  4. Don't do anything in a view!
  5. Once you're at the view you are just viewing!
  6. Have I mentioned that views are just that?

I don't begrudge anyone for using MVP or any other paradigm. But if you want to try out MVC, do it right and you will find that refactoring and maintaining your code to be much easier.

my 2cents

小兔几 2024-12-30 08:39:23

视图层相对于标签汤的优点在于,视图层应该将不良逻辑与不良数据隔离开来,从而更容易追踪根本原因。这不会自动发生,它必须被纳入视图的代码约束中。我见过的关于使用 MVC 的 HTML 视图的最佳方法如下(来自 Tony Marston ):

  • 将数据转换为HTML的代码是显示逻辑。
  • 创建或获取随后转换为 HTML 的数据的代码不是显示逻辑。

了解是成功的一半,一致的执行是另一半。能力越大,责任越大,因此使用有限的命令子集有助于强制执行更简洁的代码。最少的声明将被打印并包括在内。用于生成表、列表或表单的循环和数据绑定可以由 JavaScript 库或 XSLT 处理。变量赋值、条件逻辑和字符串操作可以在本地或全局包含中完成。其他任何事情都可以由模型或控制器处理。

The advantage of a view layer over tag soup is that the view layer should isolate bad logic from bad data, making the root cause simpler to track down. This will not happen automatically, it has to be baked into the code constraints of the view. The best approach I've seen regarding HTML views using MVC is the following (from Tony Marston):

  • Code which transforms data into HTML is display logic.
  • Code which creates or obtains the data which is subsequently transformed into HTML is not display logic.

Knowing is half the battle, consistent implementation is the other. With great power comes great responsibility, so using a limited subset of commands helps enforce cleaner code. The minimal statements would be print and include. Looping and data binding to generate tables, lists, or forms can be handled by JavaScript libraries or XSLT. Variable assignment, conditional logic, and string manipulation can be done in a local or global include. Anything else can be handled by the model or controller.

娇纵 2024-12-30 08:39:23

不要在模板代码中进行任何计算。

看看 Django 中允许的内容: https://docs.djangoproject.com/ en/1.3/topics/templates/

没有算术。不向方法传递参数。没有任何类型的定义(循环除外)。这迫使您在视图方法中执行几乎所有操作,并传入所需的任何对象和列表,从而保持干净。

Don't do any calculations in your template code.

Take a look at what is allowed in Django: https://docs.djangoproject.com/en/1.3/topics/templates/

No arithmetic. No passing parameters to methods. No definitions of any sort (other than in loops). This forces you to do almost everything in the view methods, and pass in any objects and lists required, which keeps it clean.

梦幻的味道 2024-12-30 08:39:23

您可以使用 GWT、ZK、Vaadin、JSF 2 或隐藏 HTML 的东西。我不知道你说的MVC是什么意思。 Django/RoR/CakePHP 模型-视图-呈现器模式有时称为 MVC 或真正的 MVC。如果您坚持使用 MVC,您的视图代码中应该只有数据绑定和事件触发器。

我认为这是一个设计问题而不是技术问题。

You can use GWT, ZK, Vaadin, JSF 2 or something that hides HTML. I don't know what you mean by MVC. Django/RoR/CakePHP Model-View-Presenter pattern sometimes called MVC or real MVC. If you stick to MVC you should have ONLY data binding and event triggers in your view code.

I think it is a design problem not technology.

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