https://github.com/usausa/automapperfragmentconfig
Code generator for AutoMapper fragment config.
https://github.com/usausa/automapperfragmentconfig
automapper codegenerator mapper source-generator source-generators sourcegenerator
Last synced: 5 months ago
JSON representation
Code generator for AutoMapper fragment config.
- Host: GitHub
- URL: https://github.com/usausa/automapperfragmentconfig
- Owner: usausa
- License: mit
- Created: 2024-04-05T08:08:56.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-09T08:09:36.000Z (5 months ago)
- Last Synced: 2025-05-09T09:24:33.728Z (5 months ago)
- Topics: automapper, codegenerator, mapper, source-generator, source-generators, sourcegenerator
- Language: C#
- Homepage:
- Size: 99.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AutoMapperFragmentConfig
[](https://www.nuget.org/packages/AutoMapperFragmentConfig)
## What is this?
Configure AutoMapper locally.
## Usage
```xml
all
runtime; build; native; contentfiles; analyzers; buildtransitive
```## ASP.NET Core Sample
```csharp
[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();
config.CreateMap()
.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));
}
}
``````csharp
public static partial class AutoMapperExtensions
{
// Extension method generator for fragment config.
[MapConfigExtension]
public static partial void AddFragmentProfile(this IMapperConfigurationExpression expression, IServiceProvider provider);
}
``````csharp
builder.Services.AddSingleton();// Add AutoMapper with fragment config.
builder.Services.AddSingleton(static p => new Mapper(new MapperConfiguration(c => c.AddFragmentProfile(p)), p.GetService));
``````csharp
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;
}
```