周尚緯
    • 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

      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.
      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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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

    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.
    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
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 如何在 .NET 使用 AutoMapper [![hackmd-github-sync-badge](https://hackmd.io/DbtaXmo7S-GC3pHbF3wCXg/badge)](https://hackmd.io/DbtaXmo7S-GC3pHbF3wCXg) ## 前言 一個應用程式在設計時通常會進行多層次分層,秉持著關注點分離的原則,一般不會將同一個DTO跨越多個分層進行傳輸,而是在每層間將傳入 DTO 轉換成另一個 DTO 後,傳入下個分層。 舉例來可能會進行這樣的拆分,Entity←(Domain Layer)→Service DTO←(Application Layer)→ViewModel,當要進行資料更新時,Application Layer 將 ViewModel 轉換為 Service DTO 傳入至 Domain Layer 的 Service 當參數,Service 再將 Service DTO 轉換成 Entity 透過 Repository 寫入資料(Entity Framework 本身就是實作 Repository Pattern 的架構),在處理這些 DTO 轉換的屬性/欄位設值是非常麻煩的,所以都會寫映射(Reflaction) API 來簡化操作,而 AutoMapper 是比較常用的一個映射套件,本身[文件](https://docs.automapper.org/en/latest/index.htm)撰寫非常詳細,相對好上手。 ## AutoMapper 的使用方式 AutoMapper的使用方式如下: ```csharp // 建立Configuration設定class之間的映射關係 var config = new MapperConfiguration(cfg => { cfg.CreateMap<Order, OrderDto>(); }); // 驗證Configuration的設置,Destination有的Member,Source一定要有,或是有特別處理,否則會throw AutoMapperConfigurationException config.AssertConfigurationIsValid(); // 建立Mapper var mapper = config.CreateMapper(); // 建立Destination dest,並將source的值映射至dest Destination dest mapper.Map<Destination>(source); // 將source的值值映射至已存在dest mapper.Map(source, dest); ``` ## 設置 正常來說不會有動態變動class之間的映射關係,所以MapperConfiguration只需要建置一份就好,一般會寫在Startup.cs裡,.NET 6則會新C#語法關係,預設沒有Startup.cs,所以就寫在Program.cs裡 ### class 間常用的映射設置方式 ```csharp var config = new MapperConfiguration(cfg => { // 單一型別轉換設置,設定Source可轉換成Destination cfg.CreateMap<Source, Destination>(); // 使用ConvertUsing,用Expression<Func<Source, Destination>>直接定義兩個型別的轉換關係 // 這邊用來將string的值去空白 cfg.CreateMap<string?, string?>() .ConvertUsing(x => x == null ? x : x.Trim()); // 把設置寫在Profile的引用方式 cfg.AddProfile(OtherProfile); }); //... // 把設置單獨寫在class提供別人做引用 public class OtherProfile : Profile { public OtherProfile() { cfg.CreateMap<Source1, Destination1>(); } } ``` ### Property 間常用的映射設置方式 #### 前/後綴詞 ```csharp var config = new MapperConfiguration(cfg => { // 單一class設置,設定Source可轉換成Destination cfg.CreateMap<Source, Destination>(); // 設定來源前綴詞 // 例如:Source.Name->Destination.Name和Source.PrefixName->Destination.Name都支援 // 如果Source同時有Name和PrefixName,則以Source裡先定義的Member優先 cfg.RecognizePrefixes("Prefix"); // 設定來源後綴詞 cfg.RecognizePostfixes("Postfix"); // 設定目標前綴詞 // 例如:Source.Name->Destination.Name和Source.Name->Destination.PrefixName都支援 // 如鬼Destination同時有Name和PrefixName,兩個Member得值都會是Source.Name的值 cfg.RecognizeDestinationPrefixes("Prefix"); // 設定目標後綴詞 cfg.RecognizeDestinationPostfixes("Postfix"); // 預設有加入Get為前綴詞,不想要這個前綴詞則呼叫此API cfg.ClearPrefixes(); }); //...... // RecognizePrefixes引用原則 class Destination { public string Name { get; set; } } // 如果Source.Name定義在前面 class Source { public string Name { get; set; } // Destination.Name為Source.Name的值 public string PrefixName { get; set; } } // 如果Source.PrefixName定義在前面 class Source { public string PrefixName { get; set; } // Destination.Name為Source.PrefixName的 public string Name { get; set; }值 } ```` #### 針對單一 Member 設定 ```csharp var config = new MapperConfiguration(cfg => { cfg.CreateMap<Source, Destination>() // 不映設值給Destination.Prop1 // 通常是在Destination有此Member,Source沒有此Member時使用 .ForMember(desc => desc.Prop1, opt => opt.Ignore()) // Source和Destination名稱不一樣,設定對應關係 .ForMember(dest => dest.DestProp2, opt => opt.MapFrom(src => src.SourceProp2)) // 映射物件時,不從Source給值,而是額外設值 .ForMember(desc => desc.DateProp3, opt => opt.MapFrom(src => DateTime.Now)) // 只有在Source.IntProp4大於等於0才會映設到Destination.IntProp4 .ForMember(dest => dest.IntProp4, opt => opt.Condition(src => (src.IntProp4 >= 0))) // Source.Prop5為Null時,將Destination.Prop5設值為"Other Value",反之從值為Source.Prop5 .ForMember(dest => dest.Prop5, opt => opt.NullSubstitute("Other Value"))); // 如果desc.CreatedTime不為default,則將desc.ModifiedTime設值為現在時間 // 如果desc.CreatedTime為default,則將desc.CreatedTime設值為現在時間 // 主要用於新增與修改寫入不同欄位時使用,作為判斷是否有值的CreatedTime需放置在最後映射 .ForMember(desc => desc.ModifiedTime, opt => { opt.PreCondition((src, desc, context) => desc.CreatedTime != default); opt.MapFrom(src => DateTime.Now); }) .ForMember(desc => desc.CreatedTime, opt => { opt.PreCondition((src, desc, context) => desc.CreatedTime == default); opt.MapFrom(src => DateTime.Now); }); }); class Source { public string? SourceProp2 { get; set; } public int IntProp4 { get; set; } public string? Prop5 { get; set; } } public class Destination { public string? Prop1 { get; set; } public string? Prop2 { get; set; } public DateTime DateProp3 { get; set; } public int IntProp4 { get; set; } public string? Prop5 { get; set; } public DateTime CreatedTime { get; set; } public DateTime? ModifiedTime { get; set; } } ```` #### 反向映射 如果希望Source和Destination兩種型別可以互轉,可以用以下兩種寫法 ```csharp // 分別定義Source和Destination相互間的轉換關係 var config = new MapperConfiguration(cfg => { cfg.CreateMap<Source, Destination>(); cfg.CreateMap<Destination, Source>(); }); //... // 使用反向映射 var config = new MapperConfiguration(cfg => { cfg.CreateMap<Source, Destination>() .ReverseMap(); }); ``` 使用反向映射有兩個注意的地方,在使用需要評估一下 1. 如果複雜的轉換,需要設置ForPath來定義反向轉換關係。 2. AssertConfigurationIsValid()在反向映射不起作用。 ```csharp var config = new MapperConfiguration(cfg => { cfg.CreateMap<Source, Destination>() .ForMember(dest => dest.Prop2, opt => opt.MapFrom(src => src.Prop1)) .ForMember(dest => dest.Prop5, opt => opt.MapFrom(src => src.Prop3 + "," + src.Prop4)) .ReverseMap() .ForPath(s => s.Prop3, opt => opt.MapFrom(src => src.Prop5.Split(new char[] { ',' })[0])) .ForPath(s => s.Prop4, opt => opt.MapFrom(src => src.Prop5.Split(new char[] { ',' })[1])); }); var source = new Destination { Prop2 = "123", Prop5 = "111,222" }; Source dest = mapper.Map<Source>(source); // dest.Prop1 = "123" 此對應關係較為單純,所以不用設定ForPath也可以反向轉換 // dest.Prop3 = "111" 如果沒設定ForPath會為null // dest.Prop4 = "222" 如果沒設定ForPath會為null public class Source { public string? Prop1 { get; set; } public string? Prop3 { get; set; } public string? Prop4 { get; set; } } public class Destination { public string? Prop2 { get; set; } public string? Prop5 { get; set; } } ``` ## Dependency Injection 需額外安裝NuGet套件[AutoMapper.Extensions.Microsoft.DependencyInjection](https://www.nuget.org/packages/AutoMapper.Extensions.Microsoft.DependencyInjection/)。 .NET Core 3.x Startup.cs ```csharp public void ConfigureServices(IServiceCollection services) { // 使用Assembly註冊 services.AddAutoMapper(profileAssembly1, profileAssembly2 /*, ...*/); // 使用型別註冊該型別所屬Assembly services.AddAutoMapper(typeof(ProfileTypeFromAssembly1), typeof(ProfileTypeFromAssembly2) /*, ...*/); } ``` .NET 6 Program.cs(預設不使用Startup時的寫法) ```csharp // 使用Assembly註冊 builder.Services.AddAutoMapper(profileAssembly1, profileAssembly2 /*, ...*/); // 使用型別註冊該型別所屬Assembly builder.Services.AddAutoMapper(typeof(ProfileTypeFromAssembly1), typeof(ProfileTypeFromAssembly2) /*, ...*/); ``` AutoMapper 注入至Service或Controller ```csharp public class EmployeesController { private readonly IMapper mapper; public EmployeesController(IMapper mapper) => this.mapper = mapper; } ``` ###### tags: `.NET` `AutoMapper`

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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