MyBatis.NET(以前稱爲iBatis.NET)是一個數據映射工具,具有我覺得非常有用的功能。如果你從來沒有聽說過它,它是一個ORM工具,它允許你編寫SQL,並創建域對象,並使用XML將數據庫列映射到域對象屬性。
其中一個結果映射可能看起來像:
<resultMap id="Contact" class="Contact" >
<result property="ContactId" column="ContactId" />
<result property="FirstName" column="FirstName" />
<result property="MiddleInitial" column="MiddleInitial" />
<result property="LastName" column="LastName" />
</resultMap>
在有你有一個id爲結果,一類別名(它映射到Namespace.Folder.Contact,例如),和一個列表列屬性映射。
你可能有興趣的地方在於它有能力擴展結果地圖,就像你從類繼承一樣。在你的情況,你可能有:
<resultMap id="Person" class="Person" >
<result property="Id" column="Id" />
<result property="Name" column="Name" />
<result property="Email" column="Email" />
<result property="Phone" column="Phone" />
</resultMap>
併爲您的其他情況:
<resultMap id="PersonExtended" extends="Person" class="Person">
<!-- inherits all the "Person" result map properties -->
<result property="SpecialPhoneNumber" column="SpecialPhoneNumber" />
<result property="VeryCustomValue" column="VeryCustomValue" />
</resultMap>
然後,你將有兩個單獨的查詢,一個選擇僅僅是基本的信息和地圖的「人「結果圖和另一個映射到包含更多列的」PersonExtended「結果圖的查詢。
還支持選擇字典和避免N + 1選擇。只是要注意:我不屬於MyBatis項目,我剛剛發現它非常靈活,並且是一個非常穩固的數據映射器。我將它用於我的所有項目,主要是因爲我編寫了一個工具,只需將它指向數據庫即可生成我的整個數據訪問層。
設置它有點PITA,但也許它可以爲你工作!
哇「也可以選擇爲動態對象,但與Massive不同,它只能選擇數據,不能插入,更新或刪除它。」沒有什麼比真相更遠了。 – 2012-01-31 02:43:18
@SamSaffron:我不使用Dapper,我想我在回想起最初發布時只選擇了數據。當我將Dapper添加到這個答案時,我檢查了文檔,從我可以告訴的情況來看,情況依然如此。我現在看到它可以簡單地執行任何sql,但是在doc頁面中有點模糊。我已經編輯了我的答案,正在考慮您的意見。 – 2012-01-31 03:10:40
它確實插入很好,從doco:http://code.google.com/p/dapper-dot-net/「執行一個不返回結果的命令 public static int Execute(this IDbConnection cnn,string sql, object param = null,SqlTransaction transaction = null)「..另請參閱:http://samsaffron.com/archive/2012/01/16/that-annoying-insert-problem-getting-data-into-the-db- using-dapper我正致力於改進Dapper標記wiki,但這個過程很慢 – 2012-01-31 03:18:04