Dada 劉永琦
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ## [.NET 筆記 014] Mapster: DTO 物件轉換 ###### 📅 2026-04-30 --- ### 📌 介紹-1. 什麼是 DTO? DTO (Data Transfer Object) 白話來說就是「專門用來傳資料的容器」 對應的實際檔案: - Models/Device.cs ➡️ Model (Data Model, 資料模型) | 對應資料庫的表格,是「存資料的容器」 - Dtos/DeviceDto.cs ➡️ DTO | 對應 API 回傳的格式,是「傳資料的容器」 目前範例專案裡有三種長相相似但用途不同的 class: | 名稱 | 白話說明 | 範例 (目前範例專案) | |:-----|:---------|:----------------| | Model (資料模型) | 對應資料庫的表格,完整欄位都在這 | User、Device、Role | | DTO (傳輸物件) | 只放「這支 API 需要回傳的欄位」 | UserDto、DeviceDto | | ViewModel | 只放「前端畫面需要顯示的欄位」 (Web 專案常見) | 目前範例專案 **尚無** | ☠️ 問題:從 Model 轉到 DTO,目前是手動一個欄位一個欄位自己賦值: ```csharp // 目前的寫法 (手動賦值) select new UserDto { Id = u.Id, Account = u.Account, Name = u.Name, Email = u.Email, Pwd = u.Pwd, IsActive = u.IsActive, RoleId = u.RoleId, CreatedDate = u.CreatedDate, UpdatedDate = u.UpdatedDate } ``` 欄位少還好,一旦欄位多或 DTO 很多個,這樣寫既累又容易漏這就是 Mapster 要解決的問題 --- ### 📌 介紹-2. 什麼是 Mapster? Mapster 是一個「自動幫你複製相同名稱欄位」的套件 只要兩個 class 的屬性名稱相同,Mapster 就能自動對應,不用手動一行一行寫 ```csharp // Mapster 的寫法 (一行搞定) var userDto = user.Adapt<UserDto>(); ``` > 💡 `Adapt<T>()` 就是 Mapster 的核心方法,意思是「把我轉換成 T 這個型別」 --- ### 📌 介紹-3. Mapster vs AutoMapper 有另一個類似的套件叫 AutoMapper,以下是兩者比較: | | Mapster | AutoMapper | |:--|:--------|:-----------| | 設定方式 | 預設零設定,名稱相同自動對應 | 需要先寫 Profile 設定檔 | | 速度 | 較快 | 較慢 | | 套件大小 | 較小 | 較大 | | .NET 8 支援 | ✅ | ✅ | | 適合場景 | 欄位名稱一致、快速開發 | 需要大量客製化對應規則 | > 💡 目前範例專案 DTO 欄位名稱與 Model 大多一致,Mapster 零設定就能用,不需要 AutoMapper --- ### 📌 Step-0. 開啟專案 繼上一篇 [[.NET 筆記 013]](https://hackmd.io/@dada00321/SJVshzkRWl) ➡️ API_test_260428 (.NET 8.0) --- ### 📌 Step-1. 安裝 Mapster 套件 在終端機(檢視 > 終端)執行: ``` dotnet add package Mapster --version 7.4.0 ``` 安裝完後,確認 `.csproj` 出現以下內容: ```xml <PackageReference Include="Mapster" Version="7.x.x" /> ``` > 💡 Mapster 7.x 完整支援 .NET 8.0,不需要額外安裝相依套件 --- ### 📌 Step-2. 改寫 UserService 找到 `Services/UserService.cs`,先在頂部加上 using: ```csharp using Mapster; ``` #### UserService/GetAllAsync — 改寫前 ```csharp return await (from u in _context.Users join r in _context.Roles on u.RoleId equals r.Id select new UserDto { Id = u.Id, Account = u.Account, Name = u.Name, Email = u.Email, Pwd = u.Pwd, IsActive = u.IsActive, RoleId = u.RoleId, CreatedDate = u.CreatedDate, UpdatedDate = u.UpdatedDate }).ToListAsync(); ``` #### UserService/GetAllAsync — 改寫後 ```csharp var users = await conn.QueryAsync<User>("SELECT * FROM Users"); return users.Select(u => u.Adapt<UserDto>()).ToList(); // ^^^^^^^^^^^^^^^^ // Mapster 自動把 User 轉成 UserDto ``` #### UserService/GetByIdAsync — 改寫後 ```csharp var user = await conn.QueryFirstOrDefaultAsync<User>( "SELECT * FROM Users WHERE Id = @Id", new { Id = userId }); return user?.Adapt<UserDto>(); // ^^^^^^^^^^^^^^^^ // user 是 null 就回 null,否則自動轉成 UserDto ``` --- ### 📌 Step-3. 改寫 DeviceService 找到 `Services/DeviceService.cs`,同樣加上 `using Mapster`,然後改寫: #### DeviceService/GetAllAsync — 改寫後 ```csharp var devices = await conn.QueryAsync<Device>("SELECT * FROM Devices"); return devices.Select(d => d.Adapt<DeviceDto>()).ToList(); ``` #### DeviceService/GetByIdAsync — 改寫後 ```csharp var device = await conn.QueryFirstOrDefaultAsync<Device>( "SELECT * FROM Devices WHERE Id = @Id", new { Id = deviceId }); return device?.Adapt<DeviceDto>(); ``` #### DeviceService/UpdateAsync — 改寫後 ```csharp // 更新完後,直接把 device 轉成 DeviceDto 回傳 return (await conn.QueryFirstOrDefaultAsync<Device>( "SELECT * FROM Devices WHERE Id = @Id", new { Id = deviceId })) ?.Adapt<DeviceDto>(); ``` --- ### 📌 Step-4. 改寫 RoleService 找到 `Services/RoleService.cs`,加上 `using Mapster`: #### RoleService/GetByIdAsync — 改寫後 ```csharp var role = await conn.QueryFirstOrDefaultAsync<Role>( "SELECT * FROM Roles WHERE Id = @Id", new { Id = roleId }); if (role == null) return null; var roleDto = role.Adapt<RoleDto>(); // Role → RoleDto 自動轉換 roleDto.Devices = await GetDevicesByRoleIdAsync(conn, roleId); // 這個欄位手動補 return roleDto; ``` > 💡 `RoleDto` 有 `Devices` 這個欄位在 `Role` Model 裡沒有,所以這個欄位還是要手動補上,其他欄位交給 Mapster --- ### 📌 Step-5. 驗證結果 改寫完後執行: ``` dotnet build ``` 確認沒有紅字後,跑起來用 Swagger 測試各支 API,回傳結果應與改寫前完全相同 --- ### 📌 總結:改寫前後對比 | | 改寫前 | 改寫後 (Mapster) | |:--|:------|:----------------| | 欄位對應方式 | 手動一個一個賦值 | `Adapt<T>()` 一行搞定 | | 新增欄位時 | DTO 和賦值兩個地方都要改 | 只需改 DTO class | | 漏寫欄位風險 | 有 (不會編譯錯誤) | 無 (自動對應) | | 適用條件 | 任何情況 | 欄位名稱需一致 | > 💡 如果 Model 欄位名稱和 DTO 不同,可以用 `TypeAdapterConfig` 設定自訂對應規則,這屬於 Mapster 進階用法

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully