2

I am using mapster to map between entities and DTOs. I have noticed it will not map a Guid? to a Guid (or vice-versa) even if I add it explicitely to a mapping register like such :

public sealed class HostConversionMappingRegister : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<Guid, Guid?>()
            .Map(dest => dest, src => src);

        config.NewConfig<HostConversionResponse, CreateHostConversionRequest>()
            .Map(dest => dest.OldExternalSystemID, src => src.OldExternalSystem.Id!)
            .Map(dest => dest.NewExternalSystemID, src => src.NewExternalSystem.Id!);
}

}

It maps all the other fields just fine (they are not specified in the mapping register as they as simple strings and enums and the property names are the same). Sadly I have to leave the nullability of each Guid field the way it is and I am stuck manually mapping Guids where there is a nullability difference between source and destination. Is there a way around this?

Martin
  • 1,977
  • 5
  • 30
  • 67
  • If you map from `Guid?` to `Guid` and `Guid?` is `null`, what value should `Guid` have? – mamen Feb 15 '23 at 14:01

2 Answers2

1

When I test I don't see any problems when mapping between Guid? and Guid.

Here is an example:

internal class HostConversionResponse
{
    public string? Name { get; set; }

    public Guid? Id { get; set; }
}

internal class CreateHostConversionRequest
{
    public string Name { get; set; }

    public Guid Id { get; set; }
}

var source = new HostConversionResponse()
{
    Name = "Some Name",
    Id = Guid.NewGuid(),
};

var destination = source.Adapt<CreateHostConversionRequest>();

Console.WriteLine(destination.Name);
Console.WriteLine(destination.Id);

Result: enter image description here

I am using version 7.3.0 of mapster and .NET 7.0


Check this example and see if it works for you as well.

1

Mapping works if source and destination both are configured as nullable or non-nullable.

Either leaving Guid/Guid? mapping unconfigured or using MapWith() also seems to work.

Given the following classes are mapped with Adapt():

public class A
{
    public Guid? Guid1 { get; set; }
    public Guid Guid2 { get; set; }
}

public class B
{
    public Guid Guid1 { get; set; }
    public Guid? Guid2 { get; set; }
}
var a = new A { Guid1 = null, Guid2 = Guid.NewGuid() };
var b = a.Adapt<A, B>();

Without configuring a mapping for Guid or Guid?, mapping is OK:

config.NewConfig<A, B>()
    .Map(dest => dest, src => src);
{Mapster.Playground.Data.B}
    Guid1: {00000000-0000-0000-0000-000000000000}
    Guid2: {88c0b170-216b-45ed-a1f1-a20a661164c0}

With mapping configured for Guid to Guid?, nullable destination is not set:

config.NewConfig<A, B>()
    .Map(dest => dest, src => src);

config.NewConfig<Guid, Guid?>()
    .Map(dest => dest, src => src);
{Mapster.Playground.Data.B}
    Guid1: {00000000-0000-0000-0000-000000000000}
    Guid2: null

With mapping configured for Guid? to Guid?, mapping is OK:

config.NewConfig<A, B>()
    .Map(dest => dest, src => src);

config.NewConfig<Guid?, Guid?>()
    .Map(dest => dest, src => src);
{Mapster.Playground.Data.B}
    Guid1: {00000000-0000-0000-0000-000000000000}
    Guid2: {8efb6258-bbff-4b6c-af5a-fcc960db2ed6}

With mapping configured for Guid to Guid, mapping is OK:

config.NewConfig<A, B>()
    .Map(dest => dest, src => src);

config.NewConfig<Guid, Guid>()
    .Map(dest => dest, src => src);
{Mapster.Playground.Data.B}
    Guid1: {00000000-0000-0000-0000-000000000000}
    Guid2: {a599c8c2-3e1a-4f7e-a6ab-e5bb24fa2d8d}

When using MapWith(), mapping is OK:

config.NewConfig<A, B>()
    .Map(dest => dest, src => src);

config.NewConfig<Guid, Guid?>()
    .MapWith(g => g);
{Mapster.Playground.Data.B}
    Guid1: {00000000-0000-0000-0000-000000000000}
    Guid2: {38bc2b78-8d58-476c-88a9-af82fd6a0be2}
nikstra
  • 556
  • 3
  • 9