iT邦幫忙

0

API 三層架構 練習參考問題請教

  • 分享至 

  • xImage

您好:
參考
https://igouist.github.io/post/2021/10/newbie-5-3-layer-architecture/

https://ithelp.ithome.com.tw/upload/images/20240106/201040956EZyMMy66K.png

1.範例的參考引用架構
https://ithelp.ithome.com.tw/upload/images/20240106/201040951lVtkIxEt7.png

2.CardRepository : ICardRepository
有裝 AutoMapper
https://ithelp.ithome.com.tw/upload/images/20240106/20104095TU9pD7Dlog.png

==>範圍太小,則DTO的class 要改為public
==>DyamicParameter 這一個,於該專案要在安裝Dapper 才能using

3.做到Service時
ServiceMappings : Profile 出現一堆錯誤
https://ithelp.ithome.com.tw/upload/images/20240106/20104095JuFswd4AkV.png

請問,這是該引用參考 還是 using ?

3-1. CardService : ICardService
https://ithelp.ithome.com.tw/upload/images/20240106/20104095PVNXeSwr3s.png

==>要using
其他也是DTO class要 public

4.Controller
請問
ControllerMappings()
這是要放在 哪一層資料夾下?

謝謝!

tenno081 iT邦研究生 4 級 ‧ 2024-01-09 11:24:21 檢舉
你是用.net core 3.1嗎?順便問一下你有沒有先寫過沒分層的CRUD
noway iT邦研究生 3 級 ‧ 2024-01-09 14:18:50 檢舉
沒有
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
念戀
iT邦新手 5 級 ‧ 2024-01-08 16:10:43
最佳解答

會不會是跨專案的model 沒有將專案引入你需要用到的那個.cs?

看更多先前的回應...收起先前的回應...
noway iT邦研究生 3 級 ‧ 2024-01-08 19:56:34 檢舉

都有跟著說明設定

念戀 iT邦新手 5 級 ‧ 2024-01-09 16:02:49 檢舉

圖片
模擬出上面的錯誤 是少做了
https://igouist.github.io/post/2021/10/newbie-5-3-layer-architecture/
加入參考的部分

noway iT邦研究生 3 級 ‧ 2024-01-09 23:00:16 檢舉

您好:
您是說說

API or APP: Service + Common
Service: Repository + Common
Repository: Common

這一個嗎?
有做引用
還是哪一個環節少了?
謝謝

https://ithelp.ithome.com.tw/upload/images/20240109/2010409511MRcLhSwm.png

念戀 iT邦新手 5 級 ‧ 2024-01-10 09:14:11 檢舉

感謝提供更多資訊
若還是沒排除錯誤
請在上方using 加入
using ProjectN.Service.Dtos.Condition;
using ProjectN.Service.Dtos.DataModel;

若還是無法using 請檢查Condition/Condition.cs的namespace 比照檢查DataModel/DataModel.cs的namespace 做對應的namespace using

noway iT邦研究生 3 級 ‧ 2024-01-10 22:15:15 檢舉

您好:
謝謝您
在試過一次,整理如員POST
前3項都沒紅底線了

目前剩下
4.Controller
請問
ControllerMappings()
這是要放在 哪一層資料夾下?

謝謝

念戀 iT邦新手 5 級 ‧ 2024-01-11 09:19:02 檢舉

就跟ServiceMappings 一樣的做法 只是在主站下

noway iT邦研究生 3 級 ‧ 2024-01-11 14:06:00 檢舉

projectN 的 controll 資料夾 下嗎?

念戀 iT邦新手 5 級 ‧ 2024-01-11 15:04:49 檢舉

projectN下建立一個controllMappings資料夾 再建一個.cs

noway iT邦研究生 3 級 ‧ 2024-01-11 20:59:47 檢舉

您好:
謝謝
1.我先建立ControllMappings 相關

https://ithelp.ithome.com.tw/upload/images/20240111/20104095Gn1I4qonKc.png

2.再把其他control相關程式碼 貼上
3.執行select /inert 測試
但都出現以下錯誤
https://ithelp.ithome.com.tw/upload/images/20240111/20104095tfV82KRuBg.png

於 AutoMapper.AutoMapperMappingException 擲回例外狀況: 'AutoMapper.dll'
'AutoMapper.AutoMapperMappingException' 類型的例外狀況發生於 AutoMapper.dll,但使用者程式碼未加以處理
Cannot create interface ProjectN.Service.Dtos.Info.CardSearchInfo

ProjectN.exe' (CoreCLR: clrhost): 已載入 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\PrivateAssemblies\Runtime\Microsoft.VisualStudio.Debugger.Runtime.NetCoreApp.dll'。已略過載入符號。模組已最佳化,並已啟用 [Just My Code] 偵錯工具選項。
'[12164] ProjectN.exe' 程式以返回碼 4294967295 (0xffffffff) 結束。

但我看有這檔案
這又是什模問題?
謝謝

念戀 iT邦新手 5 級 ‧ 2024-01-12 11:35:39 檢舉

CardSearchInfo 和 CardInfo 實際是什麼樣子的
??該不會CardSearchInfo
是寫 public interface CardSearchInfo
CardInfo
public interface CardInfo
??

noway iT邦研究生 3 級 ‧ 2024-01-14 21:17:52 檢舉

您好:
的確寫成interface 了,改掉後,重新build後,可以RUN
但有一個問題
GetList 有一個name 參數

https://ithelp.ithome.com.tw/upload/images/20240114/2010409532SdiD2q4a.png

https://ithelp.ithome.com.tw/upload/images/20240114/20104095Wma5ZJGv3Y.png

name 有下條件,但結果全部出來
相對的attack 有下條件,會依據條件出來

public class CardSearchInfo
{
public string Name { get; set; }

這一個 有事著改成
public string? Name { get; set; }

我看controller

  [HttpGet]
        [Produces("application/json")]
        public IEnumerable<CardViewModel> GetList(
            [FromQuery] CardSearchParameter parameter)
        {
            var info = this._mapper.Map<
                CardSearchParameter,
                CardSearchInfo>(parameter);

            var cards = this._cardService.GetList(info);

            var result = this._mapper.Map<
                IEnumerable<CardResultModel>,
                IEnumerable<CardViewModel>>(cards);

            return result;

沒有針對name 設定的地方
謝謝

念戀 iT邦新手 5 級 ‧ 2024-01-15 09:33:16 檢舉

由上方提供的資訊無法判斷是什麽原因
若資料有問題應該可以看看原本SQL是怎麽寫的
當輸入資料后 完整是sql是什麽樣子 如果按照原作者的寫法
sql
在Name判斷后填入的資料不是name
給你參考一下

noway iT邦研究生 3 級 ‧ 2024-01-15 21:48:33 檢舉

您好:
這一段 錯了
if (string.IsNullOrWhiteSpace(condition.Name) is false)
{
sqlQuery.Add($" Name LIKE @Name ");
//parameter.Add("Name", $"%{condition.MaxHealth}%");
parameter.Add("Name", $"%{condition.Name}%");
}

改過,就可了
謝謝

我要發表回答

立即登入回答