这一章节主要介绍通过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 = new Expression();

            logic.Employees = exp.List<Employees>();

        }

        public void CustomersAll(ICustomersAll logic)

        {

            Expression exp = new Expression();

            logic.Customers = exp.List<Customers>();

        }

        public void OrderList(IOrdersList logic)

        {

            Expression exp = new Expression();

            if (!Common.IsEmpty(logic.CustomerID))

            {

                exp &= DB.Orders.CustomerID == logic.CustomerID;

            }

            if (logic.EmployeeID > 0)

            {

                exp &= DB.Orders.EmployeeID == logic.EmployeeID;

            }

            if (logic.OrderDateFrom != DateTime.MinValue)

                exp &= DB.Orders.OrderDate >= logic.OrderDateFrom;

            if (logic.OrderDateTo != DateTime.MinValue)

                exp &= DB.Orders.OrderDate <= logic.OrderDateTo;

            logic.DataPage.PageSize = 10;

            logic.Orders = 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 = "EmployeeID={0}&CustomerID={1}&OrderDateFrom={2}&OrderDateTo={3}";

            return string.Format(param, EmployeeID, CustomerID, OrderDateFrom.ToShortDateString(), OrderDateTo.ToShortDateString());

        }

 

        #endregion

    }

视图的输

 <table cellpadding="1" cellspacing="1">

<tr>

                            <td class="_GridColumn"><a href="<%WriterDataPageInfo("CustomerID"); %>">CustomerID</a></td>

                                          <td class="_GridColumn"><a href="<%WriterDataPageInfo("EmployeeID"); %>">EmployeeID</a></td>

                                          <td class="_GridColumn">Freight</td>

                                          <td class="_GridColumn"><a href="<%WriterDataPageInfo("OrderDate"); %>">OrderDate</a></td>

                                          <td class="_GridColumn">OrderID</td>

                                          <td class="_GridColumn">RequiredDate</td>

                                          <td class="_GridColumn">ShipAddress</td>

                                          <td class="_GridColumn"><a href="<%WriterDataPageInfo("ShipCity"); %>">ShipCity</a></td>

                                          <td class="_GridColumn">ShipCountry</td>

                                          <td class="_GridColumn">ShipName</td>

                                          <td class="_GridColumn">ShippedDate</td>

                                          <td class="_GridColumn">ShipPostalCode</td>

                                          <td class="_GridColumn">ShipRegion</td>

                                          <td class="_GridColumn">ShipVia</td>

              </tr>

<%

   

    foreach(NorthWind.Entities.Orders item in orderlist.Orders)

        

{%>

<tr>

              <td class="_GridItem"><%=item.CustomerID%></td>

              <td class="_GridItem"><%=item.EmployeeID%></td>

              <td class="_GridItem"><%=item.Freight%></td>

              <td class="_GridItem"><%=item.OrderDate.ToShortDateString()%></td>

             <td class="_GridItem"><%=item.OrderID%></td>

              <td class="_GridItem"><%=item.RequiredDate.ToShortDateString()%></td>

              <td class="_GridItem"><%=item.ShipAddress%></td>

              <td class="_GridItem"><%=item.ShipCity%></td>

              <td class="_GridItem"><%=item.ShipCountry%></td>

              <td class="_GridItem"><%=item.ShipName%></td>

              <td class="_GridItem"><%=item.ShippedDate.ToShortDateString()%></td>

              <td class="_GridItem"><%=item.ShipPostalCode%></td>

              <td class="_GridItem"><%=item.ShipRegion%></td>

              <td class="_GridItem"><%=item.ShipVia%></td>

<%}%>

</tr>

</table>

下面最重要的一步是如何把视图和视图信息对象关联起,可以在当前WEB应用程序目录下创建vr.xml类型的文件然后在里面写上关系描述

<?xml version="1.0" encoding="utf-8" ?>

<viewrewriter>

  <rewrite url="~/Orders.aspx" to="NClayCase.DataList.OrderList"/>

 </viewrewriter>

以是上是一个全匹配的关系映射,框架也支持正则匹配的映射描述。

到这里所有工作已经完成。

在线演示

下载代码程序

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"