登山则情满于山,观海则意溢于海。这篇文章主要讲述ORM 轻量级框架 Dapper(介绍)相关的知识,希望能为你提供帮助。
Dapper是什么?Dapper是.net 一个简单的对象映射,就速度而言,他是轻量数据 Orm 之王,他和Ado.net的DataReader一样快。ORM是对象关系映射器,负责数据库和编程语言之间的映射。
Dapper 继承了IDbConnection,通过扩展数据库查询方法进行数据库查询
Dapper可与任何数据库提供程序使用,没有特定的数据库实现。
Dapper怎样工作?
主要分三个过程:
- 创建一个IDbConnection 对象
- 编写CRUD操作语句
- 将操作语句作为参数,传递给Execute 方法
Dapper通过Negut进行安装:https://www.nuget.org/packages/Dapper
PM> Install-Package Dapper
方法dapper使用多种方法扩展IDbConnection接口
- Execute
- Query
- QueryFirst
- QueryFirstOrDefault
- QuerySingle
- QuerySingleOrDefault
- QueryMultiple
string sqlOrderDetails = "SELECT TOP 5 * FROM OrderDetails; "; string sqlOrderDetail = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID; "; string sqlCustomerInsert = "INSERT INTO Customers (CustomerName) Values (@CustomerName); "; using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { var orderDetails = connection.Query< OrderDetail> (sqlOrderDetails).ToList(); var orderDetail = connection.QueryFirstOrDefault< OrderDetail> (sqlOrderDetail, new {OrderDetailID = 1}); var affectedRows = connection.Execute(sqlCustomerInsert,new {CustomerName = "Mark"}); Console.WriteLine(orderDetails.Count); Console.WriteLine(affectedRows); FiddleHelper.WriteTable(orderDetails); FiddleHelper.WriteTable(new List< OrderDetail> () { orderDetail }); }
参数执行和查询可以使用多种不同类型的参数
- Anonymous
- Dynamic
- List
- String
// Anonymous var affectedRows = connection.Execute(sql, new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"}, commandType: CommandType.StoredProcedure); // Dynamic DynamicParameters parameter = new DynamicParameters(); parameter.Add("@Kind", InvoiceKind.WebInvoice, DbType.Int32, ParameterDirection.Input); parameter.Add("@Code", "Many_Insert_0", DbType.String, ParameterDirection.Input); parameter.Add("@RowCount", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue); connection.Execute(sql, parameter, commandType: CommandType.StoredProcedure); int rowCount = parameter.Get< int> ("@RowCount"); // List connection.Query< Invoice> (sql, new {Kind = new[] {InvoiceKind.StoreInvoice, InvoiceKind.WebInvoice}}).ToList(); // String connection.Query< Invoice> (sql, new {Code = new DbString {Value = "https://www.songbingjia.com/android/Invoice_1", IsFixedLength = false, Length = 9, IsAnsi = true}}).ToList();
结果查询方法返回的结果可以映射为不同类型
- Anonymous
- Strongly Typed
- Multi-Mapping
- Multi-Result
- Multi-Type
string sqlOrderDetails = "SELECT TOP 10 * FROM OrderDetails; "; using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { var anonymousList = connection.Query(sqlOrderDetails).ToList(); var orderDetails = connection.Query< OrderDetail> (sqlOrderDetails).ToList(); Console.WriteLine(anonymousList.Count); Console.WriteLine(orderDetails.Count); FiddleHelper.WriteTable(orderDetails); FiddleHelper.WriteTable(connection.Query(sqlOrderDetails).FirstOrDefault()); }
实用工具
- Async
- Buffered
- Transaction
- Stored Procedure
// Async connection.QueryAsync< Invoice> (sql)// Buffered connection.Query< Invoice> (sql, buffered: false)// Transaction using (var transaction = connection.BeginTransaction()) { var affectedRows = connection.Execute(sql, new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"}, commandType: CommandType.StoredProcedure, transaction: transaction); transaction.Commit(); }// Stored Procedure var affectedRows = connection.Execute(sql, new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"}, commandType: CommandType.StoredProcedure);
【ORM 轻量级框架 Dapper(介绍)】
推荐阅读
- 恶搞电影《你的名字》表情图大全_微信
- 给SHOPIFY店铺产品页添加倒计时(无需安装APP)
- Rust for循环语句用法解释
- Ruby XPath和XSLT开发实例
- Ruby XML(REXML)处理开发实例
- Ruby OOPs概念(面向对象编程)
- BeanFactory和ApplicationContext的区别
- Win10系统Nvidia显卡驱动安装失败的处理办法
- Win10系统防范自动连接Wi-Fi热点的设置办法