使用自动映射器将单个对象映射到对象列表 [英] use automapper to map single object into a list of objects

查看:118
本文介绍了使用自动映射器将单个对象映射到对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的映射器中有以下一行:

I have the following line in my mapper:

我正在尝试从一个我拥有一个名为Result的属性的模型映射到一个我有一个结果列表"的模型.

I am trying to map from one model where I have a single property called Result to a model where I have a List of Results.

到目前为止,我有以下内容:

I have the following so far:

options.CreateMap<Contract.Dto.Result, List<Result>>(MemberList.Source).ConvertUsing<ResultConverter>();


internal class ResultConverter : ITypeConverter<Contract.Dto.Result, List<Result>>
    {
        public List<Result> Convert(Contract.Dto.Result source, List<Result> destination, ResolutionContext context)
        {
            destination.Add(context.Mapper.Map<Contract.Dto.Result, Result>(source));
            return destination;
        }
    }

但是,在调试时,永远不会命中ResultConverter. 关于如何从单个对象映射到对象列表,是否有人有任何解决方案?在该对象列表中将永远只有一个对象,但是其他约束阻止我修改模型.

However, when debugging, the ResultConverter is never hit. Does anyone have any solution as to how to map from single object to list of objects? There will only ever be one object in this list of objects but other constraints stop me from amending the models.

推荐答案

我设法在我创建的一个很小的类库中使它正常工作.我注意到的一些位可能会破坏您的代码:

I have managed to get this working in a very small Class Library I created. Bits I noticed that would potentially break your code:

在您要映射的任何地方都没有提及< Contract.Dto.Result,结果>-确保您已经添加了这两个地图

No mention anywhere that you are also mapping < Contract.Dto.Result, Result > - ensure you have added both these maps

var config = new MapperConfiguration(options =>
{
    options.CreateMap<Contract.Dto.Result, List<Result>>(MemberList.Source).ConvertUsing<ResultConverter>();
    options.CreateMap<Contract.Dto.Result, Result>(MemberList.Source);
});

在您的转换器中,我将其更改为使用新的结果列表,而不是目的地

In your converter, I changed this to use a new list of results rather than destination

public List<Result> Convert(Contract.Dto.Result source, List<Result> destination, ResolutionContext context)
{
    var listOfResults = new List<Result>();
    var result = context.Mapper.Map<Contract.Dto.Result, Result>(source);
    listOfResults.Add(result);
    return listOfResults;
}

在实际使用地图时,只需确保您具有正确的语法

When actually using the map, just ensure you have got the correct syntax

var result = new Contract.Dto.Result();
var expected = mapper.Map<List<Result>>(result);

此外,如果使用的是IOC,请确保已注册了连接器.下面的Autofac代码示例

Also, if using IOC, make sure you have registered the conveters. Autofac code example below

 builder.RegisterType<ResultConverter>().AsSelf();
 builder.Register(context => new MapperConfiguration(options=>
 {
     options.CreateMap<Contract.Dto.Result, List<Result>>(MemberList.Source).ConvertUsing<ResultConverter>();
     options.CreateMap<Contract.Dto.Result, Result>(MemberList.Source);
 })).AsSelf().SingleInstance();


 builder.Register(c =>
 {
     //This resolves a new context that can be used later.
     var context = c.Resolve<IComponentContext>();
     var config = context.Resolve<MapperConfiguration>();
     return config.CreateMapper(context.Resolve);
 }).As<IMapper>().InstancePerLifetimeScope();

正如下面评论中提到的OP一样,还要确保使用正确的类型,在这种情况下为List<> vs IList<>

As OP mentioned in comment below, also ensure that the correct types are used, in this instance List<> vs IList<>

在您的项目中尝试所有这些方法,希望能够解决您的问题.如果没有,请告诉我,我可以进一步看看.

Give each of these a try on your project, hopefully that will solve your issues. If not let me know and I can take a further look.

这篇关于使用自动映射器将单个对象映射到对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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