79382336

Date: 2025-01-23 19:29:38
Score: 0.5
Natty:
Report link

Just started learning Mapster. From this article I learned that the generation of extension methods for existing entities is triggered using the GenerateMapper method, which is located in a custom configuration class that derives from IRegister.

In short:

  1. Add the “dotnet mapster extension” command to the *.csproj file, for example:
<ItemGroup>
    <PackageReference Include="Mapster" Version="7.4.2-pre02"/>
</ItemGroup>

<Target Name="Mapster" AfterTargets="AfterBuild">
    <Exec WorkingDirectory="$(ProjectDir)" Command="dotnet tool restore" />
    <Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster extension -a &quot;$(TargetDir)$(ProjectName).dll&quot;" />
    <Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster mapper -a &quot;$(TargetDir)$(ProjectName).dll&quot;" />
</Target>
  1. Create a configuration class that derives from IRegister:
public class MapperConfig : IRegister {
    private const MapType MapAll = MapType.Map | MapType.MapToTarget | MapType.Projection;

    public void Register(TypeAdapterConfig config) {

        config.NewConfig<Poco, Dto>()
            .TwoWays()
            .GenerateMapper(MapAll);
    }
}
  1. Run Build.
Reasons:
  • Blacklisted phrase (1): this article
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Максим Гришкин