Skip to content

usausa/AutoMapperFragmentConfig

Repository files navigation

AutoMapperFragmentConfig

NuGet Badge

What is this?

Configure AutoMapper locally.

Usage

  <ItemGroup>
    <PackageReference Include="AutoMapperFragmentConfig" Version="0.2.0" />
    <PackageReference Include="AutoMapperFragmentConfig.SourceGenerator" Version="0.2.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

ASP.NET Core Sample

[ApiController]
[Route("[controller]/[action]")]
public class ExampleController : ControllerBase
{
    private readonly IMapper mapper;

    // AutoMapper settings used only in the Controller can be configured locally without creating Profile class.
    [MapConfig]
    public static void ConfigureMapping(IProfileExpression config, IServiceProvider provider)
    {
        var calc = provider.GetRequiredService<ICalc>();
        config.CreateMap<Request, Response>()
            .ForMember(d => d.Value, o => o.MapFrom(s => calc.Calc(s.Value)));
    }

    public ExampleController(IMapper mapper)
    {
        this.mapper = mapper;
    }

    [HttpPost]
    public IActionResult Execute([FromBody] Request request)
    {
        return Ok(mapper.Map<Request, Response>(request));
    }
}
public static partial class AutoMapperExtensions
{
    // Extension method generator for fragment config.
    [MapConfigExtension]
    public static partial void AddFragmentProfile(this IMapperConfigurationExpression expression, IServiceProvider provider);
}
builder.Services.AddSingleton<ICalc, IncrementCalc>();

// Add AutoMapper with fragment config.
builder.Services.AddSingleton<IMapper>(static p => new Mapper(new MapperConfiguration(c => c.AddFragmentProfile(p)), p.GetService));
public class Request
{
    public int Value { get; set; }
}

public class Response
{
    public string Value { get; set; } = default!;
}

public interface ICalc
{
    int Calc(int value);
}

public class IncrementCalc : ICalc
{
    public int Calc(int value) => value + 1;
}