当前位置 : 主页 > 网络编程 > ASP >

我创造了有史以来最糟糕的标签汤(ASP.NET MVC 2)

来源:互联网 收集:自由互联 发布时间:2021-06-24
我盯着最糟糕的一塌糊涂,我的显示器不够高,看不出发生了什么,VS 2010根本没有帮助. 我不知道如何重构这个垃圾. 现在是上午11点,我觉得已经喝了一杯. 这只是我没有业务编码的最后证明
我盯着最糟糕的一塌糊涂,我的显示器不够高,看不出发生了什么,VS 2010根本没有帮助.

我不知道如何重构这个垃圾.

现在是上午11点,我觉得已经喝了一杯.

这只是我没有业务编码的最后证明吗?说实话.

<div id="followedFriends">
    <% if (Model.FollowedFriends.Count() > 0)
       {
           foreach (var friend in Model.FollowedFriends)
           { %>
    <div id="friendContainer">
        <div class="followedFriend">
            <div class="userName">
                <%= Html.ActionLink(friend.FoFriend.UserName, "Visitor", "Home", new {userID = friend.FoFriend.UserId}, null)%></div>
            Currently reading:
            <br />
            <div class="bookLinks">
                <% if (friend.BookCount != 0)
                   { %>
                <% if (friend.BookCount <= 5)
                   { %>
                <%= friend.BookLinks%>
                <%}
                   else
                   { %>
                <%:Html.ActionLink(friend.BookCount + " different books.", "Visitor", "Home", new {userID = friend.FoFriend.UserId}, null)%>
                <%}
                   }
                   else
                   { %>
                Nothing, it appears...
                <%}%>
            </div>
            <%if (friend.ReviewCount != 0)
              {%>
            New review for:
            <div class="reviewLinks">
                <%if (friend.ReviewCount <= 5)
                  { %>
                <%= friend.ReviewLinks%>
                <%}
                  else
                  {%>
                <%: friend.ReviewCount %>
                different books
                <%}%></div>
            <%}

              if (friend.QuoteCount != 0)
              {%>
            <span class="highlight">&#9656</span>
            <%: friend.QuoteCount%>
            new
            <%if (friend.QuoteCount != 1)
              { %>quotes
            <%}
              else
              { %>
            quote
            <%} %>
            <%}%>
        </div>
    </div>
    <%}
       }%>
</div>
<%} %>

更新

有人问,这是视图模型的相关部分:

public class FollowedFriend
    {
        public aspnet_User FoFriend { get; set; }
        public string BookLinks { get; set; }
        public int BookCount { get; set; }
        public string ReviewLinks { get; set; }
        public int ReviewCount { get; set; }
        public int QuoteCount { get; set; }

        public FollowedFriend(Guid userID, DateTime lastVisit)
        {
            using (var context = new BookNotesEntities())
            {
                FoFriend = context.aspnet_Users.SingleOrDefault(u => u.UserId == userID);
                var reading = context.Books.Where(b => b.UserID == userID && b.CurrentlyReading == true).ToList();
                BookCount = reading.Count;
                if (BookCount <= 5)
                    BookLinks = Book.ConvertBooksToLinks("Book/Details", reading);
                else
                    BookLinks = "";
                var recentBooks = context.Books.Where(b => b.UserID == userID
                    && b.Review.DateCreated >= lastVisit).OrderByDescending(b => b.DateCreated).ToList();
                if (recentBooks.Count <= 5)
                    ReviewLinks = Book.ConvertBooksToLinks("/Book/Details", recentBooks);
                else
                    ReviewLinks = "";
                ReviewCount = recentBooks.Count;
                QuoteCount = context.Quotes.Count(q => q.UserID == userID && q.DateCreated >= lastVisit);
            }
        }
    }
让我们从最初由Rob Conery提出的格言开始:如果在视图中某处嵌入了“if”,则可能表明您应该A.)创建HtmlHelper或B.)从视图中撤回逻辑将其插入ViewModel.

通过这样做,你可以清理你的View看起来像这样(你明白了):

<div id="followedFriends">
        <% foreach (var friend in Model.FollowedFriends) { %>
        <div id="friendContainer">
            <div class="followedFriend">
                <div class="userName">
                    <%: Html.ActionLink(friend.FoFriend.UserName, "Visitor", "Home", new {userID = friend.FoFriend.UserId}, null)%>
                </div>
                Currently reading:

                <br />
                <div class="bookLinks">
                    <%: Html.DisplayBooklinks(friend) %>
                </div>
                <div class="bookReviews">
                    <%: Html.DisplayBookReviews(friend) %>
                </div>
                <div class="bookQuotes">
                    <%: Html.DisplayQuotes(friend) %>
                </div>
            </div>
        </div>
        <% } %>
    </div>

此时,如果在某些其他页面中有可能消耗此UI,则应考虑将其放入用户控件中.通过这样做,您的视图现在看起来像这样:

<% Html.RenderPartial("FriendDetails", Model.FollowedFriends); %>

最终,只要你的View开始看起来像汤,那是因为你的观点做得太多了.反过来,这意味着:您的应用程序的某些其他层没有足够的思考.通过查看视图中的逻辑,并确定哪些抽象可能有助于您保持DRY,您的视图将变得更具可读性和更易于维护.

网友评论