数据访问层学习

博客介绍了数据-元数据-元元数据的三层结构,重点阐述ECC(Engine-Collection-Class)模式的数据存取。该模式将数据对象操作和数据分离,还说明了Collection-Class的数据存储方式。同时给出数据访问、定制、导入导出工具的使用方法,并通过代码示例展示用户类的实现,以及三个关键类的扩展和封装。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.1       数据-元数据-元元数据的三层结构

数据:指业务系统中保存的业务数据。

 

元数据:即数据的数据,一个对象的元数据用来描述此对象。

元元数据:元数据的元数据。

1.2       ECCEngine-Collection-Class)模式数据存取

在一个系统的业务实体或数据对象中,实体的主要操作有:初始化、读取数据、删除、保存数据等等,实体的主要属性有:实体实例的数据、实体的元数据、实体的状态等等。

当表示一个业务实体的多个实例(如十个用户的集合)时,我们一般都采用系统提供的集合或数据来存储,这种方法实现起来简便,但当我们对这些实例再进行操作时,一方面会增加复杂性,也有可能会大大降低效率,并且不太容易进行扩展和修改。

ECC模式中,我们将一个数据对象操作和数据分离,Engine主要负责数据对象的存取和初始化操作,完成数据对象实例的创建,Class主要用来表示数据对象的一个实例,包括实例的数据和实例的元数据、状态等等。然后我们将表示数据对象实例集合的类型也进行封装,用Collection来表示,其中封装相应的操作和属性,如集合中元素个数、增加或删除一个元素、访问指定位置的元素等等。此模式模型如下图所示:

1.3       Collection-Class的数据存储方式

Collection在内存中的数据存储我们可以看作是一个二维的表格,在表格中第一行代表一个数据对象实例,表格的行数即为Collection中的元素个数。表格的每一列代表数据对象的一个属性,数据对象的位置确定,属性名称确定,即可以确定相应的属性值。

Class对应Collection的二维表格上的一行,在设计时,我们在Class中不存储具体的数据,只存储Class所在的Collection的引用和ClassCollection中的位置,这样我们就可以实现Class的各个属性的访问。

1.4       使用方法

1)       数据访问配置工具:生成配置文件.Config

 

初始化配置文件、提供定制工具对配置文件进行定制等等。

 

2)  数据对象定制工具:定制数据对象

 

3)  数据对象数据导入导出工具:

 

对数据库中的数据对象的数据进行导出和导入操作。

 

 

 

 

 

代码示例一:

//       public string Example()

 

//       {

 

//            string strUserID="8";

 

//            EngineGeneral eng=new EngineGeneral("User");

 

//            eng.TheCriteria=new BinaryOperator("UserID",strUserID);

 

//            IDPCollection col=eng.Search();

 

//            if(col.Count <= 0) return null;

 

//            return col[0]["UserID"].ToString();

 

//       }

 

代码示例二:

 

