DataRabbit 轻量的数据访问框架(04) -- IEntityRelationLoader

  (完全限定类名:DataRabbit.ORM.IEntityRelationLoader

   在DataRabbit框架提供的ORM功能之中,除了IOrmAccesser接口展现的核心ORM功能外,IEntityRelationLoader接口也提供了一些有意义的功能。正如其名,IEntityRelationLoader是通过数据表的主外键关系来加载当前Entity的Parent和Children。

   现在对我们前面示例经常用到的Student数据表做个扩充,假设,Student表的MentorID字段作为外键,指向Mentor表;而且,Book表中的StudentID字段也是外键,指向Student表。这种主外键关系的含义是:“一个学生有一个导师,并且拥有多本书”。这三个表的关系图如下所示:

   通过对Student扩充后,我们可以为Student Entity Class 加上如下两个属性,来反映这种关系:
 
         #region  Mentor
        [NonSerialized]
        
private  Mentor m_Mentor  =   null  ; 
        
public  Mentor Mentor
        {
            
get
            {
                
return   this .m_Mentor ;
            }
            
set
            {
                
this .m_Mentor  =  value ;
            }
        }
        
#endregion

        
#region  BookList
        [NonSerialized]
        
private  IList < Book >  m_BookList  =   null  ; 
        
public  IList < Book >  BookList
        {
            
get
            {
                
return   this .m_BookList ;
            }
            
set
            {
                
this .m_BookList  =  value ;
            }
        }
        
#endregion
      
   我将这种通过主外键关系得到的属性称为“FamilyRelation”属性,如果使用EntityCreator工具生成Entity Class,这些属性都会根据数据表关系而自动生成。

   现在,我们来调用如下语句:
    Student student  =  stuOrmAccesser.GetOne( new  Filter(Student._ID,  30 ));        
   结果会发现,返回的student对象的Mentor属性和BookList属性不会被填充。如果不使用IEntityRelationLoader接口,而是直接通过IOrmAccesser来获取这个student的“Family”,我们需要这样做:
    Student student = stuOrmAccesser.GetOne(new Filter(Student._ID, 30));
    student.Mentor 
=  stuOrmAccesser.GetParent < Mentor > (student);
    student.BookList 
=  stuOrmAccesser.GetChildList < Book > (student);
   像这样子可以达到目的,但是未免繁琐了一点,如果Student表有多个外键,则我们就要调用多次GetParent()方法来分别获取各个Parent;对于多个Children,也是如此。IEntityRelationLoader接口使得这种基于关系的加载变得非常简单,如上述功能使用下面的代码即可实现:
   entityRelationLoader.LoadFamily(student);
   LoadFamily方法一次调用可以加载当前student对象的所有“Family”成员(注意,这里的加载“Family”只包括加载自己Parent和Children,而不包括更上的grandfather或更下的grandson等)。

    我们可以从DataRabbit的入口点IDataAccesser接口获取IEntityRelationLoader引用:
   IEntityRelationLoader entityRelationLoader  =  dataAccesser.GetEntityRelationLoader( null );
    
   通过上面的示例,我们对IEntityRelationLoader的功能已经有了一些了解。下面我们来看看这个接口的全貌:
     public   interface  IEntityRelationLoader :ITransactionAccesser
    {
        
///   <summary>
        
///  LoadChildren 加载自己的children到对应的属性字段(如果Entity提供了这些属性字段,并且属性可写)上。
        
///   </summary>   
         void  LoadChildren( object  entity);

        
///   <summary>
        
///  LoadParents 加载自己的parents到对应的属性字段(如果Entity提供了这些属性字段,并且属性可写)上。
        
///   </summary>
         void  LoadParents( object  entity);

        
///   <summary>
        
///  LoadFamily 加载自己的parents和children到对应的属性字段(如果Entity提供了这些属性字段,并且属性可写)上。
        
///   </summary>         
         void  LoadFamily( object  entity);

        
///   <summary>
        
///  LoadOffspring 以当前entity为根,加载所有后代。注意,自己以及后代的所有Parent属性都不会被赋值。
        
///   </summary>   
         void  LoadOffspring( object  entity);       
    }



转到:DataRabbit 轻量的数据访问框架 -- 序


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值