使用 AutoMapper 映射 IList<TSource>到 (Iesi.Collections.Generic) ISet<TDestination> [英] Using AutoMapper to Map IList&lt;TSource&gt; to (Iesi.Collections.Generic) ISet&lt;TDestination&gt;

查看:25
本文介绍了使用 AutoMapper 映射 IList<TSource>到 (Iesi.Collections.Generic) ISet<TDestination>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试解决这个问题一天了,但无处可去,所以我希望有人之前可能已经解决了这个问题.我找到的最接近解决方案的是 How to map an NHibernate ISet to IList usingAutoMapper通过 AutoMapper 将 IList 映射到 ICollection 但仍然没有乐趣.

我有一个如下所示的数据对象:

公共类父级{公共虚拟 ISet<Child>儿童{得到;放;}}

还有一个如下所示的业务对象:

公共类ParentDto{公共 IList儿童{得到;放;}}

使用 AutoMapper 从数据映射到业务工作正常:

<代码>...Mapper.CreateMap();Mapper.CreateMap();...ParentDto 目的地 = CWMapper.Map(source);

但是当我使用从业务到数据的映射时,出现错误:

<代码>...Mapper.CreateMap();Mapper.CreateMap();...父目的地 = CWMapper.Map(source);

<块引用>

无法将System.Collections.Generic.List"类型的对象转换为Iesi.Collections.Generic.ISet"

我添加了自定义映射:

Mapper.CreateMap().ForMember(m => m.Children, o => o.MapFrom(s => ToISet(s.Children)));私有静态 ISetToISet(IEnumerable 列表){Iesi.Collections.Generic.ISet;设置 = 空;如果(列表!= null){set = new Iesi.Collections.Generic.HashedSet();foreach(列表中的 T 项){set.Add(item);}}返回集;}

但我仍然遇到同样的错误.任何帮助将不胜感激!

解决方案

这是因为源和目标泛型类型参数在您映射的源和目标属性中不同.你需要的映射是从IEnumerableISet,可以推广到从IEnumerableISet 而不是 IEnumerableISet.您需要在您的转换函数中考虑到这一点(实际上您在问题标题中有正确的答案......).

ToISet 方法应该类似于下面发布的方法.它也使用 AutoMapper 将 ChildDto 映射到 Child.

私有静态ISetToISet(IEnumerable源){ISet<TDestination>设置 = 空;如果(来源!= null){set = new HashSet();foreach(源中的 TSource 项){set.Add(Mapper.Map(item));}}返回集;}

然后您可以按如下方式更改地图定义:

Mapper.CreateMap().ForMember(m => m.Children,=>o.MapFrom(p => ToISet(p.Children)));

I have been trying to solve this issue for a day now and have got no where so am hoping someone might have solved this before. The closest I found to a solution was How to simply map an NHibernate ISet to IList using AutoMapper and Map IList to ICollection through AutoMapper but still no joy.

I have a data object that looks like:

public class Parent
{
   public virtual ISet<Child> Children  {get; set; }
}

And a business object that looks like:

public class ParentDto
{
   public IList<ChildDto> Children  {get; set; }
}

Using AutoMapper to map from Data to Business works fine:

...
Mapper.CreateMap<Parent, ParentDto>();
Mapper.CreateMap<Child, ChildDto>();
...

ParentDto destination = CWMapper.Map<Parent, ParentDto>(source);

But when I come to Mapping from Business to Data I get the error:

...
Mapper.CreateMap<ParentDto, Parent>();
Mapper.CreateMap<ChildDto, Child>();
...

Parent destination = CWMapper.Map<ParentDto, Parent>(source);

Unable to cast object of type 'System.Collections.Generic.List' to ''Iesi.Collections.Generic.ISet'

I added a custom mapping:

Mapper.CreateMap<ParentDto, Parent>()
      .ForMember(m => m.Children, o => o.MapFrom(s => ToISet<ChildDto>(s.Children)));

private static ISet<T> ToISet<T>(IEnumerable<T> list)
    {
        Iesi.Collections.Generic.ISet<T> set = null;

        if (list != null)
        {
            set = new Iesi.Collections.Generic.HashedSet<T>();

            foreach (T item in list)
            {
                set.Add(item);
            }
        }

        return set;
    }

But I still get the same error. Any help would be greatly apriciated!

解决方案

This is because the source and destination generic type parameters are not the same in the source and target properties that you are mapping. The mapping you need is from IEnumerable<ChildDto> to ISet<Child>, which can be generalized to a mapping from IEnumerable<TSource> to ISet<TDestination> and not IEnumerable<T> to ISet<T>. You need to take this into account in your conversion function (actually you have the correct answer in the title of your question..).

The ToISet method should be something like the one posted below. It uses AutoMapper as well to map ChildDto to Child.

private static ISet<TDestination> ToISet<TSource, TDestination>(IEnumerable<TSource> source)
{
    ISet<TDestination> set = null;
    if (source != null)
    {
        set = new HashSet<TDestination>();

        foreach (TSource item in source)
        {
            set.Add(Mapper.Map<TSource, TDestination>(item));
        }
    }
    return set;
}

You can then change the map definition as follows:

Mapper.CreateMap<ParentDto, Parent>().ForMember(m => m.Children,
          o => o.MapFrom(p => ToISet<ChildDto, Child>(p.Children)));

这篇关于使用 AutoMapper 映射 IList<TSource>到 (Iesi.Collections.Generic) ISet<TDestination>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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