CoreLibrary 讲解:
对于一个实体类的数据处理层,有两个数据访问类与之配对 (DataAccessor 和 DataMapper )
以 后缀 DataAccessor 的类 :直接和数据库层打交道,比如:新增,插入,修改等等
以 后缀 DataMapper 的类:直接位于DataAccessor 上一层,负责调用DataAccessor 类,
有点透明代理的作用,其他的作用是在里面处理cache等信息
(注:在框架的演化过程中,启用了AOP横切技术思想,因此DataMapper的类的作用被弱化,
可能考虑在今后的版本中去掉)
IBaseDataAccessor 基础操作(插入,更新,删除)
INormalQueryDataAccessor 查询操作
IReferenceQueryDataAccessor 引用操作(
对于一般的类,查询只需返回对象即可,而对于接口和抽象类,
因为不能实例化,所以只能通过传入引用来获取数据)
接口只有一个方法 void FindInheritObject(IDomainObject obj);
下面的类就是继承它们的接口然后实现之。
BaseDataAccessor类是一个抽象类,里面实现了某些方法,这些方法是通用方法
- public abstract class BaseDataAccessor:IBaseDataAccessor
- {
- public abstract Type GetDomainObjectType();
- public abstract bool IsValidType(IDomainObject obj);
- public abstract Guid InSave(IDomainObject obj, bool IsNew);
- public abstract Guid InSave(IDomainObject obj, bool IsNew, BusinessTransaction Trans);
- public abstract void InDelete(IDomainObject obj);
- public abstract void InDelete(IDomainObject obj, BusinessTransaction Trans);
- public abstract void InDelete(Guid key);
- public abstract void InDelete(Guid key, BusinessTransaction Trans);
- public abstract void InDelete(StatementSource source);
- public abstract void InDelete(StatementSource source, BusinessTransaction Trans);
-
-
-
-
- public string GetDBInstance()
- {
- }
-
-
-
-
-
-
- public string GetReadDataInstance()
获取数据库实例就是使用的微软企业库的代码
这几个核心类实现了对象间继承关系的扭转,完成将一个对象的数据放到不同的表中间去,
当然对于没有继承关系的表就更没有问题了。
同时由于继承关系被它们实现再加上配置文件的管理,因为生成的实体数据库操作类的代码几乎全部一致,
也就产生了后面的使用了自动代码生成器来生成这些实体类和实体控制类,
完成后台代码开发的90%以上工作量的自动化。
- [Serializable]
-
- public abstract class BaseUser:DomainObject
- {
-
- public string Email
- {
- get;
- set;
- }
-
- public string Type
- {
- get;
- set;
- }
-
- }
- [Serializable]
-
- public class Users:BaseUser
- {
-
- public string UserName
- {
- get;
- set;
- }
-
- public string UserPwd
- {
- get;
- set;
- }
-
- public DateTime CreateTime
- {
- get;
- set;
- }
-
- }
- internal class BaseUserDataAccessor:InterfaceVersionDataAccessor
- {
- private string __dataDBInstance = string.Empty;
- private string __dataReadDBInstance = string.Empty;
- private Database db = null;
- private Database dbRead = null;
- public BaseUserDataAccessor()
- {
- __dataDBInstance = this.GetDBInstance();
- __dataReadDBInstance = this.GetReadDataInstance();
- this.InitGeneralizationDataAccessor();
- db = DatabaseFactory.CreateDatabase(__dataDBInstance);
- dbRead = DatabaseFactory.CreateDatabase(__dataReadDBInstance);
-
-
- }
- public override Type GetDomainObjectType()
- {
- return typeof(BaseUser);
- }
- public override bool IsValidType(IDomainObject obj)
- {
- if(obj is BaseUser)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public override Guid InSave(IDomainObject obj, bool IsNew)
- {
-
- try
- {
- Guid key = INInSave(obj, IsNew);
- return key;
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- public override Guid InSave(IDomainObject obj, bool IsNew, BusinessTransaction Trans)
- {
- try
- {
-
- Guid key = INInSave(obj, IsNew, Trans);
- return key;
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- public override void InDelete(IDomainObject obj)
- {
-
- try
- {
- INInDelete(obj.Key);
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- public override void InDelete(IDomainObject obj, BusinessTransaction Trans)
- {
- try
- {
- INInDelete(obj.Key,Trans);
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- public override void InDelete(Guid key)
- {
-
- try
- {
- INInDelete(key);
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- public override void InDelete(Guid key, BusinessTransaction Trans)
- {
- try
- {
- INInDelete(key,Trans);
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- public override void InDelete(StatementSource source)
- {
-
- try
- {
- ArrayList list = new ArrayList();
- list = GetKeyList(source);
- for(int i=0;i
- {
- Guid key = GuidParse.GetGuidType(list[i].ToString());
- this.INInDelete(key);
- }
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- public override void InDelete(StatementSource source, BusinessTransaction Trans)
- {
- try
- {
-
- ArrayList list = new ArrayList();
- list = GetKeyList(source);
- for(int i=0;i
- {
- Guid key = GuidParse.GetGuidType(list[i].ToString());
- this.INInDelete(key,Trans);
- }
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- public override void FindInheritObject(IDomainObject obj)
- {
- 。。。
- }
- private Guid INInSave(IDomainObject obj, bool IsNew, BusinessTransaction Trans)
- {
- try
- {
- 。。。
- this.InSaveGeneralizationDataAccessor(obj,true,Trans);
- strSQL = "Insert into ["+this.GetDomainObjectType().Name+"]"+
- "(ID,Email) Values(@ID,@Email)";
- 。。。
- return obj.Key;
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- private Guid INInSave(IDomainObject obj, bool IsNew)
- {
- try
- {
- 。。。
- return obj.Key;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- private void INInDelete(Guid key, BusinessTransaction Trans)
- {
- try
- {
- string strSQL = "Delete From [" + this.GetDomainObjectType().Name + "] where ID=@ID";
-
- this.InDeleteGeneralizationDataAccessor(key,Trans);
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- private void INInDelete(Guid key)
- {
- try
- {
-
- 。。。
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- private ArrayList GetKeyList(StatementSource source)
- {
- try
- {
- 。。。
- return IDList;
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- }
从代码中可以看出, BaseUserDataAccrssor 和 UserDataAccessor 继承的类不一样,因
为实体类BaseUser是抽象类,而User是一般的类。 抽象类不能实例化,
因此在DataAccessor里面没有Select之类有返回对象的方法,
而UserDataAccessor 类就存在返回对象的查询方法
比如: public override IDomainObject SelectByPK(Guid key)
框架的关键之一也就在这里
DataAccrssor 类除了处理自身实体类的数据操作,还要调用父类的DataAccrssor 方法来完成数据插入到父表的操作
InSaveGeneralizationDataAccessor(obj, false); 这个方法实现了把对象中的属于父亲类的属性值插入到父表中去
InDeleteGeneralizationDataAccessor(key,。。。); 这个方法完成删除操作(先删除子类的记录,再删除父类的记录)