Skip to content

dimitrietataru/ace-csharp-structured-automapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ace.CSharp.StructuredAutoMapper

build release

Nuget | Ace.CSharp.StructuredAutoMapper Nuget | Ace.CSharp.StructuredAutoMapper.Abstractions Nuget | Ace.CSharp.StructuredAutoMapper.Abstractions.Test

Usage

One-way Mapping Profile

record BarEntity(Guid Id, string Value);
record BarDto(Guid Id, string Value);

class BarMappingProfile : OneWayProfile<BarEntity, BarDto>
{
}

Two-way Mapping Profile

record FooEntity(Guid Id, string Value);
record FooDto(Guid Id, string Value);

class FooMappingProfile : TwoWayProfile<FooEntity, FooDto>
{
}

Two-way Mapping Profile (Explicit)

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

public sealed class BarMappingProfileTests
: BaseOneWayProfileTests<BarMappingProfile, BarEntity, BarDto>
{
protected sealed override Func<BarEntity> Left =>
() => new BarEntity(Guid.NewGuid(), "bar");
protected sealed override Action<BarEntity, BarDto> LeftToRightAssertions =>
(entity, dto) =>
{
// FluentAssertions
dto.Id.Should().Be(entity.Id);
dto.Value.Should().Be(entity.Value);
// xUnit Assertions
Assert.Equal(entity.Id, dto.Id);
Assert.Equal(entity.Value, dto.Value);
};
[Fact]
public sealed override void GivenMapFromLeftToRightWhenSourceIsNotNullThenMapsData()
{
base.GivenMapFromLeftToRightWhenSourceIsNotNullThenMapsData();
}
[Fact]
public sealed override void GivenMapFromLeftToRightWhenSourceIsNullThenHandlesGracefully()
{
base.GivenMapFromLeftToRightWhenSourceIsNullThenHandlesGracefully();
}
[Fact]
public sealed override void GivenMapFromRightToLeftWhenMappingIsNotConfiguredThenThrowsException()
{
base.GivenMapFromRightToLeftWhenMappingIsNotConfiguredThenThrowsException();
}
}
public sealed class FooMappingProfileTests
: BaseTwoWayProfileTests<FooMappingProfile, FooEntity, FooDto>
{
protected sealed override Func<FooEntity> Left =>
() => new FooEntity(Guid.NewGuid(), "foo");
protected sealed override Func<FooDto> Right =>
() => new FooDto(Guid.NewGuid(), "foo-dto");
protected sealed override Action<FooEntity, FooDto>? LeftToRightAssertions =>
(entity, dto) =>
{
dto.Id.Should().Be(entity.Id);
dto.Value.Should().Be(entity.Value);
};
protected sealed override Action<FooDto, FooEntity>? RightToLeftAssertions =>
(dto, entity) =>
{
entity.Id.Should().Be(dto.Id);
entity.Value.Should().Be(dto.Value);
};
[Fact]
public sealed override void GivenMapFromLeftToRightWhenSourceIsNullThenHandlesGracefully()
{
base.GivenMapFromLeftToRightWhenSourceIsNullThenHandlesGracefully();
}
[Fact]
public sealed override void GivenMapFromLeftToRightWhenSourceIsNotNullThenMapsData()
{
base.GivenMapFromLeftToRightWhenSourceIsNotNullThenMapsData();
}
[Fact]
public sealed override void GivenMapFromRightToLeftWhenSourceIsNullThenHandlesGracefully()
{
base.GivenMapFromRightToLeftWhenSourceIsNullThenHandlesGracefully();
}
[Fact]
public sealed override void GivenMapFromRightToLeftWhenSourceIsNotNullThenMapsData()
{
base.GivenMapFromRightToLeftWhenSourceIsNotNullThenMapsData();
}
}
public sealed class TransactionMappingProfileTests
: BaseTwoWayProfileTests<TransactionMappingProfile, TransactionEntity, TransactionDto>
{
protected sealed override Func<TransactionEntity> Left =>
() =>
new TransactionEntity
{
Id = 1,
OperatorId = Guid.Empty,
ProcessedAt = DateTimeOffset.UtcNow,
Amount = 1.2345D
};
protected sealed override Func<TransactionDto> Right =>
() =>
new TransactionDto
{
Id = 1,
ProcessedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
Amount = 9.8765D
};
protected sealed override Action<TransactionEntity, TransactionDto>? LeftToRightAssertions =>
(entity, dto) =>
{
dto.Id.Should().Be(entity.Id);
dto.ProcessedAt.Should().Be(entity.ProcessedAt.ToUnixTimeMilliseconds());
dto.Amount.Should().Be(entity.Amount);
};
protected sealed override Action<TransactionDto, TransactionEntity>? RightToLeftAssertions =>
(dto, entity) =>
{
entity.Id.Should().Be(dto.Id);
entity.OperatorId.Should().BeEmpty();
entity.ProcessedAt.Should().Be(DateTimeOffset.FromUnixTimeMilliseconds(dto.ProcessedAt));
entity.Amount.Should().Be(dto.Amount);
};
[Fact]
public sealed override void GivenMapFromLeftToRightWhenSourceIsNullThenHandlesGracefully()
{
base.GivenMapFromLeftToRightWhenSourceIsNullThenHandlesGracefully();
}
[Fact]
public sealed override void GivenMapFromLeftToRightWhenSourceIsNotNullThenMapsData()
{
base.GivenMapFromLeftToRightWhenSourceIsNotNullThenMapsData();
}
[Fact]
public sealed override void GivenMapFromRightToLeftWhenSourceIsNullThenHandlesGracefully()
{
base.GivenMapFromRightToLeftWhenSourceIsNullThenHandlesGracefully();
}
[Fact]
public sealed override void GivenMapFromRightToLeftWhenSourceIsNotNullThenMapsData()
{
base.GivenMapFromRightToLeftWhenSourceIsNotNullThenMapsData();
}
}

See also

License

AceCSharp.StructuredAutoMapper is Copyright © 2023 Dimitrie Tataru and other contributors under the MIT license.