实践ORM,创建基于Grove的.NET应用程序(三)

类别:.NET开发 点击:0 评论:0 推荐:

实践ORM,创建基于Grove.NET应用程序(三)

 

撰稿 林学鹏 [email protected]

 

       9) 在当前项目的Managers下添加CustomerManager类及AddressManager类,Managers层属于商业逻辑层,负责对实体数据库操作的再一次封装。

       在商业逻辑层,此处将类构造函数定义成私有,实现Singleton模式。

      

CustomerManager.cs

     using System;

     using Grove;

     using WebApp1.Entities;

     using WebApp1.EntityDB;

 

     public class CustomerManager

     {

         private CustomerManager()

         {

         }

         public static void AddNewCustomer(Customer c)

         {

              CustomerDB db=new CustomerDB();

              db.Insert(c);

         }

         public static void DeleteCustomer(Customer c)

         {

              CustomerDB db=new CustomerDB();

              db.Delete(c);

         }

         public static void UpdateCustomer(Customer c)

         {

              CustomerDB db=new CustomerDB();

              db.Update(c);

         }

         public static EntityData GetAllCustomers()

         {

              CustomerDB db=new CustomerDB();

              return db.SelectAll();

         }

     }

AddressManager.cs

     using System;

     using Grove;

     using WebApp1.Entities;

     using WebApp1.EntityDB;

 

     public class AddressManager

     {

         private AddressManager()

         {

 

         }

         public static void AddNewAddress(Address a)

         {

              AddressDB db=new AddressDB();

              db.Insert(a);

         }

         public static void DeleteAddress(Address a)

         {

              AddressDB db=new AddressDB();

              db.Delete(a);

         }

         public static void UpdateAddress(Address a)

         {

              AddressDB db=new AddressDB();

              db.Update(a);

         }

         public static EntityData GetAllAddresses()

         {

              AddressDB db=new AddressDB();

              return db.SelectAll();

         }

     }

代码3.Façade层定义

       10)添加CustomerAddress的添加页面,在ASPX页面后置类aspx.cs中添加对Façade层的引用实现对实体数据的插入,修改,删除等功能。

       简单示例:

              Customer c=new Customer();

              c.CustomerName=”创特软件”;

              c.CustomerDesc=”http://www.tryitsoft.com”;

              CustomerManager.AddNewCustomer(c );

       11)CustomersAddresses表中,Addresses表中的CustomerID为主表CustomersPK(主键),所以实体类Address.cs的属性CustomerID,绑定的DataField()可以改为ForeignKeyField(),在对某个Customer操作的时候,可以通过以下方式得到该客户的所有地址。

 

ArrayList addresses=new ArrayList();

IObjectOperator oo=ObjectOperatorFactory.GetObjectOperator();

oo.RetrieveChildObjects(c.CustomerID,addresses,typeof(Address));

 

       12)当我们需要快速开发,不需要考虑三层或多层应用的时候,实现实体类的自身持久化就很有必要,要实现实体类的自身持久化,必须继承Grove.DataObject.EntityClassBase

简单示例:

              Customer c=new Customer();

              c.CustomerName=”创特软件”;

       c.CustomerDesc=”http://www.tryitsoft.com”;

       c.Insert();

       13)***关于实体映射多表关系查询。点GroveToolKittoolbar中的Build Relationship Query按钮,出现多表关系查询向导,选择Entity Class点击下一步,出现Relation Query Builder窗口,在窗口的下半部分右击选择”Add Table…”或者在窗口的toolbar上点击Add Existing Table按钮,出现添加表窗口,分别添加表CustomersAddresses

 

3. 构造关系实体映射

       在图3中,点击Customers表的字段部分(按住鼠标左键)拖动到Addresses表的字段区域,并放开鼠标左键,出现两表关系窗口

4.表间关系属性

       在图4中选择Addresses表的CustomerID列后,点击OK,如果需要指定两表之间是左关联、右关联或者全外关联,请选择Include rows下的选项。

5.关系生成后

       在图5中,右击Customers表选择Select all columns或选择Table Property后选择需要显示的列。

6.选择表的显示列

 

       在图5中,选择Addresses表需要显示的列,然后点击当前窗口toolbar上的执行按钮,如果正确执行,执行结果将会显示在当前窗口,现在,点击toolbar上的Preview relation query class按钮预览关系实体映射类。

 

CustomersRelationQuery.cs

     using System;

     using Grove.DataObject;

     [RelationTable("CustomersRelationQuery",BeginWithTable="Customers")]

     public class CustomersRelationQuery

     {

         [RelationReflect("Customers","Addresses")]

         [RelationField("CustomerID","AddressID")]

         public string Relationship_1

         {

              get{return "[Customers].[CustomerID]=[Addresses].[AddressID]";}

         }

 

         int _CustomerID;

         [DataField("CustomerID",TableName="Customers")]

         public int CustomerID

         {

              get{return this._CustomerID;}

              set{this._CustomerID=value;}

         }

         string _CustomerName;

         [DataField("CustomerName",TableName="Customers")]

         public string CustomerName

         {

              get{return this._CustomerName;}

              set{this._CustomerName=value;}

         }

         string _CustomerDesc;

         [DataField("CustomerDesc",TableName="Customers")]

         public string CustomerDesc

         {

              get{return this._CustomerDesc;}

              set{this._CustomerDesc=value;}

         }

         string _Address;

         [DataField("Address",TableName="Addresses")]

         public string Address

         {

              get{return this._Address;}

              set{this._Address=value;}

         }

     }

代码4.关系映射实体类

本文地址:http://com.8s8s.com/it/it45233.htm