MapsterMapper / Mapster

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

Fix Issue #656. Mapping init Property #658

Open DocSvartz opened 7 months ago

DocSvartz commented 7 months ago

Fix Issue #656

Partial fix issue #422 Fix MapToTarget generation to not Public setters (init Prorperty also working in Mapster 7.4.0) Not Fix AdaptTo - need use .MapToConstructor(true);

example

public class SalesOrder
{
   public SalesOrder(){}
   public SalesOrder(Guid id, decimal total, string status)
    {
        Id = id;
        Total = total;
        Status = status;
    }

    public Guid Id { get; protected set; }
    public decimal Total { get; protected set; }
    public string Status { get; protected set; }
}

Bad generation :

public static SalesOrder AdaptToSalesOrder(this SalesOrderDto p1)
{
    return p1 == null ? null : new SalesOrder()
    {
        Id = p1.Id, 
        Total = p1.Total,
        Status = p1.Status
    };
}

when using .MapToConstructor(true)

Good generation :

public static SalesOrder AdaptToSalesOrder(this SalesOrderDto p1)
{
    return p1 == null ? null : new SalesOrder(p1.Id, p1.Total, p1.Status) {};
}