/// <summary>

 

     /// 表示一个用户对象。

 

     /// </summary>

 

     public class User:ClassGeneral

 

     {

 

         /// <summary>

 

         /// 用户ID

 

         /// </summary>

 

         /// <value>用户ID</value>

 

         public Guid UserID

 

         {

 

              get  {return (Guid)this["UserID"];}

 

              set {this["UserID"] = value;}

 

          }

 

 

         /// <summary>

 

         /// 用户登陆名称

 

         /// </summary>

 

         /// <value></value>

 

         public string Name

 

         {

 

              get  {return (string)this["Name"];}

 

              set {this["Name"] = value;}

 

         }

 

 

         /// <summary>

 

         /// 用户真实名称

 

         /// </summary>

 

         /// <value></value>

 

         public string TrueName

 

         {

 

              get  {return (string)this["TrueName"];}

 

              set {this["TrueName"] = value;}

 

         }

 

 

 

         /// <summary>

 

         /// 用户密码

 

         /// </summary>

 

         /// <value>用户密码</value>

 

         public string Password

 

         {

 

              set

 

              {

 

                   string temp = SysUtlilty.CryptStringByMD5(value);

 

                   this["Password"] = temp;

 

              }

 

         }

 

 

         public bool PassswordIs(string val)

 

         {

 

              string inputpwd = SysUtlilty.CryptStringByMD5(val);

 

              string pwd = this["Password"].ToString();

 

              if(pwd == inputpwd) return true;

 

              return false;

 

         }

 

 

         /// <summary>

 

         /// 用户描述

 

         /// </summary>

 

         /// <value>用户描述</value>

 

         public string Description

 

         {

 

              get  {return (string)this["Description"];}

 

              set {this["Description"] = value;}

 

         }

 

 

         public Guid FGID

 

         {

 

              get { return (Guid)this["FGID"]; }

 

              set { this["FGID"] = value; }

 

         }

 

 

         public Guid DGID

 

         {

 

              get { return (Guid)this["DGID"]; }

 

              set { this["DGID"] = value; }

 

         }

 

 

         public int Sequence

 

         {

 

              get { return (int)this["Sequence"];}

 

              set { this["Sequence"] = value;}

 

         }

 

 

         /// <summary>

 

         /// 用户所在的组

 

         /// </summary>

 

         /// <value>用户所在的组</value>

 

         public Department Department

 

         {

 

              get

 

              {

 

                   DepartmentEng dEg = new DepartmentEng();

 

dEg.TheCriteria = new BinaryOperator("DepartmentID", (Guid)this["DepartmentID"]);

 

                   DepartmentCollection depts = dEg.Search() as DepartmentCollection;

 

                   if (depts != null)

 

                   {

 

                       if (depts.Count <= 0) return null;

 

                       if (depts.Count > 1) return depts[0] as Department;

 

                       return depts[0] as Department;

 

                   }

 

                   else

 

                   {

 

                       return null;

 

                   }

 

              }

 

         }

 

     }

 

 

     /// <summary>

 

     /// 表示用户对象的集合。

 

     /// </summary>

 

     public class UserCollection:CollectionGeneral

 

     {

 

         public UserCollection()

 

         {

 

              this.ClassTypeInCollection = typeof(User);

 

         }

 

     }

 

 

     /// <summary>

 

     /// 用来存取用户对象的数据。

 

     /// </summary>

 

     public class UserEngine:EngineGeneral

 

     {

 

         public UserEngine():base("User")

 

         {

 

              this.CollectionTypeInEngine = typeof(UserCollection);

 

         }

 

 

         public static User NewUser()

 

         {

 

              UserEngine engine = new UserEngine();

 

              return engine.NewClass() as User;

 

         }

 

 

         public static bool Update(User col)

 

         {

 

              UserEngine engine = new UserEngine();

 

              engine.Update(col.Collection);

 

              return true;

 

         }

 

        

 

         /// <summary>

 

         /// 根据用户ID查询用户对象。

 

         /// </summary>

 

         /// <param name="strUserID">用户ID</param>

 

         /// <returns>得到的用户对象</returns>

 

         public static User SearchByID(string strUserID)

 

         {

 

              if (strUserID.Trim() == "" || strUserID.Trim() == Guid.Empty.ToString()) return null;

 

 

              UserEngine eng = new UserEngine();

 

              eng.TheCriteria = new BinaryOperator("UserID", strUserID);

 

              IDPCollection col = eng.Search() as UserCollection;

 

 

              if(col.Count <= 0) return null;

 

              return col[0] as User;

 

         }

 

 

         /// <summary>

 

         /// 根据部门ID,查找部门中人员的集合

 

         /// </summary>

 

         /// <param name="strName">部门ID</param>

 

         /// <returns>满足条件的用户集合</returns>

 

         public static UserCollection SearchByDeptId(string strDeptId)

 

         {

 

              UserEngine eng = new UserEngine();

 

              eng.TheCriteria = new BinaryOperator("FGID", strDeptId);

 

              eng.TheOrderBy = new OrderByOperatorGeneral("Sequence");

 

              return eng.Search() as UserCollection;

 

         }

 

 

         /// <summary>

 

         /// 根据角色ID,查找部门中人员的集合。

 

         /// </summary>

 

         /// <param name="strName">角色ID</param>

 

         /// <returns>满足条件的用户集合</returns>

 

         public static ArrayList SearchByRoleId(string strRoleId)

 

         {

 

              ArrayList userList=new ArrayList();

 

              UserRoles uroles=UserRoleEngine.GetByRole(new Guid(strRoleId));

 

              if (uroles != null)

 

              {

 

                   for(int i=0;i<uroles.Count;i++)

 

                   {

 

                       string strUserID=((UserRole)uroles[i]).UserID.ToString();

 

                       User user=UserEngine.SearchByID(strUserID);

 

                       userList.Add(user);

 

                   }

 

              }

 

              return userList;

 

         }

 

 

         /// <summary>

 

         /// 根据岗位的Id,查找岗位中的人员集合

 

         /// </summary>

 

         /// <returns></returns>

 

         public static UserCollection SearchByPosId(string strDeptId,string strRoleId)

 

         {

 

           // UserCollection userByDeptId=UserEngine.SearchByDeptId(strDeptId);

 

              return null;

 

         }

 

 

         public static UserCollection SearchAll()

 

         {

 

 

              UserEngine eng = new UserEngine();

 

              eng.TheCriteria = null;

 

              eng.TheOrderBy = new OrderByOperatorGeneral("Sequence");

 

              return eng.Search() as UserCollection;

 

         }

 

 

         public static UserCollection SearchByName(string userName)

 

         {

 

 

              UserEngine eng = new UserEngine();

 

              eng.TheCriteria = new BinaryOperator("Name", userName);;

 

              eng.TheOrderBy = new OrderByOperatorGeneral("Sequence");

 

              return eng.Search() as UserCollection;

 

         }

 

}

 

要点:

 

1.      三个关键的类

 

EngineGeneral:扩展此类主要是提供更具体的数据存取操作,如此类提供的读取数据的操作一般是赋予查询内容、查询规则等属性后,进行读取数据对象的功能,在此基础上可以扩展出具体业务对象的存取操作,如对于User对象的查询引擎的可以扩展方法GetByUserIDGetByUserNameUpdateUserCollectionUpdateUser等等。

CollectionGeneral:此类封装数据对象集合的数据和基本操作方法,如对数据对象的访问、基于此集合的内存查询等等,但都是较通用的方法,扩展此类主要是提供更通用的操作,如对User对象的集合对象可以扩展方法FindUserByIDFindByName等等。

ClassGeneral:此类提供对数据对象的数据的封装,所有数据对象的属性以属性列表的形式提供,所以在访问时前提是要知道所访问属属的名称,并且在编码时不能使用IDE提供的智能提示功能,对此类的扩展主要是硬编码对象的属性,以利于访问者的使用。如对User的对象类可以扩展属性:UserIDNameDescription等等。但是由于单个对象还可能包括其它的操作,如用户密码在数据库存储的加密、访问此用户对象的部门属性等等,可以在此类中扩展。

2.      对上面三个类的封装

 

UserEngine:EngineGeneral

 

UserCollection:CollectionGeneral

 

User:ClassGeneral

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值