Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 自动映射:是否映射<;A、 B>;给予<;B、 A>;?_Asp.net Mvc_Automapper - Fatal编程技术网

Asp.net mvc 自动映射:是否映射<;A、 B>;给予<;B、 A>;?

Asp.net mvc 自动映射:是否映射<;A、 B>;给予<;B、 A>;?,asp.net-mvc,automapper,Asp.net Mvc,Automapper,使用Automapper我创建了一个简单的地图: Mapper.CreateMap<MyCustomerDTO, YourCustomerDTO>() Mapper.CreateMap() 我也经常需要走另一条路。我是否需要以另一种方式创建映射,或者Automapper是否会根据上述映射进行推断 Mapper.CreateMap<YourCustomerDTO, MyCustomerDTO>() //Needed? Mapper.CreateMap()//需要吗

使用Automapper我创建了一个简单的地图:

Mapper.CreateMap<MyCustomerDTO, YourCustomerDTO>()
Mapper.CreateMap()
我也经常需要走另一条路。我是否需要以另一种方式创建映射,或者Automapper是否会根据上述映射进行推断

Mapper.CreateMap<YourCustomerDTO, MyCustomerDTO>()   //Needed?
Mapper.CreateMap()//需要吗?

您还需要创建第二个映射。如果一个简单的测试试图在没有第二次映射的情况下运行你的应用程序,将会出现运行时错误

注意关于
.ReverseMap()
的答案


请注意,
.ReverseMap()
用于基本映射。如果需要使用选项(例如特定的
FormMember
映射),则需要创建自定义反向映射。

否。必须创建双向映射。双向映射的好帮手方法可以是:

 protected virtual void ViceVersa<T1, T2>()
        {
            Mapper.CreateMap<T1, T2>();
            Mapper.CreateMap<T2, T1>();
        }
受保护的虚拟void ViceVersa()
{
CreateMap();
CreateMap();
}
然后像这样使用它:

ViceVersa<T1, T2>();
ViceVersa();

我在使用AutoMapper时遇到了同样的问题,@Behnam Esmaili是一个很好的答案,但它可以改进

您可以为
IMapperConfigurationExpression
实现一个扩展方法,该方法将执行此双向映射,还需要两个可选参数(
Action
),在尝试为这两种类型配置映射时将使用该参数

public static class ModelMapper
{
    private static readonly IMapper _mapper;

    static ModelMapper()
    {
        var mapperConfiguration = new MapperConfiguration(config =>
        {
            config.CreateTwoWayMap<CustomerViewModel, Customer>(
                secondExpression: (exp) => exp.ForMember((source) => source.CustomerEmail, opt => opt.MapFrom(src => src.Email)));
        });

        _mapper = mapperConfiguration.CreateMapper();
    }

    public static void CreateTwoWayMap<T, Y>(this IMapperConfigurationExpression config, 
        Action<IMappingExpression<T, Y>> firstExpression = null,
        Action<IMappingExpression<Y, T>> secondExpression = null)
    {
        var mapT = config.CreateMap<T, Y>();
        var mapY = config.CreateMap<Y, T>();

        firstExpression?.Invoke(mapT);
        secondExpression?.Invoke(mapY);
    }

    public static T Map<T>(object model)
    {
        return _mapper.Map<T>(model);
    }
}
公共静态类ModelMapper
{
专用静态只读IMapper\u映射器;
静态模型映射器()
{
var mapperConfiguration=新的mapperConfiguration(配置=>
{
config.CreateTwoWayMap(
第二个表达式:(exp)=>exp.ForMember((source)=>source.CustomerEmail,opt=>opt.MapFrom(src=>src.Email));
});
_mapper=mapperConfiguration.CreateMapper();
}
公共静态void CreateTowayMap(此IMapperConfigurationExpression配置,
Action firstExpression=null,
Action secondExpression=null)
{
var mapT=config.CreateMap();
var mapY=config.CreateMap();
firstExpression?.Invoke(mapT);
第二个表达式?.Invoke(mapY);
}
公共静态T映射(对象模型)
{
返回_mapper.Map(模型);
}
}

上面的实现是实现它的一种方法,但是它可以进行不同的设计。

如果我没有记错,我认为它不会产生编译错误。。。它给出了一个运行时错误?找不到B,A的映射图或是沿着这些线的东西?没错。。。我记得它给出了某种错误。请注意,现在可以使用
ReverseMap
选项“开箱即用”完成此操作。