use automapper to map single object into a list of objects

眉间皱痕 提交于 2019-12-13 00:22:35

问题


I have the following line in my mapper:

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;
        }
    }

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.


回答1:


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

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);

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();

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.




回答2:


I didn't use automapper ever. You can try this and please let me know if it works:

        //Use this method.
        public void Map(object PreEqual, string PreEqProperty, object PostEqual, string PostEqProperty)
        {
            PreEqual.GetType().GetProperty(PreEqProperty).SetValue(PreEqual, PostEqual.GetType().GetProperty(PostEqProperty).GetValue(PostEqual, null), null);
        }

        //Use Map method somewhere you want, like this:
        //myRefObj is your source object and myRefProp is its property that you want to map to other objects.
        foreach(SomeType item in CollectionOfSomeType)
        {
             Map(item, "myRefProp", myRefObj, "myRefProp");
        }    

I think it will work with primitive types. Can you try and let me know if it works?



来源:https://stackoverflow.com/questions/56987748/use-automapper-to-map-single-object-into-a-list-of-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!