-
Notifications
You must be signed in to change notification settings - Fork 331
Mapster.Tool
#skip this step if you already have dotnet-tools.json
dotnet new tool-manifest
dotnet tool install Mapster.Tool
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
Mapster.Tool provides 3 commands
- model: generate models from entities
- extension: generate extension methods from entities
- mapper: generate mappers from interfaces
And Mapster.Tool provides following options
- -a: Define input assembly
- -b: Specify base namespace for generating dynamic outputs & namespaces
- -n: Define namespace of generated classes
- -o: Define output directory
- -p: Print full type name (if your DTOs/POCOs having the same name)
- -r: Generate record types instead of POCO types
- -s: Skip generating existing files
add following code to your csproj
file.
<Target Name="Mapster">
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet build" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet tool restore" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster model -a "$(TargetDir)$(ProjectName).dll"" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster extension -a "$(TargetDir)$(ProjectName).dll"" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster mapper -a "$(TargetDir)$(ProjectName).dll"" />
</Target>
to generate run following command on csproj
file directory:
dotnet msbuild -t:Mapster
add following code to your csproj
file.
<Target Name="Mapster" AfterTargets="AfterBuild">
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet tool restore" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster model -a "$(TargetDir)$(ProjectName).dll"" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster extension -a "$(TargetDir)$(ProjectName).dll"" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster mapper -a "$(TargetDir)$(ProjectName).dll"" />
</Target>
add following code to your csproj
file.
<ItemGroup>
<Generated Include="**\*.g.cs" />
</ItemGroup>
<Target Name="CleanGenerated">
<Delete Files="@(Generated)" />
</Target>
to clean up run following command:
dotnet msbuild -t:CleanGenerated
If your POCOs and DTOs have the same name, you might need to generate using full type name, by adding -p
flag.
<Target Name="Mapster">
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet build" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet tool restore" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster model -a "$(TargetDir)$(ProjectName).dll" -p" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster extension -a "$(TargetDir)$(ProjectName).dll" -p" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster mapper -a "$(TargetDir)$(ProjectName).dll" -p" />
</Target>
For example you have following structure.
Sample.CodeGen
- Domains
- Sub1
- Domain1
- Sub2
- Domain2
And if you can specify base namespace as Sample.CodeGen.Domains
<Exec WorkingDirectory="$(ProjectDir)"
Command="dotnet mapster model -a "$(TargetDir)$(ProjectName).dll" -n Sample.CodeGen.Generated -b Sample.CodeGen.Domains" />
Code will be generated to
Sample.CodeGen
- Generated
- Sub1
- Dto1
- Sub2
- Dto2
There are 3 flavors, to generate DTOs and mapping codes
- Fluent API: if you don't want to touch your domain classes, or generate DTOs from domain types in different assembly.
- Attributes: if you would like to keep mapping declaration close to your domain classes.
- Interfaces: if you already have DTOs, and you would like to define mapping through interfaces.
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:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
[...]
Workaround 2
Change your dotnet build
command to dotnet build -p:CopyLocalLockFileAssemblies=true
as follows:
<Target Name="Mapster">
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet build -p:CopyLocalLockFileAssemblies=true" />
[...]
</Target>
- Configuration
- Config inheritance
- Config instance
- Config location
- Config validation & compilation
- Config for nested mapping
- Custom member matching logic
- Constructor mapping
- Before & after mapping
- Setting values
- Shallow & merge mapping
- Recursive & object references
- Custom conversion logic
- Inheritance