MyORM的使用(三)

在Web项目中MS提供了ObjectDataSource,利用它配合MyOrm可以方便的实现分页、排序等。

简单说一下ObjectDataSource,这个东东感觉多少有点变扭。首先它需要指定一个BusinessObject,通过给Select、Update、Insert等操作指定BusinessObject中对应的方法,页面的操作通过反射调用对应的方法。这里MS提供了DataObjectAttribute和DataObjectMethodAttribute来标识BusinessObject以及其中的方法对应哪个操作。其实我倒觉得不如给BusinessObject定义一个接口来的省事,省得在界面中设置一堆参数。另外一个,BusinessObject如果不是静态类,ObjectDataSource每次都会创建一个对象,而不能指定某个现有的BusinessObject变量。这导致了像NHibernate、Spring之类底层的框架完全不可用,另外每次创建对象对效率也不利。

为了解决这个问题,需要专门为ObjectDataSource定义新的BusinessObject,在ObjectDataSource和底层业务之间搭一座桥。

在MyOrm的例子中,定义了ObjectSource<T>:

  1. [DataObject]
  2.     public class ObjectSource<T>
  3.     {
  4.         private IObjectViewDAO<T> objectViewDAO;
  5.         public IObjectViewDAO<T> ObjectViewDAO
  6.         {
  7.             get
  8.             {
  9.                 if (objectViewDAO == null) objectViewDAO = (IObjectViewDAO<T>)NorthwindFactory.GetObjectViewDAO(typeof(T));
  10.                 return objectViewDAO;
  11.             }
  12.         }
  13.         [DataObjectMethod(DataObjectMethodType.Select, true)]
  14.         public List<T> Select(Condition condition)
  15.         {
  16.             return ObjectViewDAO.Search(condition);
  17.         }
  18.         [DataObjectMethod(DataObjectMethodType.Select, false)]
  19.         public List<T> Select(Condition condition, int startRowIndex, int maximumRows)
  20.         {
  21.             return ObjectViewDAO.SearchSection(condition, startRowIndex, maximumRows, nullfalse);
  22.         }
  23.         [DataObjectMethod(DataObjectMethodType.Select, false)]
  24.         public List<T> Select(Condition condition, int startRowIndex, int maximumRows, string orderBy)
  25.         {
  26.             bool desc = false;
  27.             if (!String.IsNullOrEmpty(orderBy))
  28.             {
  29.                 string[] args = orderBy.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  30.                 orderBy = args[0];
  31.                 desc = args.Length > 1 && String.Compare(args[1], "desc"true) == 0;
  32.             }
  33.             return ObjectViewDAO.SearchSection(condition, startRowIndex, maximumRows, orderBy, desc);
  34.         }
  35.         public int Count(Condition condition)
  36.         {
  37.             return ObjectViewDAO.Count(condition);
  38.         }
  39.     }

ObjectSource的工作很简单,就是把ObjectDataSource的输入转化为底层可接受的输入,而且创建ObjectSource的开销也很小。这里ObjectSource只定义了查询,没有增删改操作,如果需要可以自己加下。

ObjectSource<T>是泛型,并不能直接被ObjectDataSource使用,还需要为每个实体类定义对应的ObjectSource。

  1.     public class CategoriesSource : ObjectSource<Categories> { }
  2.     public class CustomerCustomerDemoSource : ObjectSource<CustomerCustomerDemo> { }
  3.     public class CustomerDemographicsSource : ObjectSource<CustomerDemographics> { }
  4.     public class CustomersSource : ObjectSource<Customers> { }
  5.     public class EmployeesSource : ObjectSource<Employees> { }
  6.     public class EmployeeTerritoriesSource : ObjectSource<EmployeeTerritories> { }
  7.     public class OrderDetailsSource : ObjectSource<OrderDetails> { }
  8.     public class OrdersSource : ObjectSource<Orders> { }
  9.     public class ProductsSource : ObjectSource<Products> { }
  10.     public class RegionSource : ObjectSource<Region> { }
  11.     public class ShippersSource : ObjectSource<Shippers> { }
  12.     public class SuppliersSource : ObjectSource<Suppliers> { }
  13.     public class TerritoriesSource : ObjectSource<Territories> { }
  14.     public class CustomerCustomerDemoViewSource : ObjectSource<CustomerCustomerDemoView> { }
  15.     public class EmployeesViewSource : ObjectSource<EmployeesView> { }
  16.     public class EmployeeTerritoriesViewSource : ObjectSource<EmployeeTerritoriesView> { }
  17.     public class OrderDetailsViewSource : ObjectSource<OrderDetailsView> { }
  18.     public class OrdersViewSource : ObjectSource<OrdersView> { }
  19.     public class ProductsViewSource : ObjectSource<ProductsView> { }
  20.     public class TerritoriesViewSource : ObjectSource<TerritoriesView> { }

