|
7 | 7 | [](https://www.nuget.org/packages/AceCSharp.StructuredAutoMapper.Abstractions)
|
8 | 8 | [](https://www.nuget.org/packages/AceCSharp.StructuredAutoMapper.Abstractions.Test)
|
9 | 9 |
|
| 10 | +## Usage |
| 11 | + |
| 12 | +### One-way Mapping Profile |
| 13 | +``` csharp |
| 14 | +record BarEntity(Guid Id, string Value); |
| 15 | +record BarDto(Guid Id, string Value); |
| 16 | + |
| 17 | +class BarMappingProfile : OneWayProfile<BarEntity, BarDto> |
| 18 | +{ |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +### Two-way Mapping Profile |
| 23 | +``` csharp |
| 24 | +record FooEntity(Guid Id, string Value); |
| 25 | +record FooDto(Guid Id, string Value); |
| 26 | + |
| 27 | +class FooMappingProfile : TwoWayProfile<FooEntity, FooDto> |
| 28 | +{ |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +### Two-way Mapping Profile (Explicit) |
| 33 | +``` csharp |
| 34 | +class TransactionEntity |
| 35 | +{ |
| 36 | + public int Id { get; set; } |
| 37 | + public Guid OperatorId { get; set; } |
| 38 | + |
| 39 | + public DateTimeOffset ProcessedAt { get; set; } |
| 40 | + public double Amount { get; set; } |
| 41 | +} |
| 42 | + |
| 43 | +class TransactionDto |
| 44 | +{ |
| 45 | + public int Id { get; set; } |
| 46 | + |
| 47 | + public long ProcessedAt { get; set; } |
| 48 | + public double Amount { get; set; } |
| 49 | +} |
| 50 | + |
| 51 | +class TransactionMappingProfile : TwoWayProfile<TransactionEntity, TransactionDto> |
| 52 | +{ |
| 53 | + public override void ConfigureLeftToRightMapping() |
| 54 | + { |
| 55 | + CreateMap<TransactionEntity, TransactionDto>() |
| 56 | + .ForMember( |
| 57 | + dto => dto.Id, |
| 58 | + options => options.MapFrom(entity => entity.Id)) |
| 59 | + .ForMember( |
| 60 | + dto => dto.ProcessedAt, |
| 61 | + options => options.MapFrom(entity => entity.ProcessedAt.ToUnixTimeMilliseconds())) |
| 62 | + .ForMember( |
| 63 | + dto => dto.Amount, |
| 64 | + options => options.MapFrom(entity => entity.Amount)); |
| 65 | + } |
| 66 | + |
| 67 | + public override void ConfigureRightToLeftMapping() |
| 68 | + { |
| 69 | + CreateMap<TransactionDto, TransactionEntity>() |
| 70 | + .ForMember( |
| 71 | + entity => entity.Id, |
| 72 | + options => options.MapFrom(dto => dto.Id)) |
| 73 | + .ForMember( |
| 74 | + entity => entity.OperatorId, |
| 75 | + options => options.Ignore()) |
| 76 | + .ForMember( |
| 77 | + entity => entity.ProcessedAt, |
| 78 | + options => options.MapFrom(dto => DateTimeOffset.FromUnixTimeMilliseconds(dto.ProcessedAt))) |
| 79 | + .ForMember( |
| 80 | + entity => entity.Amount, |
| 81 | + options => options.MapFrom(dto => dto.Amount)); |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Tests |
| 87 | +https://github.com/dimitrietataru/ace-csharp-structured-automapper/blob/ace/src/sample/Ace.CSharp.StructuredAutoMapper.Sample.Tests/BarMappingProfileTests.cs#L8-L43 |
| 88 | +https://github.com/dimitrietataru/ace-csharp-structured-automapper/blob/ace/src/sample/Ace.CSharp.StructuredAutoMapper.Sample.Tests/FooMappingProfileTests.cs#L8-L54 |
| 89 | +https://github.com/dimitrietataru/ace-csharp-structured-automapper/blob/ace/src/sample/Ace.CSharp.StructuredAutoMapper.Sample.Tests/TransactionMappingProfileTests.cs#L8-L70 |
| 90 | + |
| 91 | +### See also |
| 92 | +* [AutoMapper](https://github.com/AutoMapper/AutoMapper) |
| 93 | +* [FluentAssertions](https://github.com/fluentassertions/fluentassertions) |
10 | 94 |
|
11 | 95 | ### License
|
12 | 96 | AceCSharp.StructuredAutoMapper is Copyright © 2023 [Dimitrie Tataru](https://github.com/dimitrietataru) and other contributors under the [MIT license](https://github.com/dimitrietataru/ace-csharp-structured-automapper/blob/ace/LICENSE).
|
0 commit comments