Asp.net Mvc 2 を使用している私の最近のプロジェクトでは、DisplayFor にパフォーマンスの問題があることがわかりました。それが本当の問題なのか、それとも何かを見逃したのかよくわかりません。
Asp.net Mvc Guru が説明してくれることを願っています。:)
モデル。
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string EmailAddress { get; set; }
public static IEnumerable<Customer> GetCustomers()
{
for (int i = 0; i < 1000; i++)
{
var cust = new Customer()
{
CustomerId = i + 1,
Name = "Name - " + (i + 1),
Address = "Somewhere in the Earth...",
EmailAddress = "customerABC"
};
yield return cust;
}
}
}
コントローラ
public ActionResult V1()
{
return View(Customer.GetCustomers());
}
public ActionResult V2()
{
return View(Customer.GetCustomers());
}
V1 (パフォーマンスの問題がある)
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Customer>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
V1
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>V1</h2>
<table>
<%foreach (var cust in this.Model)
{%>
<%= Html.DisplayFor(m => cust) %>
<%} %>
</table>
</asp:Content>
そしてテンプレートは
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Customer>" %>
<tr>
<td><%= this.Model.CustomerId %></td>
<td><%= this.Model.Name %></td>
<td><%= this.Model.Address %></td>
<td><%= this.Model.EmailAddress %></td>
</tr>
V2 (パフォーマンスの問題なし)
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Customer>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
V2
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>V2</h2>
<table>
<%foreach (var cust in this.Model)
{%>
<tr>
<td><%= cust.CustomerId%></td>
<td><%= cust.Name%></td>
<td><%= cust.Address%></td>
<td><%= cust.EmailAddress%></td>
</tr>
<%} %>
</table>
</asp:Content>
V1 と V2 のパフォーマンスの違いは簡単にわかります。
編集: ローカルの IIS 7 (リリース バージョン) にデプロイすると、(V1) が非常に高速になります。問題は解決しましたが、理由を知りたいです。:)
ありがとう、
ソエモエ