最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

輕量級ORM框架Dapper應(yīng)用之實現(xiàn)DTO

 更新時間:2022年03月09日 14:04:29   作者:.NET開發(fā)菜鳥  
本文詳細講解了使用Dapper實現(xiàn)DTO的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、什么是DTO

先來看看百度百科的解釋:

數(shù)據(jù)傳輸對象(DTO)(Data Transfer Object),是一種設(shè)計模式之間傳輸數(shù)據(jù)的軟件應(yīng)用系統(tǒng)。數(shù)據(jù)傳輸目標往往是數(shù)據(jù)訪問對象從數(shù)據(jù)庫中檢索數(shù)據(jù)。數(shù)據(jù)傳輸對象與數(shù)據(jù)交互對象或數(shù)據(jù)訪問對象之間的差異是一個以不具有任何行為除了存儲和檢索的數(shù)據(jù)(訪問和存取器)。

二、為什么需要DTO

在一個軟件系統(tǒng)的實現(xiàn)中,我們常常需要訪問數(shù)據(jù)庫,并將從數(shù)據(jù)庫中所取得的數(shù)據(jù)顯示在用戶界面上。這樣做的一個問題是:用于在用戶界面上展示的數(shù)據(jù)模型和從數(shù)據(jù)庫中取得的數(shù)據(jù)模型常常具有較大區(qū)別。在這種情況下,我們常常需要向服務(wù)端發(fā)送多個請求才能將用于在頁面中展示的數(shù)據(jù)湊齊。

三、使用Dapper實現(xiàn)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對應(yīng)ProductDetail表的Price,CategoryName對應(yīng)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)
        {
            // 數(shù)據(jù)庫連接
            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,將查詢的字段轉(zhuǎn)換成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>();
        }
    }
}

結(jié)果:

從截圖中看出,返回的就是想要的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)
        {
            // 數(shù)據(jù)庫連接
            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,將查詢的字段轉(zhuǎn)換成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語句中根據(jù)哪個字段進行分割
            string splitOn = "CategoryName";
            List<ProductDto> list = conn.Query<ProductDetail, Category, ProductDto>(strSql2, map, splitOn: splitOn).ToList<ProductDto>();
        }
    }
}

結(jié)果:

注意:

1、splitOn

splitOn表示查詢的SQL語句中按照哪個字段進行分割,splitOn的順序是從右向左的,遇到splitOn設(shè)置的字段接結(jié)束,把從右邊開始到設(shè)置的這個字段歸為同一個實體。例如:上面的例子中,splitOn設(shè)置為CategoryName,則表示從右邊開始,到CategoryName為止的所有字段都是屬于Category這個實體的,剩余的字段都是屬于ProductDetail實體的。

2、注意委托中實體類的前后順序

委托中實體類的前后順序一定要和查詢的SQL語句中字段的前后順序一致,上面的例子中先查詢的ProductDetail、后查詢的Category,那么定義委托的時候,要先寫ProductDetail,后寫Category,如果委托中實體類的順序錯了,那么不會得到映射的數(shù)據(jù),看下面的例子:

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)
        {
            // 數(shù)據(jù)庫連接
            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,將查詢的字段轉(zhuǎn)換成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語句中根據(jù)哪個字段進行分割
            string splitOn = "CategoryName";
            List<ProductDto> list = conn.Query< Category, ProductDetail, ProductDto>(strSql2, map, splitOn: splitOn).ToList<ProductDto>();
        }
    }
}

結(jié)果:

到此這篇關(guān)于使用Dapper實現(xiàn)DTO的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

新巴尔虎右旗| 宜都市| 拜泉县| 太仓市| 阳东县| 崇阳县| 南昌县| 泉州市| 怀安县| 柯坪县| 军事| 平远县| 乐陵市| 农安县| 健康| 若尔盖县| 北碚区| 安图县| 吴忠市| 连城县| 兰州市| 抚州市| 九龙县| 临西县| 赤壁市| 托里县| 乌拉特前旗| 通渭县| 公主岭市| 南丹县| 玛曲县| 江源县| 临邑县| 会东县| 独山县| 新源县| 马公市| 绥棱县| 延吉市| 卢湾区| 北票市|