Skip to content

Commit

Permalink
Add sample docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitrietataru committed May 5, 2023
1 parent b9a857e commit c6ea693
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<BarEntity, BarDto>
{
}
```

### Two-way Mapping Profile
``` csharp
record FooEntity(Guid Id, string Value);
record FooDto(Guid Id, string Value);

class FooMappingProfile : TwoWayProfile<FooEntity, FooDto>
{
}
```

### 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<TransactionEntity, TransactionDto>
{
public override void ConfigureLeftToRightMapping()
{
CreateMap<TransactionEntity, TransactionDto>()
.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<TransactionDto, TransactionEntity>()
.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).

0 comments on commit c6ea693

Please sign in to comment.