亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

一、什么是DTO

先來(lái)看看百度百科的解釋?zhuān)?/p>

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

二、為什么需要DTO

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

三、使用Dapper實(shí)現(xiàn)DTO

使用Dapper可以直接返回DTO類(lèi)型,包括兩種方式:

新建Category、ProductDetail和ProductDTO實(shí)體類(lèi):

Category實(shí)體類(lèi)定義如下:

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實(shí)體類(lèi)定義如下:

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實(shí)體類(lèi)定義如下:

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實(shí)體類(lèi)中的ProductPrice對(duì)應(yīng)ProductDetail表的Price,CategoryName對(duì)應(yīng)Category表的CategoryName。

方式一:直接在SQL語(yǔ)句中使用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ù)庫(kù)連接
            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語(yǔ)句中使用as,將查詢(xún)的字段轉(zhuǎn)換成DTO類(lèi)型的屬性
            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類(lèi)型。

方式二:使用委托的方式進(jìn)行映射,分別把Category和ProductDetail實(shí)體類(lèi)里的屬性,映射成ProductDTO類(lèi)型的屬性:

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ù)庫(kù)連接
            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語(yǔ)句中使用as,將查詢(xún)的字段轉(zhuǎn)換成DTO類(lèi)型的屬性
            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>();

            // 方式二:使用委托進(jìn)行自定義映射
            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表示查詢(xún)的SQL語(yǔ)句中根據(jù)哪個(gè)字段進(jìn)行分割
            string splitOn = "CategoryName";
            List<ProductDto> list = conn.Query<ProductDetail, Category, ProductDto>(strSql2, map, splitOn: splitOn).ToList<ProductDto>();
        }
    }
}

結(jié)果:

注意:

1、splitOn

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

2、注意委托中實(shí)體類(lèi)的前后順序

委托中實(shí)體類(lèi)的前后順序一定要和查詢(xún)的SQL語(yǔ)句中字段的前后順序一致,上面的例子中先查詢(xún)的ProductDetail、后查詢(xún)的Category,那么定義委托的時(shí)候,要先寫(xiě)ProductDetail,后寫(xiě)Category,如果委托中實(shí)體類(lèi)的順序錯(cuò)了,那么不會(huì)得到映射的數(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ù)庫(kù)連接
            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語(yǔ)句中使用as,將查詢(xún)的字段轉(zhuǎn)換成DTO類(lèi)型的屬性
            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>();

            // 方式二:使用委托進(jìn)行自定義映射
            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;
            //};

            // 錯(cuò)誤的委托
            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表示查詢(xún)的SQL語(yǔ)句中根據(jù)哪個(gè)字段進(jìn)行分割
            string splitOn = "CategoryName";
            List<ProductDto> list = conn.Query< Category, ProductDetail, ProductDto>(strSql2, map, splitOn: splitOn).ToList<ProductDto>();
        }
    }
}

結(jié)果:

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

相關(guān)文章

  • ASP.NET中頁(yè)面之間傳遞值的幾種方式整理

    ASP.NET中頁(yè)面之間傳遞值的幾種方式整理

    頁(yè)面?zhèn)髦凳菍W(xué)習(xí)asp.net初期都會(huì)面臨的一個(gè)問(wèn)題,總的來(lái)說(shuō)有頁(yè)面?zhèn)髦?、存?chǔ)對(duì)象傳值、ajax、類(lèi)、model、表單等,這里簡(jiǎn)單的整理下,需要的朋友可以收藏下
    2012-05-05
  • Repeater怎么實(shí)現(xiàn)多行間隔顯示分隔符

    Repeater怎么實(shí)現(xiàn)多行間隔顯示分隔符

    本文為大家介紹下Repeater如何實(shí)現(xiàn)多行間隔顯示分隔符,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下
    2014-01-01
  • .net 解決spider多次和重復(fù)抓取的方案

    .net 解決spider多次和重復(fù)抓取的方案

    這篇文章主要介紹了.net 解決spider多次和重復(fù)抓取的解決方案,需要的朋友可以參考下
    2015-01-01
  • ASP.NET MVC頁(yè)面重定向簡(jiǎn)單介紹

    ASP.NET MVC頁(yè)面重定向簡(jiǎn)單介紹

    這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC頁(yè)面重定向的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • ASP.NET MVC使用異步TPL模式

    ASP.NET MVC使用異步TPL模式

    這篇文章介紹了ASP.NET MVC使用異步TPL模式的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • asp.net 利用NPOI導(dǎo)出Excel通用類(lèi)的方法

    asp.net 利用NPOI導(dǎo)出Excel通用類(lèi)的方法

    本篇文章主要介紹了asp.net 利用NPOI導(dǎo)出Excel通用類(lèi)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • DataTable多列合并問(wèn)題輕松搞定

    DataTable多列合并問(wèn)題輕松搞定

    由于題庫(kù)的表結(jié)構(gòu)不相同,導(dǎo)致同樣的Gridview在顯示時(shí)不能同時(shí)兩種不同結(jié)構(gòu)的數(shù)據(jù),這時(shí)如何在這個(gè)固定的GridView中顯示不同的數(shù)據(jù)呢,感興趣的朋友可以看下本文的解決方法
    2013-04-04
  • 詳解log4net的使用

    詳解log4net的使用

    這篇文章主要介紹了log4net的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • .NET 6開(kāi)發(fā)TodoList應(yīng)用之使用AutoMapper實(shí)現(xiàn)GET請(qǐng)求

    .NET 6開(kāi)發(fā)TodoList應(yīng)用之使用AutoMapper實(shí)現(xiàn)GET請(qǐng)求

    我們希望接受的請(qǐng)求和返回的值具有以下兩點(diǎn)需要遵循的原則:每個(gè)model被且只被一個(gè)API消費(fèi);每個(gè)model里僅僅包含API發(fā)起方希望包含的必要字段或?qū)傩浴utoMapper庫(kù)就是為了實(shí)現(xiàn)這個(gè)需求而存在的。本文將為大家介紹AutoMapper如何實(shí)現(xiàn)GET請(qǐng)求,需要的可以參考一下
    2021-12-12
  • .NET中RDLC循環(huán)處理數(shù)據(jù)的應(yīng)用分析

    .NET中RDLC循環(huán)處理數(shù)據(jù)的應(yīng)用分析

    本篇文章介紹了,.NET中RDLC循環(huán)處理數(shù)據(jù)的應(yīng)用分析。需要的朋友參考下
    2013-05-05

最新評(píng)論