运维开发网

如何实现Dapper应用程序DTO

运维开发网 https://www.qedev.com 2022-07-30 21:28 出处:网络
本文详细讲解了使用Dapper实现DTO的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 一、什么是DTO

本文详细讲解了使用Dapper实现DTO的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 一、什么是DTO

我们先来看看百度百科的解释:

数据传输对象(DTO)是一个在设计模式之间传输数据的软件应用系统。数据传输目标通常是从数据库中检索数据的数据访问对象。数据传输对象与数据交互对象或数据访问对象的区别在于,数据(访问和访问器)除了存储和检索之外没有其他行为。

二、为什么需要DTO

在一个软件系统的实现中,我们经常需要访问数据库并将从数据库中获得的数据显示在用户界面上。其中一个问题是,用于在用户界面上显示的数据模型通常与从数据库中获得的数据模型大相径庭。在这种情况下,我们经常需要向服务器发送多个请求来收集数据,以便在页面中显示。

三、使用Dapper实现DTO

衣冠楚楚可以直接返回DTO式,包括两种方式:

创建新类别、ProductDetail和ProductDTO实体类:

类别实体类定义如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DapperConvertDto{ public class Category { public int CategoryId { get; set; } public string CategoryName { get; set; } }}

ProductDetail实体类定义如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DapperConvertDto{ public class ProductDetail { public int ProductId { get; set; } public string ProductName { get; set; } public double Price { get; set; } public int CategoryId { get; set; } }}

ProductDTO实体类定义如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DapperConvertDto{ public class ProductDto { public int ProductId { get; set; } public string ProductName { get; set; } public double ProductPrice { get; set; } public string CategoryName { get; set; } }}

ProductDTO实体类中的ProductPrice对应于ProductDetail表的价格,CategoryName对应于Category表的CategoryName。

