使用自定义解析器跳过空值 [英] Skip null values with custom resolver

查看:117
本文介绍了使用自定义解析器跳过空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用自动映射器在我的公共数据合同和我的数据库模型之间进行映射.我创建了一个类,该类自动完成所有合同并创建映射.我唯一的问题是,如果值不为null,则只想将合同中的值映射到DB模型.我在这里查看了其他问题,但看不到使用自定义解析器的示例.

I want to use automapper to map between my public data contracts and my DB models. I have created a class which automatically goes through all the contracts are creates mappings. The only problem I have is that I only want to map values from the contract to the DB model if the value is not null. I have looked at other question on here but cant see examples that use custom resolvers.

这是我的一些代码

var mapToTarget = AutoMapper.Mapper.CreateMap(contract, mappedTo);
foreach (var property in contract.GetProperties().Where(property => property.CustomAttributes.Any(a => a.AttributeType == typeof(MapsToProperty))))
{
  var attribute = property.GetCustomAttributes(typeof(MapsToProperty), true).FirstOrDefault() as MapsToProperty;

  if (attribute == null) continue;

  mapToTarget.ForMember(attribute.MappedToName,
                    opt => 
                        opt.ResolveUsing<ContractToSourceResolver>()
                            .ConstructedBy(() => new ContractToSourceResolver(new MapsToProperty(property.Name, attribute.SourceToContractMethod, attribute.ContractToSourceMethod))));
}


private class ContractToSourceResolver : ValueResolver<IDataContract, object>
{
  private MapsToProperty Property { get; set; }

  public ContractToSourceResolver(MapsToProperty property)
  {
     Property = property;
  }

  protected override object ResolveCore(IDataContract contract)
  {
     object result = null;
     if (Property.ContractToSourceMethod != null)
     {
         var method = contract.GetType()
                    .GetMethod(Property.ContractToSourceMethod, BindingFlags.Public | BindingFlags.Static);
          result = method != null ? method.Invoke(null, new object[] {contract}) : null;
      }
      else
      {
         var property =
                    contract.GetType().GetProperties().FirstOrDefault(p => p.Name == Property.MappedToName);
         if (property != null)
         {
             result = property.GetValue(contract);
         }
      }

      return result;
   }
}

这就是我要使用的方式

AutoMapper.Mapper.Map(dataContract, dbModel)

这当前效果很好,但是如果dataContract中有NULL值,则它将替换dbModel中的现有值,这不是我想要的.如何使AutoMapper忽略空源值?

This currently works great but if there is a NULL value in the dataContract then it will replace the existing value in the dbModel, this is not what I want. How do I make AutoMapper ignore null source values?

编辑

如答案之一所指出的那样

As pointed out in one of the answers there is this

Mapper.CreateMap<SourceType, DestinationType>().ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull));

除了无法从

Mapper.CreateMap(SourceType, DestinationType)

推荐答案

更新:IsSourceValueNull为不从V5开始可用.

UPDATE: IsSourceValueNull is not available starting from V5.

如果要忽略所有具有空值的源属性,可以使用:

If you want all source properties with null values to be ignored you could use:

Mapper.CreateMap<SourceType, DestinationType>()
  .ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull));

否则,您可以为每个成员做类似的事情.

Otherwise, you can do something similar for each member.

阅读.

这篇关于使用自定义解析器跳过空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