如何使用Dapper轻量级ORM框架实现DTO的转换和操作?

2026-03-30 11:111阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1662个文字,预计阅读时间需要7分钟。

如何使用Dapper轻量级ORM框架实现DTO的转换和操作?

一、什么是DTO?

DTO(Data Transfer Object)是数据传输对象(Data Transfer Object),是一种设计模式。它是软件应用系统中的数据传输载体,用于在系统间传递数据。

DTO的作用:

1. 将数据封装起来,便于在系统间传递。

2.隐藏数据访问细节,提高代码的可维护性。

3.便于数据序列化和反序列化。

DTO的使用场景:

1. 客户端与服务端之间的数据传输。

2.服务端与数据库之间的数据传输。

3.不同服务之间进行数据交互。

DTO举例:

假设有一个用户实体,需要将用户信息从数据库查询后传输到前端界面。这时可以使用DTO来封装用户信息,如下:

java

public class UserDTO { private String username; private String password; private String email; // ... 其他属性}

通过DTO,可以将用户实体中的数据封装起来,方便在客户端和服务端之间传输。

一、什么是DTO

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

数据传输对象(DTO)(Data Transfer Object),是一种设计模式之间传输数据的软件应用系统。数据传输目标往往是数据访问对象从数据库中检索数据。数据传输对象与数据交互对象或数据访问对象之间的差异是一个以不具有任何行为除了存储和检索的数据(访问和存取器)。

二、为什么需要DTO

在一个软件系统的实现中,我们常常需要访问数据库,并将从数据库中所取得的数据显示在用户界面上。这样做的一个问题是:用于在用户界面上展示的数据模型和从数据库中取得的数据模型常常具有较大区别。在这种情况下,我们常常需要向服务端发送多个请求才能将用于在页面中展示的数据凑齐。

三、使用Dapper实现DTO

使用Dapper可以直接返回DTO类型,包括两种方式:

新建Category、ProductDetail和ProductDTO实体类:

Category实体类定义如下:

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表的Price,CategoryName对应Category表的CategoryName。

方式一:直接在SQL语句中使用as

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.Query<ProductDto>(strSql).FirstOrDefault<ProductDto>(); } } }

结果:

从截图中看出,返回的就是想要的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.Query<ProductDto>(strSql).FirstOrDefault<ProductDto>(); // 方式二:使用委托进行自定义映射 string strSql2 = @" SELECT p.ProductId,p.ProductName,p.Price,c.CategoryName FROM Category c INNER JOIN ProductDetail p ON c.CategoryId=p.CategoryId "; // 定义映射的委托 Func<ProductDetail, Category, ProductDto> map = (p, c) => { 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"; List<ProductDto> list = conn.Query<ProductDetail, Category, ProductDto>(strSql2, map, splitOn: splitOn).ToList<ProductDto>(); } } }

结果:

注意:

1、splitOn

splitOn表示查询的SQL语句中按照哪个字段进行分割,splitOn的顺序是从右向左的,遇到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.Query<ProductDto>(strSql).FirstOrDefault<ProductDto>(); // 方式二:使用委托进行自定义映射 string strSql2 = @" SELECT p.ProductId,p.ProductName,p.Price,c.CategoryName FROM Category c INNER JOIN ProductDetail p ON c.CategoryId=p.CategoryId "; // 定义映射的委托 //Func<ProductDetail, Category, ProductDto> map = (p, c) => //{ // ProductDto dto = new ProductDto(); // dto.ProductId = p.ProductId; // dto.ProductName = p.ProductName; // dto.ProductPrice = p.Price; // dto.CategoryName = c.CategoryName; // return dto; //}; // 错误的委托 Func<Category, ProductDetail, ProductDto> map = (c,p) => { 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"; List<ProductDto> list = conn.Query< Category, ProductDetail, ProductDto>(strSql2, map, splitOn: splitOn).ToList<ProductDto>(); } } }

结果:

到此这篇关于使用Dapper实现DTO的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持自由互联。

