MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.29k stars 327 forks source link

Named Tuple Mapping #568

Open ShSSP opened 1 year ago

ShSSP commented 1 year ago

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(); Console.WriteLine(a.Id); //0 Console.WriteLine(a.Name); //null ` Is it possible that one day it will work?

DocSvartz commented 10 months ago

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; }
}