(完全限定類名:DataRabbit.ORM.IOrmAccesser)
在DataRabbit框架中,通過IOrmAccesser來對數據庫進行ORM訪問,只要Entity(即ORM中的“O”)的定義與數據庫表的結構完全一致,即可使用IOrmAccesser來對其進行ORM操作。
1.Entity
Entity除了包括成員變量與屬性(這些變量與屬性與數據庫表的結構完全一致)外,不需要包含任何其它元素。在 輕量的數據訪問框架 --序 的例子代碼中,我們已經看到了一個Student Entity的示例。
2.Filter
我們經常需要依據條件來搜索數據,在DataRabbit的ORM框架中,使用Filter來表示一個單獨的條件。Filter從ColumnItem繼承,ColumnItem 表示數據列及其對應的值。比如,在進行Update操作時,我們可以使用一個ColumnItem 的List來指定要將哪些列的值更新為指定的值。
首先,注意,Filter中包含一個GetBooleanExpression()方法,該方法將當前Filter轉換為一個標準的Sql條件字句。
Filter的ComparisonOperator屬性表明了采用何種比較符來構建條件表達式。
ComparisonOperators
在IOrmAccesser接口中,很多方法都接受 params Filter[] conditions 或IList<Filter>參數,比如:
針對接受 params Filter[] conditions 或IList<Filter>參數的方法,在OrmAccesser內部將會對每個Filter所表示的單個條件進行“與”操作,來作為搜索條件。那么如果需要對各個Filter進行“或”操作,或進行更復雜的邏輯組合(如“filter1 && (filter2 || filter3)”),就需要采用對應的接受IFilterTree作為參數的重載方法。
3.IFilterTree
IFilterTree 用于表示多個Filter的邏輯組合。
注意,IFilterTree中也包含一個GetBooleanExpression()方法,該方法將當前Filter通過邏輯組合轉換為一個標準的Sql條件字句。
當前的IFilterTree有兩個默認實現:
(1)SimpleFilterTree
該實現僅僅支持最簡單的對多個條件都進行“與”或都進行“或”的邏輯操作,更復雜的邏輯組合需求,可以使用AgileFilterTree。
如果將上述的例子,改用IFilterTree可以這樣實現:
(2)AgileFilterTree
AgileFilterTree 可以表示非常復雜的多個Filter的邏輯組合。它使用一個logicExpression字符串描述邏輯組合表達式,如"filter1 && (filter2 || filter3)",然后解析這個表達式獲取正確的Sql條件語句。
比如,我們要找出所有年齡在20與25歲之間,或者名字為sky的學生:
4.IOrmAccesser的泛型參數
IOrmAccesser是一個泛型接口,泛型參數是EntityType--即實體的類型,我們知道,每個EntityType對應著數據庫中的一個表,Entity class的名字最好與數據表的名字一致,這樣IOrmAccesser就可以根據EntityType知道要訪問數據庫中的哪個表。如果Entity class的名字最好與數據表的名字不一致,也沒關系,我們可以通過向IDataAccesser的OrmCoagent屬性注入IOrmCoagent來提供EntityType與數據表名稱的映射關系。IOrmCoagent接口定義如下:
5.基本"CRUD"方法
IOrmAccesser提供了基本的“CRUD”方法,如Insert,Update,Delete,GetOne,GetMuch,GetDataSet等等。這些方法都提供了多個重載,以接收單個Filter條件或多個Filter以及IFilterTree。
大多數方法的含義都特別清晰明了,有幾個方法的含義需要特別指出一下:
6.基于主外鍵關系進行Get獲取
IOrmAccesser會自動依據數據表的主外鍵關系來進行關系加載,比如,Student表中有MentorID字段(外鍵),對應Mentor表中的主鍵。那么,IOrmAccesser的GetParent方法,就可以根據指定的Student對象返回Mentor對象。
在DataRabbit框架中,通過IOrmAccesser來對數據庫進行ORM訪問,只要Entity(即ORM中的“O”)的定義與數據庫表的結構完全一致,即可使用IOrmAccesser來對其進行ORM操作。
1.Entity
Entity除了包括成員變量與屬性(這些變量與屬性與數據庫表的結構完全一致)外,不需要包含任何其它元素。在 輕量的數據訪問框架 --序 的例子代碼中,我們已經看到了一個Student Entity的示例。
2.Filter
我們經常需要依據條件來搜索數據,在DataRabbit的ORM框架中,使用Filter來表示一個單獨的條件。Filter從ColumnItem繼承,ColumnItem 表示數據列及其對應的值。比如,在進行Update操作時,我們可以使用一個ColumnItem 的List來指定要將哪些列的值更新為指定的值。

