一、课程导学
当前市面上的 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数据模型映射(上)
02、视频链结:FluentData轻量级.NET ORM持久化技术详解03- Mapping数据模型映射(下)
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.