如何使用Automapper将所有零int值映射到可为空的int目标的空值?

时间:2021-11-01 10:00:32

I use int? for all my required 'FK' properties in ViewModels. This gives me an easy way of specifying on a Create view model that a value is nullable and must be assigned a value to satisfy the Required attribute.

我用int?在ViewModels中我所需的所有'FK'属性。这为我提供了一种在Create视图模型上指定值可为空的简单方法,并且必须为其指定一个值以满足Required属性。

My problem comes in because I create the domain model entity first, using a domain factory, then map it to the view model. Now, many of the nullable ints in the view model get assigned 0 from non-nullable ints in the domain model. I would prefer not to build the new entity in the view model and only map it back to the domain model to avoid his. What else can I do? i'm sure there is som Automapper voodoo that can help me.

我的问题来了,因为我首先使用域工厂创建域模型实体,然后将其映射到视图模型。现在,视图模型中的许多可为空的int在域模型中从非可空的int中赋值为0。我宁愿不在视图模型中构建新实体,只将其映射回域模型以避免他的。我还可以做些什么?我敢肯定有一个可以帮助我的索马里Automapper伏都教。

2 个解决方案

#1


1  

EDIT: you dont need to do any of this, but i thought i'd leave it here for people looking for a similar solution. really all you have to do is just provide a mapping from int to int? like this: Mapper.Map<int, int?>()

编辑:你不需要做任何这个,但我想我会留在这里寻找类似解决方案的人。你真的需要做的只是提供从int到int的映射?像这样:Mapper.Map () ,int?>

in that case, I believe you could use a custom type converter, which inherits from automappers ITypeConverter. This code works, I've run it through .NET Fiddle:

在这种情况下,我相信你可以使用自定义类型转换器,它继承自自动化器ITypeConverter。这段代码有效,我通过.NET Fiddle运行它:

using System;
using AutoMapper;

public class Program
{
    public void Main()
    {
        CreateMappings();
        var vm = Mapper.Map<MyThingWithInt, MyThingWithNullInt>(new MyThingWithInt());

        if (vm.intProp.HasValue)
        {
            Console.WriteLine("Value is not NULL!");

        }
        else
        {
            Console.WriteLine("Value is NULL!");
        }
    }

    public void CreateMappings() 
    {
        Mapper.CreateMap<int, int?>().ConvertUsing(new ZeroToNullIntTypeConverter ());
        Mapper.CreateMap<MyThingWithInt, MyThingWithNullInt>();
    }


    public class ZeroToNullIntTypeConverter : ITypeConverter<int, int?>
    {
        public int? Convert(ResolutionContext ctx)
        {
           if((int)ctx.SourceValue == 0)
           {
              return null;
           }
            else
           {
               return (int)ctx.SourceValue;
           }
        }
    }

    public class MyThingWithInt
    {
        public int intProp = 0; 
    }

    public class MyThingWithNullInt
    {
        public int? intProp {get;set;}  
    }
}

#2


0  

You can always use the .ForMember() method on your mapping. Something like this:

您始终可以在映射上使用.ForMember()方法。像这样的东西:

Mapper
    .CreateMap<Entity, EntityDto>()
    .ForMember(
        dest => dest.MyNullableIntProperty,
        opt => opt.MapFrom(src => 0)
    );

#1


1  

EDIT: you dont need to do any of this, but i thought i'd leave it here for people looking for a similar solution. really all you have to do is just provide a mapping from int to int? like this: Mapper.Map<int, int?>()

编辑:你不需要做任何这个,但我想我会留在这里寻找类似解决方案的人。你真的需要做的只是提供从int到int的映射?像这样:Mapper.Map () ,int?>

in that case, I believe you could use a custom type converter, which inherits from automappers ITypeConverter. This code works, I've run it through .NET Fiddle:

在这种情况下,我相信你可以使用自定义类型转换器,它继承自自动化器ITypeConverter。这段代码有效,我通过.NET Fiddle运行它:

using System;
using AutoMapper;

public class Program
{
    public void Main()
    {
        CreateMappings();
        var vm = Mapper.Map<MyThingWithInt, MyThingWithNullInt>(new MyThingWithInt());

        if (vm.intProp.HasValue)
        {
            Console.WriteLine("Value is not NULL!");

        }
        else
        {
            Console.WriteLine("Value is NULL!");
        }
    }

    public void CreateMappings() 
    {
        Mapper.CreateMap<int, int?>().ConvertUsing(new ZeroToNullIntTypeConverter ());
        Mapper.CreateMap<MyThingWithInt, MyThingWithNullInt>();
    }


    public class ZeroToNullIntTypeConverter : ITypeConverter<int, int?>
    {
        public int? Convert(ResolutionContext ctx)
        {
           if((int)ctx.SourceValue == 0)
           {
              return null;
           }
            else
           {
               return (int)ctx.SourceValue;
           }
        }
    }

    public class MyThingWithInt
    {
        public int intProp = 0; 
    }

    public class MyThingWithNullInt
    {
        public int? intProp {get;set;}  
    }
}

#2


0  

You can always use the .ForMember() method on your mapping. Something like this:

您始终可以在映射上使用.ForMember()方法。像这样的东西:

Mapper
    .CreateMap<Entity, EntityDto>()
    .ForMember(
        dest => dest.MyNullableIntProperty,
        opt => opt.MapFrom(src => 0)
    );