首先,注意,Filter中包含一個GetBooleanExpression()方法,該方法將當前Filter轉換為一個標準的Sql條件字句。
Filter的ComparisonOperator屬性表明了采用何種比較符來構建條件表達式。

void
Delete(IList
<
Filter
>
conditions);
void Delete( params Filter[]conditions);
比如,要刪除數據庫Student表中所有Age大于20歲且為男性的學生記錄
void Delete( params Filter[]conditions);
stuOrmAccesser.Delete(
new
Filter(Student._Age,
20
,ComparisonOperators.Greater),
new
Filter(Student._IsBoy,
true
,ComparisonOperators.Equal));
針對接受 params Filter[] conditions 或IList<Filter>參數的方法,在OrmAccesser內部將會對每個Filter所表示的單個條件進行“與”操作,來作為搜索條件。那么如果需要對各個Filter進行“或”操作,或進行更復雜的邏輯組合(如“filter1 && (filter2 || filter3)”),就需要采用對應的接受IFilterTree作為參數的重載方法。
3.IFilterTree
IFilterTree 用于表示多個Filter的邏輯組合。

注意,IFilterTree中也包含一個GetBooleanExpression()方法,該方法將當前Filter通過邏輯組合轉換為一個標準的Sql條件字句。
當前的IFilterTree有兩個默認實現:
(1)SimpleFilterTree
該實現僅僅支持最簡單的對多個條件都進行“與”或都進行“或”的邏輯操作,更復雜的邏輯組合需求,可以使用AgileFilterTree。
如果將上述的例子,改用IFilterTree可以這樣實現:
IList
<
Filter
>
conditions
=
new
List
<
Filter
>
();
conditions.Add( new Filter(Student._Age, 20 ,ComparisonOperators.Greater));
conditions.Add( new Filter(Student._IsBoy, true ,ComparisonOperators.Equal));
IFilterTree conditionTree = new SimpleFilterTree (LogicType.And,conditions);
stuOrmAccesser.Delete(conditionTree);
conditions.Add( new Filter(Student._Age, 20 ,ComparisonOperators.Greater));
conditions.Add( new Filter(Student._IsBoy, true ,ComparisonOperators.Equal));
IFilterTree conditionTree = new SimpleFilterTree (LogicType.And,conditions);
stuOrmAccesser.Delete(conditionTree);
(2)AgileFilterTree
AgileFilterTree 可以表示非常復雜的多個Filter的邏輯組合。它使用一個logicExpression字符串描述邏輯組合表達式,如"filter1 && (filter2 || filter3)",然后解析這個表達式獲取正確的Sql條件語句。
比如,我們要找出所有年齡在20與25歲之間,或者名字為sky的學生:
IDictionary
<
string
,Filter
>
mapping
=
new
Dictionary
<
string
,Filter
>
();
mapping.Add( " A " , new Filter (Student._Name, " sky " ,ComparisonOperators.Equal));
mapping.Add( " B " , new Filter (Student._Age, 20 ,ComparisonOperators.Greater));
mapping.Add( " C " , new Filter (Student._Age, 25 ,ComparisonOperators.Less));
IFilterTreeagileTree = new AgileFilterTree ( " A||(B&&C) " ,mapping);
IList <Student> list = stuOrmAccesser.GetMuch(agileTree);
mapping.Add( " A " , new Filter (Student._Name, " sky " ,ComparisonOperators.Equal));
mapping.Add( " B " , new Filter (Student._Age, 20 ,ComparisonOperators.Greater));
mapping.Add( " C " , new Filter (Student._Age, 25 ,ComparisonOperators.Less));
IFilterTreeagileTree = new AgileFilterTree ( " A||(B&&C) " ,mapping);
IList <Student> list = stuOrmAccesser.GetMuch(agileTree);
4.IOrmAccesser的泛型參數
IOrmAccesser是一個泛型接口,泛型參數是EntityType--即實體的類型,我們知道,每個EntityType對應著數據庫中的一個表,Entity class的名字最好與數據表的名字一致,這樣IOrmAccesser就可以根據EntityType知道要訪問數據庫中的哪個表。如果Entity class的名字最好與數據表的名字不一致,也沒關系,我們可以通過向IDataAccesser的OrmCoagent屬性注入IOrmCoagent來提供EntityType與數據表名稱的映射關系。IOrmCoagent接口定義如下:
public
interface
IOrmCoagent
{
/// <summary>
/// GetTableName根據Entity類型獲取對應的數據庫表名
/// </summary>
string GetTableName(TypeentityType);
}
IOrmAccesser可以通過IOrmCoagent的GetTableName()方法來獲取EntityType所對應的數據庫表的名稱。
{
/// <summary>
/// GetTableName根據Entity類型獲取對應的數據庫表名
/// </summary>
string GetTableName(TypeentityType);
}
5.基本"CRUD"方法
IOrmAccesser提供了基本的“CRUD”方法,如Insert,Update,Delete,GetOne,GetMuch,GetDataSet等等。這些方法都提供了多個重載,以接收單個Filter條件或多個Filter以及IFilterTree。
大多數方法的含義都特別清晰明了,有幾個方法的含義需要特別指出一下:
///
<summary>
/// GetMuchWithoutBlob根據參數conditions來提取符合條件的所有Entity,但是Entity的blob字段沒有被填充。
/// </summary>
IList < EntityType > GetMuchWithoutBlob( params Filter[]conditions);
/// <summary>
/// LoadBlob填充entity的Blob字段
/// </summary>
void LoadBlob(EntityTypeentity);
/// GetMuchWithoutBlob根據參數conditions來提取符合條件的所有Entity,但是Entity的blob字段沒有被填充。
/// </summary>
IList < EntityType > GetMuchWithoutBlob( params Filter[]conditions);
/// <summary>
/// LoadBlob填充entity的Blob字段
/// </summary>
void LoadBlob(EntityTypeentity);
///
<summary>
/// Update更新符合conditions的記錄中由columnItems指定的各個Column為對應目標值。
/// </summary>
void Update(IList < ColumnItem > columnItems,IList < Filter > conditions);
/// <summary>
/// GetDataSet選取表中符合條件的row,并且只返回columns指定的列。
/// </summary>
DataSetGetDataSet(IList < Filter > conditions, params string []columns);
/// Update更新符合conditions的記錄中由columnItems指定的各個Column為對應目標值。
/// </summary>
void Update(IList < ColumnItem > columnItems,IList < Filter > conditions);
/// <summary>
/// GetDataSet選取表中符合條件的row,并且只返回columns指定的列。
/// </summary>
DataSetGetDataSet(IList < Filter > conditions, params string []columns);
6.基于主外鍵關系進行Get獲取
IOrmAccesser會自動依據數據表的主外鍵關系來進行關系加載,比如,Student表中有MentorID字段(外鍵),對應Mentor表中的主鍵。那么,IOrmAccesser的GetParent方法,就可以根據指定的Student對象返回Mentor對象。
Mentormentor
=
stuOrmAccesser.GetParent
<
Mentor
>
(student);
基于主外鍵關系進行Get的方法包括:
///
<summary>
/// GetParent依據entity中外鍵的值,獲取主表中的實體。外鍵建立了"語義"上的"Master-Detail"關系
/// </summary>
ParentTypeGetParent < ParentType > (EntityTypeentity);
/// <summary>
/// GetForeigner依據entity中名為fkeyName外鍵的值,獲取主表中的對應實體。外鍵沒有"語義"上的"Master-Detail"關系
/// </summary>
ForeignerTypeGetForeigner < ForeignerType > (EntityTypeentity, string fkeyName);
/// <summary>
/// GetChildList依據entity中主鍵的值,獲取從表中的所有相關實體
/// </summary>
IList < ChildType > GetChildList < ChildType > (EntityTypeentity);
/// <summary>
/// GetChildDataSet依據entity中主鍵的值,獲取從表中的所有相關記錄
/// </summary>
DataSetGetChildDataSet < ChildType > (EntityTypeentity);
#endregion
/// GetParent依據entity中外鍵的值,獲取主表中的實體。外鍵建立了"語義"上的"Master-Detail"關系
/// </summary>
ParentTypeGetParent < ParentType > (EntityTypeentity);
/// <summary>
/// GetForeigner依據entity中名為fkeyName外鍵的值,獲取主表中的對應實體。外鍵沒有"語義"上的"Master-Detail"關系
/// </summary>
ForeignerTypeGetForeigner < ForeignerType > (EntityTypeentity, string fkeyName);
/// <summary>
/// GetChildList依據entity中主鍵的值,獲取從表中的所有相關實體
/// </summary>
IList < ChildType > GetChildList < ChildType > (EntityTypeentity);
/// <summary>
/// GetChildDataSet依據entity中主鍵的值,獲取從表中的所有相關記錄
/// </summary>
DataSetGetChildDataSet < ChildType > (EntityTypeentity);
#endregion
本文簡單介紹了IOrmAccesser的一些基本功能和使用,在后面的文章中將介紹一些IOrmAccesser
高級用法
。
返回到:
輕量的數據訪問框架 --序
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
