Mapster在C#中未将派生类映射到List中。

huangapple go评论103阅读模式
英文:

Mapster not mapping derived class in a List c#

问题

I have multiple classes implementing a base class, e.g.:

public abstract class Step
{
    public StepId Id {get; set;}
    public string Text {get; set;
    public StepType Type {get;set;}
}

public class SelectionStep : Step 
{
    public List<string> Selections {get; set;}
}

public class NumericStep : Step 
{
}

With the following Mapster configs:

config.NewConfig<SelectionStep, StepResponse>()
    .Map(dest => dest.Selections, src => src.Selections.Select(x => x.Value));

config.NewConfig<Step, StepResponse>()
    .Map(dest => dest.Id, src => src.Id.Value.ToString())
    .Map(dest => dest.Type, src => src.Type.ToString());

When I map from one SelectionStep to StepResponse it's working
_mapper.Map<StepResponse>(SelectionStep)

When I map from List<Step> containing both SelectionStep and NumericStep models, it does not use the SelectionStepConfig.
_mapper.Map<List<StepResponse>>(List<Step>)

英文:

I have multiple classes implementing a base class, e.g.:

public abstract class Step
{
    public StepId Id {get; set;}
    public string Text {get; set;
    public StepType Type {get;set;}
}

public class SelectionStep : Step 
{
    public List&lt;string&gt; Selections {get; set;}
}

public class NumericStep : Step 
{
}

With the following Mapster configs:

config.NewConfig&lt;SelectionStep, StepResponse&gt;()
    .Map(dest =&gt; dest.Selections, src =&gt; src.Selections.Select(x =&gt; x.Value));

config.NewConfig&lt;Step, StepResponse&gt;()
    .Map(dest =&gt; dest.Id, src =&gt; src.Id.Value.ToString())
    .Map(dest =&gt; dest.Type, src =&gt; src.Type.ToString());

When I map from one SelectionStep to StepResponse it's working
_mapper.Map&lt;StepResponse&gt;(SelectionStep)

When I map from List&lt;Step&gt; containing both SelectionStep and NumericStep models, it does not use the SelectionStepConfig.
_mapper.Map&lt;List&lt;StepResponse&gt;&gt;(List&lt;Step&gt;)

答案1

得分: 0

我找到了解决方案。

我将我的 Step 配置更改为包括 SelectionStep,请参见下面的新配置。

config.NewConfig&lt;SelectionStep, StepResponse&gt;()
    .Map(dest =&gt; dest.Selections, src =&gt; src.Selections.Select(x =&gt; x.Value));

config.NewConfig&lt;Step, StepResponse&gt;()
    .Include&lt;SelectionStep, StepResponse&gt;()
    .Map(dest =&gt; dest.Id, src =&gt; src.Id.Value.ToString())
    .Map(dest =&gt; dest.Type, src =&gt; src.Type.ToString());
英文:

I found the solution.

I changed my Step configuration to include the SelectionStep see below the new configs.

config.NewConfig&lt;SelectionStep, StepResponse&gt;()
    .Map(dest =&gt; dest.Selections, src =&gt; src.Selections.Select(x =&gt; x.Value));

config.NewConfig&lt;Step, StepResponse&gt;()
    .Include&lt;SelectionStep, StepResponse&gt;()
    .Map(dest =&gt; dest.Id, src =&gt; src.Id.Value.ToString())
    .Map(dest =&gt; dest.Type, src =&gt; src.Type.ToString());

huangapple
  • 本文由 发表于 2023年6月6日 16:09:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定