如何使用Dapper轻量级ORM框架实现DTO的转换和操作?

本文共计1662个文字,预计阅读时间需要7分钟。

如何使用Dapper轻量级ORM框架实现DTO的转换和操作?

一、什么是DTO?

DTO(Data Transfer Object)是数据传输对象(Data Transfer Object),是一种设计模式。它是软件应用系统中的数据传输载体,用于在系统间传递数据。

DTO的作用:

1. 将数据封装起来,便于在系统间传递。

2.隐藏数据访问细节,提高代码的可维护性。

3.便于数据序列化和反序列化。

DTO的使用场景:

1. 客户端与服务端之间的数据传输。

2.服务端与数据库之间的数据传输。

3.不同服务之间进行数据交互。

DTO举例:

假设有一个用户实体,需要将用户信息从数据库查询后传输到前端界面。这时可以使用DTO来封装用户信息,如下:

java

public class UserDTO { private String username; private String password; private String email; // ... 其他属性}

通过DTO,可以将用户实体中的数据封装起来,方便在客户端和服务端之间传输。

一、什么是DTO

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

数据传输对象(DTO)(Data Transfer Object),是一种设计模式之间传输数据的软件应用系统。数据传输目标往往是数据访问对象从数据库中检索数据。数据传输对象与数据交互对象或数据访问对象之间的差异是一个以不具有任何行为除了存储和检索的数据(访问和存取器)。

二、为什么需要DTO

在一个软件系统的实现中,我们常常需要访问数据库,并将从数据库中所取得的数据显示在用户界面上。这样做的一个问题是:用于在用户界面上展示的数据模型和从数据库中取得的数据模型常常具有较大区别。在这种情况下,我们常常需要向服务端发送多个请求才能将用于在页面中展示的数据凑齐。

三、使用Dapper实现DTO

使用Dapper可以直接返回DTO类型,包括两种方式:

新建Category、ProductDetail和ProductDTO实体类:

Category实体类定义如下:

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表的Price,CategoryName对应Category表的CategoryName。

方式一:直接在SQL语句中使用as

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.Query<ProductDto>(strSql).FirstOrDefault<ProductDto>(); } } }

结果:

从截图中看出,返回的就是想要的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.Query<ProductDto>(strSql).FirstOrDefault<ProductDto>(); // 方式二:使用委托进行自定义映射 string strSql2 = @" SELECT p.ProductId,p.ProductName,p.Price,c.CategoryName FROM Category c INNER JOIN ProductDetail p ON c.CategoryId=p.CategoryId "; // 定义映射的委托 Func<ProductDetail, Category, ProductDto> map = (p, c) => { 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"; List<ProductDto> list = conn.Query<ProductDetail, Category, ProductDto>(strSql2, map, splitOn: splitOn).ToList<ProductDto>(); } } }

结果:

注意:

1、splitOn

splitOn表示查询的SQL语句中按照哪个字段进行分割,splitOn的顺序是从右向左的,遇到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.Query<ProductDto>(strSql).FirstOrDefault<ProductDto>(); // 方式二:使用委托进行自定义映射 string strSql2 = @" SELECT p.ProductId,p.ProductName,p.Price,c.CategoryName FROM Category c INNER JOIN ProductDetail p ON c.CategoryId=p.CategoryId "; // 定义映射的委托 //Func<ProductDetail, Category, ProductDto> map = (p, c) => //{ // ProductDto dto = new ProductDto(); // dto.ProductId = p.ProductId; // dto.ProductName = p.ProductName; // dto.ProductPrice = p.Price; // dto.CategoryName = c.CategoryName; // return dto; //}; // 错误的委托 Func<Category, ProductDetail, ProductDto> map = (c,p) => { 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"; List<ProductDto> list = conn.Query< Category, ProductDetail, ProductDto>(strSql2, map, splitOn: splitOn).ToList<ProductDto>(); } } }

结果:

到此这篇关于使用Dapper实现DTO的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持自由互联。

如何使用Dapper轻量级ORM框架实现DTO的转换和操作?