简体   繁体   English

基于源和目标的 AutoMapper 条件映射

[英]AutoMapper Conditional Mapping based on source and destination

I need to map a UpdateViewModel to its Model .我需要将UpdateViewModel映射到它的Model The Model is Model

public class Model
{
    // ...
    public DateTime? Disabled
}

The UpdateViewModel is UpdateViewModel

public class UpdateViewModel
{
    // ...
    Status Status
}
public enum Status
{
    Disabled,
    Active
}

I now have this code我现在有这个代码

if(uvm.Status == Status.Active)
{
    model.Disabled = null;
}
else
{
    if(model.Disabled == null)
    {
        model.Disabled = DateTime.UtcNow;
    }
}

I want to get this in my Map but i struggle to have an if/else condition and I struggle to set Disabled to null .我想在我的地图中得到这个,但我很难有一个 if/else 条件,我很难将Disabled设置为null

It should cover the following cases:它应涵盖以下情况:

Model.Disabled == null and uvm.Status == Status.Disabled -> Set model.Disabled to DateTime.UtcNow Model.Disabled == null 和 uvm.Status == Status.Disabled -> 将 model.Disabled 设置为 DateTime.UtcNow

Model.Disabled == null and uvm.Status == Status.Active -> Stay with model.Disabled = null Model.Disabled == null 和 uvm.Status == Status.Active -> 保持 model.Disabled = null

Model.Disabled != null and uvm.Status == Status.Disabled -> Stay with current model.Disabled Model.Disabled != null 和 uvm.Status == Status.Disabled -> 保持当前 model.Disabled

Model.Disabled != null and uvm.Status == Status.Active -> Set model.Disabled to null Model.Disabled != null 和 uvm.Status == Status.Active -> 将 model.Disabled 设置为 null

I managed to get this working with an IValueResolver我设法使用 IValueResolver 解决了这个问题

public class DisabledResolver : IValueResolver<UpdateViewModel, Model, DateTime?>
{
    public DateTime? Resolve(UpdateViewModel uvm, Model entity, DateTime? value, ResolutionContext context)
    {
        if(uvm.Status == Status.Active)
        {
            return null;
        }
        else
        {
            if(entity.Disabled == null)
            {
                return DateTime.UtcNow;
            }
            return entity.Disabled;
        }
    }
}

and

CreateMap<UpdateViewModel, Model>()
    .ForMember(model => model.Disabled, opt => opt.MapFrom<DisabledResolver>());

You can create custom configuration rules for mapping between types.您可以为类型之间的映射创建自定义配置规则。 For example,例如,

 var configuration = new MapperConfiguration(cfg => {
                cfg.CreateMap<UpdateViewModel, Model>()
                  .ForMember(dest => dest.Disabled, opt => opt.MapFrom((src,dest)=>
                  {
                      return src.Status switch
                      {
                          Status.Active => null,
                          Status.Disabled when dest.Disabled is null => DateTime.UtcNow,
                          Status.Disabled => dest.Disabled,
                          _ => throw new ArgumentException()
                      };

                  }));
            });


and then接着

var mapper = new Mapper(configuration);
var result = mapper.Map<UpdateViewModel,Model>(uModel,model);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Automapper - 目的地的条件映射 - Automapper - conditional mapping on destination Automapper:基于Source对象和ResolutionContext的条件映射 - Automapper: Conditional mapping based on Source object and ResolutionContext 自动映射:在映射中一起使用源和目标 - Automapper: use source and destination together in mapping C# AutoMapper:在条件映射中验证后通过源值设置目标值 - C# AutoMapper: set destination value by source value after validation in conditional mapping 具有不同源类型的 Automapper 条件映射 - Automapper conditional mapping with different source types AutoMapper条件映射不能与跳过空目标值一起使用 - AutoMapper Conditional Mapping Not Working With Skipping Null Destination Values 从源映射到现有目标时,AutoMapper 不会忽略 List - AutoMapper does not ignore List when mapping from source to existing destination 使用Automapper进行自定义映射,其中目标中的字段是源中两个字段的串联 - Custom mapping with Automapper where a field in destination is the concatenation of two fields in source Automapper - 从源子 object 到目标的映射包括父值 - Automapper - Mapping from source child object to destination is including parent values Automapper 将多个列表从源映射到目标上的单个列表 - Automapper mapping multiple lists from source to single list on destination
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM