-
Notifications
You must be signed in to change notification settings - Fork 380
Mapster.Tool
Nguyen Gia Hoang edited this page Feb 21, 2022
·
20 revisions
#skip this step if you already have dotnet-tools.json
dotnet new tool-manifest
dotnet tool install Mapster.ToolFor 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
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:Mapsteradd 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:CleanGeneratedIf 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.
- 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