Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Named Tuple Mapping

Open ShSSP opened this issue 2 years ago • 1 comments

As I am seeing, Mapster can't map named tuple to class with the same names. As I understand, it happened because named tuple is just a syntactic sugar. Example: `class A { public int Id {get; set;} public string Name{get; set;} }

(int Id, string Name) tuple = (1, "Name"); var a = tuole.Adapt<A>(); Console.WriteLine(a.Id); //0 Console.WriteLine(a.Name); //null ` Is it possible that one day it will work?

ShSSP avatar Mar 28 '23 14:03 ShSSP

Auto it is not impossible. This name exist only develop time but this will work soon, or it's already working


/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/568
/// </summary>
[TestMethod]
public void TupleAndPoco()
{
    TypeAdapterConfig<A568, (int Id, string Name)>
    .NewConfig()
    .Map(dest => dest.Id, src => src.Id)
    .Map(dest => dest.Name, src => src.Name);

    TypeAdapterConfig<(int Id, string Name),A568>
    .NewConfig()
    .Map(dest => dest.Id, src => src.Id)
    .Map(dest => dest.Name, src => src.Name);

    var pocoTo = new A568() { Name = "Me", Id = 2 };
    var pocoDest = new A568() { Name = "You", Id = 3 };

    (int Id, string Name) tupleTO = (4, "John");
    (int Id, string Name) tupleDest = (5, "Name");

    var _tupleResult = pocoTo.Adapt(tupleDest); // _tupleResult  = {2, Me}
    var _pocoResult = tupleTO.Adapt(pocoDest); // _pocoResult = {4, John}
}

class A568
{
    public int Id { get; set; }
    public string Name { get; set; }
}

DocSvartz avatar Oct 21 '23 13:10 DocSvartz