iT邦幫忙

0

Dapper和Slapper.AutoMapper多層應用問題

  • 分享至 

  • xImage

各位好,目前正在撰寫一支API碰到了一些問題想詢問各位大大可能的解法
這是我的Model

public class TestDto
    {
        public string CUSIDN { get; set; }
        public List<Zero> Zero { get; set; }
    }
    public class GUR
    {
        public string ITEMS { get; set; }
        public string NAME { get; set; }
    }
    public class Zero
    {
        public string CUSIDN { get; set; }
        public string ITMES { get; set; }
        public string ACN { get; set; }
        public List<GUR> GUR { get; set; }
    }

這是我的DB欄位資料內容分別擁有Test Table 和GUR Table
![https://ithelp.ithome.com.tw/upload/images/20220615/20127159Cs1mG37OvR.png]
https://ithelp.ithome.com.tw/upload/images/20220615/20127159v2TaVDD3zY.png
依照條件我進行了GURKEY的left join

select test.CUSIDN,test.ITMES,test.ACN,GUR.ITEMS,GUR.NAME
from test
left join GUR
on test.GURKEY = GUR.GURKEY
where CUSIDN='F123456789'

https://ithelp.ithome.com.tw/upload/images/20220615/20127159PqBBaaKGzK.png
最後我想將DB資料最後變成這樣的Json內容

{
    "CUSIDN":"F123456789",
    "Zero": [
        {
            "CUSIDN": "F123456789",
            "ITMES": "測試",
            "ACN": "1234556",
            "GUR": [
                {
                    "ITEMS": "123456",
                    "NAME": "張OO"
                },
                {
                    "ITEMS": "asd",
                    "NAME": "測試OO"
                }
            ]
        },
        {
            "CUSIDN": "F123456789",
            "ITMES": "測試二",
            "ACN": "1235323",
            "GUR": [
                {
                    "ITEMS": "23456",
                    "NAME": "文OO"
                }
            ]
        },
        {
            "CUSIDN": "F123456789",
            "ITMES": "測試三",
            "ACN": "1235323",
            "GUR": [
                {
                    "ITEMS": "123456",
                    "NAME": "張OO"
                },
                {
                    "ITEMS": "asd",
                    "NAME": "測試OO"
                }
            ]
        },

    ]
}

目前我寫了一個程式

        public static IEnumerable<TestDto> TestSlapper()
        {
            using (SqlConnection conn = new SqlConnection(GetDBConnectionString()))
            {
                string sql = @"
                select test.CUSIDN,test.CUSIDN AS Zero_CUSIDN,test.ITMES as Zero_ITMES,test.ACN as Zero_ACN,GUR.ITEMS as Zero_GUR_ITEMS,
                GUR.NAME as Zero_GUR_NAME
                from test
                left join GUR
                on test.GURKEY = GUR.GURKEY
                where CUSIDN='F123456789';
                ";
                var data = conn.Query<dynamic>(sql);

                var TestList = Slapper.AutoMapper.MapDynamic<TestDto>(data, false).ToList();
                return TestList;
            }
        }

這是我得到的結果

[
    {
        "CUSIDN": "F123456789",
        "Zero": [
            {
                "CUSIDN": "F123456789",
                "ITMES": "測試        ",
                "ACN": "1234556   ",
                "GUR": [
                    {
                        "ITEMS": "123456    ",
                        "NAME": "張OO       "
                    }
                ]
            }
        ]
    },
    {
        "CUSIDN": "F123456789",
        "Zero": [
            {
                "CUSIDN": "F123456789",
                "ITMES": "測試        ",
                "ACN": "1234556   ",
                "GUR": [
                    {
                        "ITEMS": "asd       ",
                        "NAME": "測試OO      "
                    }
                ]
            }
        ]
    },
    {
        "CUSIDN": "F123456789",
        "Zero": [
            {
                "CUSIDN": "F123456789",
                "ITMES": "測試二       ",
                "ACN": "1245693   ",
                "GUR": [
                    {
                        "ITEMS": "23456     ",
                        "NAME": "文OO       "
                    }
                ]
            }
        ]
    },
    {
        "CUSIDN": "F123456789",
        "Zero": [
            {
                "CUSIDN": "F123456789",
                "ITMES": "測試三       ",
                "ACN": "1235323   ",
                "GUR": [
                    {
                        "ITEMS": "123456    ",
                        "NAME": "張OO       "
                    }
                ]
            }
        ]
    },
    {
        "CUSIDN": "F123456789",
        "Zero": [
            {
                "CUSIDN": "F123456789",
                "ITMES": "測試三       ",
                "ACN": "1235323   ",
                "GUR": [
                    {
                        "ITEMS": "asd       ",
                        "NAME": "測試OO      "
                    }
                ]
            }
        ]
    }
]

這並不是我想要的結果
目前是有想說還是用Dapper的QueryMultiple來處理
但是會變成我必須先去select test Table的CUSIDN和ACN和GURKEY並存放到另外一個Model
再針對這個Model去做Foreach再去做塞值的結果
想請問各位大大是否有更好的做法呢?

看更多先前的討論...收起先前的討論...
canrong iT邦新手 2 級 ‧ 2022-06-15 23:09:58 檢舉
參考看看Foreign Key、Foreign Key Model
canrong iT邦新手 2 級 ‧ 2022-06-15 23:15:15 檢舉
可以試試看group by test.CUSIDN。
Mikey iT邦新手 5 級 ‧ 2022-06-16 12:42:49 檢舉
好的 謝謝您! 這是一個我沒想到的方式,我會再次嘗試看看 謝謝
canrong iT邦新手 2 級 ‧ 2022-06-16 13:45:09 檢舉
雖然沒使用Dapper寫過,但是你資料沒有使用group by收束它,這可能是導致輸出結果為一對一的原因,上面提供的兩個方向都可以嘗試看看可不可以單次查詢就達到你想要的目的。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
w4560000
iT邦研究生 5 級 ‧ 2022-06-16 10:16:49
最佳解答

可以參考這篇 Slapper.AutoMapper Identifiers 說明
小碼農米爾 - [C#][ASP.NET] Web API 開發心得 (6) - 輕量級的 ORM 工具 Dapper + Slapper.AutoMapper

測試程式

using Newtonsoft.Json;
using Slapper;
using System;
using System.Collections.Generic;
using System.Linq;

namespace AutomapperTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var dictionary = new Dictionary<string, object>
            {
                { "CUSIDN", "F123456789" },
                { "Zero_CUSIDN", "F123456789" },
                { "Zero_ITMES", "測試" },
                { "Zero_ACN", "1234556" },
                { "Zero_GUR_ITEMS", "123456" },
                { "Zero_GUR_NAME", "張OO" }
            };
            var dictionary2 = new Dictionary<string, object>
            {
                { "CUSIDN", "F123456789" },
                { "Zero_CUSIDN", "F123456789" },
                { "Zero_ITMES", "測試" },
                { "Zero_ACN", "1234556" },
                { "Zero_GUR_ITEMS", "asd" },
                { "Zero_GUR_NAME", "測試OO" }
            };
            var dictionary3 = new Dictionary<string, object>
            {
                { "CUSIDN", "F123456789" },
                { "Zero_CUSIDN", "F123456789" },
                { "Zero_ITMES", "測試二" },
                { "Zero_ACN", "1245693" },
                { "Zero_GUR_ITEMS", "23456" },
                { "Zero_GUR_NAME", "文OO" }
            };
            var dictionary4 = new Dictionary<string, object>
            {
                { "CUSIDN", "F123456789" },
                { "Zero_CUSIDN", "F123456789" },
                { "Zero_ITMES", "測試三" },
                { "Zero_ACN", "1235323" },
                { "Zero_GUR_ITEMS", "123456" },
                { "Zero_GUR_NAME", "張OO" }
            };
            var dictionary5 = new Dictionary<string, object>
            {
                { "CUSIDN", "F123456789" },
                { "Zero_CUSIDN", "F123456789" },
                { "Zero_ITMES", "測試三" },
                { "Zero_ACN", "1235323" },
                { "Zero_GUR_ITEMS", "asd" },
                { "Zero_GUR_NAME", "測試OO" }
            };

            var list = new List<IDictionary<string, object>> 
            { 
                dictionary, 
                dictionary2,
                dictionary3, 
                dictionary4, 
                dictionary5 
            };
            var ss = AutoMapper.MapDynamic<TestDto>(list, false).First();

            string json = JsonConvert.SerializeObject(ss);
            Console.ReadKey();
        }
    }

    public class TestDto
    {
        [AutoMapper.Id]
        public string CUSIDN { get; set; }

        public List<Zero> Zero { get; set; }
    }

    public class GUR
    {
        public string ITEMS { get; set; }
        public string NAME { get; set; }
    }

    public class Zero
    {
        public string CUSIDN { get; set; }
        public string ITMES { get; set; }

        [AutoMapper.Id]
        public string ACN { get; set; }

        public List<GUR> GUR { get; set; }
    }
}

Json

{
  "CUSIDN": "F123456789",
  "Zero": [
    {
      "CUSIDN": "F123456789",
      "ITMES": "測試",
      "ACN": "1234556",
      "GUR": [
        {
          "ITEMS": "123456",
          "NAME": "張OO"
        },
        {
          "ITEMS": "asd",
          "NAME": "測試OO"
        }
      ]
    },
    {
      "CUSIDN": "F123456789",
      "ITMES": "測試二",
      "ACN": "1245693",
      "GUR": [
        {
          "ITEMS": "23456",
          "NAME": "文OO"
        }
      ]
    },
    {
      "CUSIDN": "F123456789",
      "ITMES": "測試三",
      "ACN": "1235323",
      "GUR": [
        {
          "ITEMS": "123456",
          "NAME": "張OO"
        },
        {
          "ITEMS": "asd",
          "NAME": "測試OO"
        }
      ]
    }
  ]
}
Mikey iT邦新手 5 級 ‧ 2022-06-16 17:54:37 檢舉

謝謝您提供的方法,對於[AutoMapper.Id]我還需要去研究一下 感謝您!

我要發表回答

立即登入回答