现在可以在页面中很方便的使用ObjectDataSource了。

例如需要创建一个可排序可分页的GridView:

首先建一个GridView;

然后给GridView选择数据源,选择添加新数据源;

在向导中选择创建Object类型的数据源,在BusinessObject选择界面中会出现已经定义好的ObjectSource(ObjectSource定义后需要重新编译一下才会出现),选择你所需要的。

然后next、next最后finish就可以了。

现在GridView已经可以显示内容了,但是排序、分页还没实现。还需要多2个步骤:

设置ObjectDataSource的EnablePaging为true,SelectCountMethod为Count,SortParameterName为orderBy(ObjectSource中的Select方法的对应参数名,MaximumRowsParameterName和StartRowIndexParameterName的默认值和ObjectSource中的参数名一致,就不需要设置了);

最后把GridView的AllowPaging和AllowSorting设为true就可以了。

 

下面是生成的aspx页面,后台aspx.cs不需要添加代码。

  1. <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
  2.             AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
  3.             <Columns>
  4.                 <asp:BoundField DataField="Category_CategoryName" HeaderText="Category_CategoryName"
  5.                     SortExpression="Category_CategoryName" />
  6.                 <asp:BoundField DataField="Category_Description" HeaderText="Category_Description"
  7.                     SortExpression="Category_Description" />
  8.                 <asp:BoundField DataField="Supplier_CompanyName" HeaderText="Supplier_CompanyName"
  9.                     SortExpression="Supplier_CompanyName" />
  10.                 <asp:BoundField DataField="Supplier_ContactName" HeaderText="Supplier_ContactName"
  11.                     SortExpression="Supplier_ContactName" />
  12.                 <asp:BoundField DataField="Supplier_ContactTitle" HeaderText="Supplier_ContactTitle"
  13.                     SortExpression="Supplier_ContactTitle" />
  14.                 <asp:BoundField DataField="Supplier_Address" HeaderText="Supplier_Address" SortExpression="Supplier_Address" />
  15.                 <asp:BoundField DataField="Supplier_City" HeaderText="Supplier_City" SortExpression="Supplier_City" />
  16.                 <asp:BoundField DataField="Supplier_Region" HeaderText="Supplier_Region" SortExpression="Supplier_Region" />
  17.                 <asp:BoundField DataField="Supplier_PostalCode" HeaderText="Supplier_PostalCode"
  18.                     SortExpression="Supplier_PostalCode" />
  19.                 <asp:BoundField DataField="Supplier_Country" HeaderText="Supplier_Country" SortExpression="Supplier_Country" />
  20.                 <asp:BoundField DataField="Supplier_Phone" HeaderText="Supplier_Phone" SortExpression="Supplier_Phone" />
  21.                 <asp:BoundField DataField="Supplier_Fax" HeaderText="Supplier_Fax" SortExpression="Supplier_Fax" />
  22.                 <asp:BoundField DataField="Supplier_HomePage" HeaderText="Supplier_HomePage" SortExpression="Supplier_HomePage" />
  23.                 <asp:BoundField DataField="ProductID" HeaderText="ProductID" SortExpression="ProductID" />
  24.                 <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
  25.                 <asp:BoundField DataField="SupplierID" HeaderText="SupplierID" SortExpression="SupplierID" />
  26.                 <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
  27.                 <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit" />
  28.                 <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
  29.                 <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
  30.                 <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" SortExpression="UnitsOnOrder" />
  31.                 <asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" SortExpression="ReorderLevel" />
  32.                 <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" />
  33.             </Columns>
  34.         </asp:GridView>
  35.         <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" EnablePaging="True" OldValuesParameterFormatString="original_{0}"
  36.             SelectCountMethod="Count" SelectMethod="Select" SortParameterName="orderBy" TypeName="Northwind.ProductsViewSource">
  37.             <SelectParameters>
  38.                 <asp:Parameter Name="condition" Type="Object" />
  39.             </SelectParameters>
  40.         </asp:ObjectDataSource>

如果ObjectDataSource能设计的再好些的话整个步骤会更方便。目前为止Select方法的condition参数都是为空的,也就是说只是查询所有的内容。如果需要使用查询条件的,可以有3个方法:

1、自定义查询条件控件,并提供一个类型为Condition的属性,然后把ObjectDataSource的SelectParameters中的condition设为ControlParameter,并指定ControlID为自定义的查询条件控件。

2、在ObjectDataSource的Selecting事件中,通过程序给e.InputParameters["condition"]赋值。

3、自定义包含Condition的Parameter,关于自定义Parameter可以参考msdn:Parameter Class

 

示例代码可以到CodePlex下载,包含在Samples解决方案里。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值