FluentData輕量級.NET ORM持久化技術詳解03- Mapping數據模型映射(視頻+教程)

一、課程導學

當前市面上的 ORM 框架,如 Entity Framework 和 NHibernate,都過于復雜而且難于學習。此外,由于這些框架自身抽象的查詢語言以及從數據庫到 .NET 對象的映射太過麻煩,導致它們生成的 SQL 都很低效。

FluentData 另辟蹊徑,它是一個輕量級框架,擁有簡單的 fluent API 并且很容易學會。現在越來越多的.NET企業級開發選擇后端的ORM采用FluentData框架。

本系列教程將會從最基本的FluentData安裝、配置開始講起,一步步深入API使用與詳細案例與性能優,全程會以【視頻+教程】方式呈現,原系我本人的一某在線網站的收費課程,現打算全部公開出去。具體教程公開計劃如下:

01_FluentData快速入門

02_FluentData進階-Query查詢

03_FluentData進階-Mapping

04_FluentData進階-多結果集&分頁查詢

05_FluentData進階-Insert的三種方式

06_FluentData進階-InsertAndUpdateBuilder&Delete操作

07_FluentData高級-如何操作存儲過程

08_FluentData高級-使用事務與Entity Factory

09_FluentData高級-綜合示例

我會持續本專題,如你想要獲取本套課程課件,源碼可在本文后留言給我,我收到后會第一時間回復。你的支持是我持續寫作的動力,如你覺得對你有幫助,歡迎點贊,收藏,轉發,評論,打賞,謝謝!

二、視頻

本章內容:FluentData輕量級.NET ORM持久化技術詳解03- Mapping數據模型映射(視頻+教程)

01、視頻鏈結:FluentData輕量級.NET ORM持久化技術詳解03- Mapping數據模型映射(上)


FluentData輕量級.NET ORM持久化技術詳解03- Mapping數據模型映射(上) - 騰訊視頻

02、視頻鏈結:FluentData輕量級.NET ORM持久化技術詳解03- Mapping數據模型映射(下)


FluentData輕量級.NET ORM持久化技術詳解03- Mapping數據模型映射(下) - 騰訊視頻

03、視頻鏈結:FluentData輕量級.NET ORM持久化技術詳解03- 多結果集&分頁技術


FluentData輕量級.NET ORM持久化技術詳解03- 多結果集&分頁技術- 騰訊視頻


三、配套文字教程

3.1、Mapping(映射)

3.1.1、 自動匹配:在數據庫和.NET 對象一一對應Listproducts = Context.Sql(@"select *from Product").QueryMany();

3.1.2、自動映射到自定義的集合ProductionCollection products = Context.Sql("select * from Product").QueryMany();

3.1.3、 Automapping - Mismatch between the database and the .NET object, use the alias keyword in SQL:Weakly typed:Listproducts = Context.Sql(@"select p.*,c.CategoryId as Category_CategoryId,c.Name as Category_Namefrom Product pinner join Category c on p.CategoryId = c.CategoryId").QueryMany();

3.1.4、 Custom mapping using dynamic:Listproducts = Context.Sql(@"select * from Product").QueryMany(Custom_mapper_using_dynamic);

public void Custom_mapper_using_dynamic(Product product, dynamic row)

{

product.ProductId = row.ProductId;

product.Name = row.Name;

}

3.1.5、Custom mapping using a datareader:Listproducts = Context.Sql(@"select * from Product").QueryMany(Custom_mapper_using_datareader);public void Custom_mapper_using_datareader(Product product, IDataReader row){product.ProductId = row.GetInt32("ProductId");product.Name = row.GetString("Name");}或者:如果你有一個比較復雜的類型你需要控制它的創建的話,那么你可以使用QueryComplexMany/QueryComplexSingle方法來實現var products = new List();Context.Sql("select * from Product").QueryComplexMany(products, MapComplexProduct);private void MapComplexProduct(IListproducts, IDataReader reader)

{

var product = new Product();

product.ProductId = reader.GetInt32("ProductId");

product.Name = reader.GetString("Name");

products.Add(product);

}

3.2、多結果集(Multiple result sets)

FluentData supports multiple resultsets. This allows you to do multiple queries in a single database call. When this feature is used it's important to wrap the code inside a using statement as shown below in order to make sure that the database connection is closed.using (var command = Context.MultiResultSql){Listcategories = command.Sql(@"select * from Category;select * from Product;").QueryMany();Listproducts = command.QueryMany();

}

The first time the Query method is called it does a single query against the database. The second time the Query is called, FluentData already knows that it's running in a multiple result set mode, so it reuses the data retrieved from the first query.

3.3、分頁(Select data and Paging)

A select builder exists to make selecting data and paging easy:Listproducts = Context.Select("p.*, c.Name as Category_Name")

.From(@"Product p

inner join Category c on c.CategoryId = p.CategoryId")

.Where("p.ProductId > 0 and p.Name is not null")

.OrderBy("p.Name")

.Paging(1, 10).QueryMany();

By calling Paging(1, 10) then the first 10 products will be returned.

四、結束

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容