asp.net - 我如何避免在ASP.NET MVC View 中无法维护的代码

标签 asp.net asp.net-mvc views

我发现越来越多的我的asp.net mvc View 开始看起来像我那老旧的asp代码,我永远无法使用集成的<%%>和html来保持或保持整洁。下面我有我所谈论的代码的牺牲品。是否有任何最佳实践或推荐的方法来避免这种情况,并保持 View 更具可维护性和可读性。

            <td>
                <%
                    bool userRequiresApproval = false;
                    if (!string.IsNullOrEmpty(item.loginName))
                    {
                        MembershipUserCollection membership = (MembershipUserCollection)ViewData["UnapprovedUsers"];
                        if (membership != null && membership[item.loginName] != null)
                        {
                            userRequiresApproval = true;
                        }
                    }
                    bool isLoggedInUserAdmin = false;
                    if (ViewData.ContainsKey("isAdmin"))
                    {
                        isLoggedInUserAdmin = (bool)ViewData["isAdmin"];
                    }
                    if (isLoggedInUserAdmin && userRequiresApproval)
                    {%>
                        <%= Html.ActionLink("View", "Details", new { id=item.Mail_ID })%>, <%= Html.ActionLink("Delete", "GotoDeleteConfirmPage", new { id = item.Mail_ID })%>, <%= Html.ActionLink("Approve", "Approve", new { id = item.Mail_ID })%>
                    <%}
                    else if (isLoggedInUserAdmin)
                    {%>
                        <%= Html.ActionLink("View", "Details", new { id = item.Mail_ID })%>, <%= Html.ActionLink("Delete", "GotoDeleteConfirmPage", new { id = item.Mail_ID })%>
                    <%}
                    else
                    {%>
                        <%= Html.ActionLink("View", "Details", new { id = item.Mail_ID })%>
                    <%}%>
        </tr>
        <% } %>

最佳答案

我将首先讨论具体问题,然后是抽象问题:

具体的

一种可能的方法是创建一套Html帮助程序或用户控件,它们具有一些基本逻辑来确定它们是否应可见。例如,您的用法可能是:

<td>
    Html.LinkList(", "
        ActionLinks.ViewDetails(item),
        ActionLinks.DeleteAndConfirm(item),
        ActionLinks.Approve(item))
</td>

每个操作都包含自己的逻辑来确定是否应使用该操作(例如,“我需要管理员权限”),如果该操作确定不满足其自身条件,则只需返回string.Empty即可:
class ActionLinks
{
    public static string Approve(Item item)
    {
        if(ItemRequiresApproval(item) && CurrentUserIsAdmin())
        {
            return Html.ActionLink("Approve", "Approve", new { id = item.Mail_ID });
        }
        else
        {
            return string.Empty;
        }
    }

    private static bool ItemRequiresApproval(Item item)
    {
        //determine whether item requires approval
        //this could be further broken into a separate utilities class
    }

    private static bool CurrentUserIsAdmin()
    {
        //this should definitely go in a separate class dedicated to
        //handling membership and authorization
        //as well as figuring out who the current user is
    }
}

LinkList看起来像这样:
string LinkList(string delimiter, params string[] links)
{
    StringBuilder sb = new StringBuilder();
    foreach(string link in links)
    {
        if(!string.IsNullOrEmpty(link))
        {
            sb.Append(delimiter);
            sb.Append(link);
        }
    }
    return sb.ToString().Substring(delimiter.Length);
}

抽象的

解决您的问题的方法在于记住SRP (Single Responsibility Principle)SOC (Separation of Concerns)。在当前示例中,您的View是一个类。您已经使该类不仅负责标记的整体结构,而且负责几乎整个应用程序的每一分细节!您的 View 不应了解或关心管理员权限或批准。只有批准按钮才应该知道批准。只有特定于管理员的元素才应该了解管理员权限。如果您发现自己重复进行某些类型的检查(例如,“如果admin显示x,否则显示y”),则创建一些通用包装程序(例如AdminPanel),将其适本地打开或关闭。对于所有其他参与者,给定元素仅仅是或不是-做出决定的是该元素的责任。

关于asp.net - 我如何避免在ASP.NET MVC View 中无法维护的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1236739/

相关文章:

asp.net-mvc - 相对于当前 Controller 的 MVC Ajax 操作

甲骨文 : Materialized view not working when Using LEFT JOIN

asp.net-mvc - ASP.NET MVC View 中允许多少逻辑?

c# - 来自 ASP.Net Web API 2 的统一、一致的错误响应

asp.net - 无法在 Web 应用程序 VS2012 中找到全局 asax 文件

asp.net - 为什么这个清晰可见的符号无法解析?

c# - 如何使用 ScriptManager.GetStateString()?

c# - ASP.NET web.api 帮助页面不显示任何描述

asp.net-mvc - 识别网络爬虫

views - CouchDB View : remove duplicates *and* order by time