方式一:直接在SQL语句中使用asusing System;using System.Collections.Generic;using System.Data.SqlClient;using System.Linq;using System.Text;using System.Threading.Tasks;using Dapper;namespace DapperConvertDto{ class Program { static void Main(string[] args) { // 数据库连接 string strCon = @"Initial Catalog=StudentSystem; Integrated Security=False;User Id=sa;Password=1qaz@WSX;Data Source=127.0.0.1;Failover Partner=127.0.0.1;Application Name=TransForCCT"; SqlConnection conn = new SqlConnection(strCon); // 方式一:直接在SQL语句中使用as,将查询的字段转换成DTO类型的属性 string strSql = @" SELECT p.ProductId,p.ProductName,p.Price AS ProductPrice,c.CategoryName FROM Category c INNER JOIN ProductDetail p ON c.CategoryId=p.CategoryId "; ProductDto product = conn.Querylt;ProductDtogt;(strSql).FirstOrDefaultlt;ProductDtogt;(); } }}

结果:


从截图中可以看到,返回的是想要的DTO类型。

方式二:使用委托的方式进行映射,分别把Category和ProductDetail实体类里的属性,映射成ProductDTO类型的属性:using System;using System.Collections.Generic;using System.Data.SqlClient;using System.Linq;using System.Text;using System.Threading.Tasks;using Dapper;namespace DapperConvertDto{ class Program { static void Main(string[] args) { // 数据库连接 string strCon = @"Initial Catalog=StudentSystem; Integrated Security=False;User Id=sa;Password=1qaz@WSX;Data Source=127.0.0.1;Failover Partner=127.0.0.1;Application Name=TransForCCT"; SqlConnection conn = new SqlConnection(strCon); // 方式一:直接在SQL语句中使用as,将查询的字段转换成DTO类型的属性 string strSql = @" SELECT p.ProductId,p.ProductName,p.Price AS ProductPrice,c.CategoryName FROM Category c INNER JOIN ProductDetail p ON c.CategoryId=p.CategoryId "; ProductDto product = conn.Querylt;ProductDtogt;(strSql).FirstOrDefaultlt;ProductDtogt;(); // 方式二:使用委托进行自定义映射 string strSql2 = @" SELECT p.ProductId,p.ProductName,p.Price,c.CategoryName FROM Category c INNER JOIN ProductDetail p ON c.CategoryId=p.CategoryId "; // 定义映射的委托 Funclt;ProductDetail, Category, ProductDtogt; map = (p, c) =gt; { ProductDto dto = new ProductDto(); dto.ProductId = p.ProductId; dto.ProductName = p.ProductName; dto.ProductPrice = p.Price; dto.CategoryName = c.CategoryName; return dto; }; // splitOn表示查询的SQL语句中根据哪个字段进行分割 string splitOn = "CategoryName"; Listlt;ProductDtogt; list = conn.Querylt;ProductDetail, Category, ProductDtogt;(strSql2, map, splitOn: splitOn).ToListlt;ProductDtogt;(); } }}

结果:


注意:

1、splitOn

SplitOn指示根据查询的SQL语句中的哪个字段进行划分。拆分的顺序是从右到左。当splitOn设置的字段结束时,从右边到设置的字段被分类为同一个实体。例如,在上面的示例中,splitOn被设置为CategoryName,这意味着从CategoryName右侧的所有字段都属于Category实体,其余字段属于ProductDetail实体。

2、注意委托中实体类的前后顺序

委托中实体类的顺序必须与查询SQL语句中字段的顺序一致。在上面的示例中,首先查询ProductDetail,然后查询Category。定义委托时,应该先写ProductDetail,再写Category。如果委托中实体类的顺序是错误的,您将无法获得映射的数据。请参见以下示例:

using System;using System.Collections.Generic;using System.Data.SqlClient;using System.Linq;using System.Text;using System.Threading.Tasks;using Dapper;namespace DapperConvertDto{ class Program { static void Main(string[] args) { // 数据库连接 string strCon = @"Initial Catalog=StudentSystem; Integrated Security=False;User Id=sa;Password=1qaz@WSX;Data Source=127.0.0.1;Failover Partner=127.0.0.1;Application Name=TransForCCT"; SqlConnection conn = new SqlConnection(strCon); // 方式一:直接在SQL语句中使用as,将查询的字段转换成DTO类型的属性 string strSql = @" SELECT p.ProductId,p.ProductName,p.Price AS ProductPrice,c.CategoryName FROM Category c INNER JOIN ProductDetail p ON c.CategoryId=p.CategoryId "; ProductDto product = conn.Querylt;ProductDtogt;(strSql).FirstOrDefaultlt;ProductDtogt;(); // 方式二:使用委托进行自定义映射 string strSql2 = @" SELECT p.ProductId,p.ProductName,p.Price,c.CategoryName FROM Category c INNER JOIN ProductDetail p ON c.CategoryId=p.CategoryId "; // 定义映射的委托 //Funclt;ProductDetail, Category, ProductDtogt; map = (p, c) =gt; //{ // ProductDto dto = new ProductDto(); // dto.ProductId = p.ProductId; // dto.ProductName = p.ProductName; // dto.ProductPrice = p.Price; // dto.CategoryName = c.CategoryName; // return dto; //}; // 错误的委托 Funclt;Category, ProductDetail, ProductDtogt; map = (c,p) =gt; { ProductDto dto = new ProductDto(); dto.ProductId = p.ProductId; dto.ProductName = p.ProductName; dto.ProductPrice = p.Price; dto.CategoryName = c.CategoryName; return dto; }; // splitOn表示查询的SQL语句中根据哪个字段进行分割 string splitOn = "CategoryName"; Listlt;ProductDtogt; list = conn.Querylt; Category, ProductDetail, ProductDtogt;(strSql2, map, splitOn: splitOn).ToListlt;ProductDtogt;(); } }}

结果:


这就是这篇关于使用Dapper实现DTO的文章。希望对大家的学习有所帮助,

0

精彩评论

暂无评论...
验证码 换一张
取 消