Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Mapster.Tool failing with init and derived class

Open Melethainiel opened this issue 5 years ago • 1 comments

Hi,

I have some classes like this :

public class ParameterValue
{
    public Guid Id { get; init; }
    public string? Value { get; init; }
    public DateTime DateTime { get; init; }
    public Guid ParameterId { get; init; }
}

public class CategoryParameterValue : ParameterValue
{
    public Guid CategoryId { get; init; }
}

public class OccurrenceParameterValue : ParameterValue
{
    public Guid OccurrenceId { get; init; }
}

Those classes are mapped to their corresponding Dto model with these settings

[Mapper]
public interface IParameterValueMapper
{
    ParameterValue Map(ParameterValueDtoResponse category);
    ParameterValueDtoRequest Map(ParameterValue category);
}

public class MyRegister : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<ParameterValueDtoResponse, ParameterValue>()
            .Include<CategoryParameterValueDtoResponse, CategoryParameterValue>()
            .Include<OccurrenceParameterValueDtoResponse, OccurrenceParameterValue>();

        config.NewConfig<ParameterValue, ParameterValueDtoRequest>()
            .Include<CategoryParameterValue ,CategoryParameterValueDtoRequest>()
            .Include<OccurrenceParameterValue ,OccurrenceParameterValueDtoRequest>();
    }
}

All this generates a mapper file, but it does not take the init setter into account :

private CategoryParameterValue funcMain1(CategoryParameterValueDtoResponse p4)
{
    if (p4 == null)
    {
        return null;
    }
    CategoryParameterValue result = new CategoryParameterValue();
    
    result.CategoryId = p4.CategoryId;
    result.Id = p4.Id;
    result.Value = p4.Value;
    result.DateTime = p4.DateTime;
    result.ParameterId = p4.ParameterId;
    return result;
    
}

if I remove the derived classes, it's working great with the init :

public ParameterValue Map(ParameterValueDtoResponse p1)
{
    return p1 == null ? null : new ParameterValue ()
    {
        Id = p1.Id,
        Value = p1.Value,
        DateTime = p1.DateTime,
        ParameterId = p1.ParameterId
    };
}

Am I missing something ? Thank you all

Melethainiel avatar Apr 30 '21 07:04 Melethainiel

In order to identify issues that are still active, we are closing issues that we believe are either resolved or are dormant. If your issue is still active then please reopen. Thanks.

stagep avatar Jan 07 '25 17:01 stagep