以条件驱动的数据访问方式

所谓“以条件驱动”其实是自己给起名称,这种数据库访问方式和传统持久化组件的操作方式有所差别。在传统的数据访问操作中先明确操作对象然后设置相关条件;但在本文中所在地提到的访问方式是以条件为主导,就是根据你需要的情况编写条件直接在这基础上进行操作,并不需要显式借助于某个容器,所有操作所需要支持的环境在条件构成造过程中隐式创建。

看以下基于条件驱动访问数据的用例代码:

System.Collections.IList list= (DBMapping.Employees.EmployeeID==3).List();

以上代码是创建EmployeeID==3的条件并获取相关条件的雇员对象集。

当然条件模型可以相对复杂

list  =(DBMapping.Orders.Employee ==(DBMapping.Employees.EmployeeID ==3)).List();

以上是一个简单的对象约束条件。

list =(DBMapping.Orders.OrderDate.Between(

DateTime.Parse("1997-7-1"),DateTime.Parse("1997-7-31"))

     & DBMapping.Orders.EmployeeID==3).List();

   条件除了可以对象数据进行查询外还可以进行其他操作:

   (DBMapping.Employees.EmployeeID==3).Edit(

                   DBMapping.Employees.Region.Set("GuangZhou"));

更新相关条件下的字段模型信息。

     (DBMapping.Employees.Region.Match("Guang")).Delete();

或者删除相关条件的数据。                       

(DBMapping.Employees.FirstName =="henry").Count();

统计相关条件记录数.

以下是相关映射模型结构:

public class EmployeesMapper:HFSoft.Data.Mapping.Table

         {

              public EmployeesMapper(string name):base(name)

              {

                   mALL = new HFSoft.Data.Mapping.ObjectField("*",this);

                   mEmployeeID = new HFSoft.Data.Mapping.NumberField("EmployeeID",this);

                   mLastName = new HFSoft.Data.Mapping.StringField("LastName",this);

                   mFirstName = new HFSoft.Data.Mapping.StringField("FirstName",this);

                   mTitle = new HFSoft.Data.Mapping.StringField("Title",this);

                   mTitleOfCourtesy = new HFSoft.Data.Mapping.StringField("TitleOfCourtesy",this);

                   mBirthDate = new HFSoft.Data.Mapping.DateTimeField("BirthDate",this);

                   mHireDate = new HFSoft.Data.Mapping.DateTimeField("HireDate",this);

                   mAddress = new HFSoft.Data.Mapping.StringField("Address",this);

                   mCity = new HFSoft.Data.Mapping.StringField("City",this);

                   mRegion = new HFSoft.Data.Mapping.StringField("Region",this);

                   mPostalCode = new HFSoft.Data.Mapping.StringField("PostalCode",this);

                   mCountry = new HFSoft.Data.Mapping.StringField("Country",this);

                   mHomePhone = new HFSoft.Data.Mapping.StringField("HomePhone",this);

                   mExtension = new HFSoft.Data.Mapping.StringField("Extension",this);

                   mPhoto = new HFSoft.Data.Mapping.BytesField("Photo",this);

                   mNotes = new HFSoft.Data.Mapping.TextField("Notes",this);

                   mReportsTo = new HFSoft.Data.Mapping.NumberField("ReportsTo",this);

                   mPhotoPath = new HFSoft.Data.Mapping.StringField("PhotoPath",this);

              }

              public Employees  GetByIndex(int id,HFSoft.Data.IDataSession session)

              {

                   return (Employees)OnGetByIndex(id,session);

              }

              public Employees  GetByIndex(int id)

              {

                   return (Employees)OnGetByIndex(id);

              }

              private  HFSoft.Data.Mapping.NumberField mEmployeeID ;

              public HFSoft.Data.Mapping.NumberField EmployeeID

              {

                   get

                   {

                       return mEmployeeID;

                   }

              }

              private  HFSoft.Data.Mapping.StringField mLastName ;

………………………..

基本上所有包含条件的数据操作方式都可以通过这种方式去进行数据库操作。这种操作方式是在构造组件模型中无意发现,感觉其操作方便简单因此发表布出来,感趣的朋友发表一下自己的看法。

posted on 2006-08-20 22:23  henry  阅读(1980)  评论(5编辑  收藏  举报

导航