Mapster.Tool (original) (raw)

Mapster.Tool

Install Mapster.Tool

#skip this step if you already have dotnet-tools.json dotnet new tool-manifest

dotnet tool install Mapster.Tool

Install Mapster

For lightweight dependency, you can just install Mapster.Core.

PM> Install-Package Mapster.Core

However, if you need TypeAdapterConfig for advance configuration, you still need Mapster.

PM> Install-Package Mapster

Commands

Mapster.Tool provides 3 commands

And Mapster.Tool provides following options

csproj integration

Generate manually

add following code to your csproj file.

to generate run following command on csproj file directory:

dotnet msbuild -t:Mapster

Generate automatically on build

add following code to your csproj file.

Clean up

add following code to your csproj file.

to clean up run following command:

dotnet msbuild -t:CleanGenerated

Generate full type name

If your POCOs and DTOs have the same name, you might need to generate using full type name, by adding -p flag.

Dynamic outputs & namespaces

For example you have following structure.

Sample.CodeGen
- Domains
  - Sub1
    - Domain1
  - Sub2
    - Domain2

And if you can specify base namespace as Sample.CodeGen.Domains

Code will be generated to

Sample.CodeGen
- Generated
  - Sub1
    - Dto1
  - Sub2
    - Dto2

Generate DTOs and mapping codes

There are 3 flavors, to generate DTOs and mapping codes

Sample

Troubleshooting

System.IO.FileNotFoundException

If you get an error similar to Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly '...'. The system cannot find the file specified. and you are using Mapster.Tool 8.4.1 or newer, then you can try one of the following workarounds:

Workaround 1

Add <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> to your csproj file as follows:

net8.0 true [...]

Workaround 2

Change your dotnet build command to dotnet build -p:CopyLocalLockFileAssemblies=true as follows:

[...]