轻量级ORM框架Dapper应用之实现DTO

2022-04-17 11:05:34

一、什么是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 DapperConvertDtowww.easck.com{    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>();        }    }}

结果:

轻量级ORM框架Dapper应用之实现DTO

从截图中看出,返回的就是想要的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 strbTkraQSql = @" 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>();        }    }}

结果:

轻量级ORM框架Dapper应用之实现DTO

注意:

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) =>http://www.easck.com;            //{            //    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>();        }    }}

结果:

轻量级ORM框架Dapper应用之实现DTO

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