这一章节主要介绍通过NClay框架的Asp.net MVC功能实现数据查询,分页和排序.通过业务接口的制定、接口处理和视图三个主要部分来了解NClay.MVC的处理层次和使用规则。
功能概述:
逻辑分析
这个查询需求主要包括以下几个逻辑:
所有雇员工查询
public interface IEmployeesAll
{
IList<Employees> Employees
{
get;
set;
}
}
所有客户查询
public interface ICustomersAll
{
IList<Customers> Customers
{
get;
set;
}
}
订单查询
public interface IOrdersList:NClay.IDataPageProperty
{
int EmployeeID
{
get;
set;
}
string CustomerID
{
get;
set;
}
IList<Orders> Orders
{
get;
set;
}
DateTime OrderDateFrom
{
get;
set;
}
DateTime OrderDateTo
{
get;
set;
}
}
逻辑处理
[Controller]
public class Handler
{
public void EmployeesAll(IEmployeesAll logic)
{
Expression exp &#61; new Expression();
logic.Employees &#61; exp.List<Employees>();
}
public void CustomersAll(ICustomersAll logic)
{
Expression exp &#61; new Expression();
logic.Customers &#61; exp.List<Customers>();
}
public void OrderList(IOrdersList logic)
{
Expression exp &#61; new Expression();
if (!Common.IsEmpty(logic.CustomerID))
{
exp &&#61; DB.Orders.CustomerID &#61;&#61; logic.CustomerID;
}
if (logic.EmployeeID > 0)
{
exp &&#61; DB.Orders.EmployeeID &#61;&#61; logic.EmployeeID;
}
if (logic.OrderDateFrom !&#61; DateTime.MinValue)
exp &&#61; DB.Orders.OrderDate >&#61; logic.OrderDateFrom;
if (logic.OrderDateTo !&#61; DateTime.MinValue)
exp &&#61; DB.Orders.OrderDate <&#61; logic.OrderDateTo;
logic.DataPage.PageSize &#61; 10;
logic.Orders &#61; exp.List<Orders>(logic);
}
}
通过框架的Controller属性标记接受框架托管处理。到这里所有逻辑处理的代码已经编写完成。
视图信息对象提供描述
[All(typeof(IEmployeesAll),typeof(ICustomersAll),typeof(IOrdersList))]
public class OrderList : IEmployeesAll, ICustomersAll, IOrdersList,NClay.Web.IDataPageParamUrl
{
#region IOrdersList 成员
public int EmployeeID
{ get; set; }
public string CustomerID
{ get; set; }
public IList<Orders> Orders
{ get; set; }
public DateTime OrderDateFrom
{ get; set; }
public DateTime OrderDateTo
{ get; set; }
#endregion
#region IDataPageProperty 成员
[Bind(typeof(NClay.DataPage))]
public IDataPage DataPage
{ get; set; }
#endregion
#region ICustomersAll 成员
public IList<Customers> Customers
{ get; set; }
#endregion
#region IEmployeesAll 成员
public IList<Employees> Employees
{ get; set; }
#endregion
#region IDataPageParamUrl 成员
public string GetParamUrl()
{
string param &#61; "EmployeeID&#61;{0}&CustomerID&#61;{1}&OrderDateFrom&#61;{2}&OrderDateTo&#61;{3}";
return string.Format(param, EmployeeID, CustomerID, OrderDateFrom.ToShortDateString(), OrderDateTo.ToShortDateString());
}
#endregion
}
视图的输
<table cellpadding&#61;"1" cellspacing&#61;"1">
<tr>
<td class&#61;"_GridColumn"><a href&#61;"<%WriterDataPageInfo("CustomerID"); %>">CustomerIDa>td>
<td class&#61;"_GridColumn"><a href&#61;"<%WriterDataPageInfo("EmployeeID"); %>">EmployeeIDa>td>
<td class&#61;"_GridColumn">Freighttd>
<td class&#61;"_GridColumn"><a href&#61;"<%WriterDataPageInfo("OrderDate"); %>">OrderDatea>td>
<td class&#61;"_GridColumn">OrderIDtd>
<td class&#61;"_GridColumn">RequiredDatetd>
<td class&#61;"_GridColumn">ShipAddresstd>
<td class&#61;"_GridColumn"><a href&#61;"<%WriterDataPageInfo("ShipCity"); %>">ShipCitya>td>
<td class&#61;"_GridColumn">ShipCountrytd>
<td class&#61;"_GridColumn">ShipNametd>
<td class&#61;"_GridColumn">ShippedDatetd>
<td class&#61;"_GridColumn">ShipPostalCodetd>
<td class&#61;"_GridColumn">ShipRegiontd>
<td class&#61;"_GridColumn">ShipViatd>
tr>
<%
foreach(NorthWind.Entities.Orders item in orderlist.Orders)
{%>
<tr>
<td class&#61;"_GridItem"><%&#61;item.CustomerID%>td>
<td class&#61;"_GridItem"><%&#61;item.EmployeeID%>td>
<td class&#61;"_GridItem"><%&#61;item.Freight%>td>
<td class&#61;"_GridItem"><%&#61;item.OrderDate.ToShortDateString()%>td>
<td class&#61;"_GridItem"><%&#61;item.OrderID%>td>
<td class&#61;"_GridItem"><%&#61;item.RequiredDate.ToShortDateString()%>td>
<td class&#61;"_GridItem"><%&#61;item.ShipAddress%>td>
<td class&#61;"_GridItem"><%&#61;item.ShipCity%>td>
<td class&#61;"_GridItem"><%&#61;item.ShipCountry%>td>
<td class&#61;"_GridItem"><%&#61;item.ShipName%>td>
<td class&#61;"_GridItem"><%&#61;item.ShippedDate.ToShortDateString()%>td>
<td class&#61;"_GridItem"><%&#61;item.ShipPostalCode%>td>
<td class&#61;"_GridItem"><%&#61;item.ShipRegion%>td>
<td class&#61;"_GridItem"><%&#61;item.ShipVia%>td>
<%}%>
tr>
table>
下面最重要的一步是如何把视图和视图信息对象关联起&#xff0c;可以在当前WEB应用程序目录下创建vr.xml类型的文件然后在里面写上关系描述
xml version&#61;"1.0" encoding&#61;"utf-8" ?>
<viewrewriter>
<rewrite url&#61;"~/Orders.aspx" to&#61;"NClayCase.DataList.OrderList"/>
viewrewriter>
以是上是一个全匹配的关系映射&#xff0c;框架也支持正则匹配的映射描述。
到这里所有工作已经完成。
在线演示
下载代码程序