From c6ea69337be410cbaa96df3db9c57e6d01308b07 Mon Sep 17 00:00:00 2001 From: MYT Date: Fri, 5 May 2023 03:26:49 +0300 Subject: [PATCH] Add sample docs --- README.md | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/README.md b/README.md index 47b4107..cbe8b05 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,90 @@ [![Nuget | Ace.CSharp.StructuredAutoMapper.Abstractions](https://img.shields.io/nuget/v/AceCSharp.StructuredAutoMapper.Abstractions)](https://www.nuget.org/packages/AceCSharp.StructuredAutoMapper.Abstractions) [![Nuget | Ace.CSharp.StructuredAutoMapper.Abstractions.Test](https://img.shields.io/nuget/v/AceCSharp.StructuredAutoMapper.Abstractions.Test)](https://www.nuget.org/packages/AceCSharp.StructuredAutoMapper.Abstractions.Test) +## Usage + +### One-way Mapping Profile +``` csharp +record BarEntity(Guid Id, string Value); +record BarDto(Guid Id, string Value); + +class BarMappingProfile : OneWayProfile +{ +} +``` + +### Two-way Mapping Profile +``` csharp +record FooEntity(Guid Id, string Value); +record FooDto(Guid Id, string Value); + +class FooMappingProfile : TwoWayProfile +{ +} +``` + +### Two-way Mapping Profile (Explicit) +``` csharp +class TransactionEntity +{ + public int Id { get; set; } + public Guid OperatorId { get; set; } + + public DateTimeOffset ProcessedAt { get; set; } + public double Amount { get; set; } +} + +class TransactionDto +{ + public int Id { get; set; } + + public long ProcessedAt { get; set; } + public double Amount { get; set; } +} + +class TransactionMappingProfile : TwoWayProfile +{ + public override void ConfigureLeftToRightMapping() + { + CreateMap() + .ForMember( + dto => dto.Id, + options => options.MapFrom(entity => entity.Id)) + .ForMember( + dto => dto.ProcessedAt, + options => options.MapFrom(entity => entity.ProcessedAt.ToUnixTimeMilliseconds())) + .ForMember( + dto => dto.Amount, + options => options.MapFrom(entity => entity.Amount)); + } + + public override void ConfigureRightToLeftMapping() + { + CreateMap() + .ForMember( + entity => entity.Id, + options => options.MapFrom(dto => dto.Id)) + .ForMember( + entity => entity.OperatorId, + options => options.Ignore()) + .ForMember( + entity => entity.ProcessedAt, + options => options.MapFrom(dto => DateTimeOffset.FromUnixTimeMilliseconds(dto.ProcessedAt))) + .ForMember( + entity => entity.Amount, + options => options.MapFrom(dto => dto.Amount)); + } +} +``` + +## Tests +https://github.com/dimitrietataru/ace-csharp-structured-automapper/blob/ace/src/sample/Ace.CSharp.StructuredAutoMapper.Sample.Tests/BarMappingProfileTests.cs#L8-L43 +https://github.com/dimitrietataru/ace-csharp-structured-automapper/blob/ace/src/sample/Ace.CSharp.StructuredAutoMapper.Sample.Tests/FooMappingProfileTests.cs#L8-L54 +https://github.com/dimitrietataru/ace-csharp-structured-automapper/blob/ace/src/sample/Ace.CSharp.StructuredAutoMapper.Sample.Tests/TransactionMappingProfileTests.cs#L8-L70 + +### See also +* [AutoMapper](https://github.com/AutoMapper/AutoMapper) +* [FluentAssertions](https://github.com/fluentassertions/fluentassertions) ### License 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).