- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在标题中提到的映射中遇到了一些问题。以下是详细信息:
class MyDomain
{
public Iesi.Collections.Generic.ISet<SomeType> MySomeTypes{ get; set; }
....
}
class MyDTO
{
public IList<SomeTypeDTO> MySomeTypes{ get; set; }
...
}
Mapper.CreateMap<MyDomain, MyDTO>().ForMember(dto=>dto.MySomeTypes, opt.ResolveUsing<DomaintoDTOMySomeTypesResolver>());
Mapper.CreateMap<MyDTO, MyDomain>().ForMember(domain=>domain.MySomeTypes, opt.ResolveUsing<DTOtoDomainMySomeTypesResolver>());
class DomaintoDTOMySomeTypesResolver: ValueResolver<MyDomain, IList<SomeTypeDTO>>
{
protected override IList<SomeTypeDTO> ResolveCore(MyDomain source)
{
IList<SomeTypeDTO> abc = new List<DemandClassConfigurationDTO>();
//Do custom mapping
return abc;
}
}
class DTOtoDomainMySomeTypesResolver: ValueResolver<MyDTO, Iesi.Collections.Generic.ISet<SomeType>>
{
protected override Iesi.Collections.Generic.ISet<SomeType> ResolveCore(SystemParameterDTO source)
{
Iesi.Collections.Generic.ISet<SomeType> abc = new HashedSet<SomeType>();
//Do custom mapping
return abc;
}
}
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
----> AutoMapper.AutoMapperMappingException : Trying to map Iesi.Collections.Generic.HashedSet`1[SomeType, MyAssembly...] to Iesi.Collections.Generic.ISet`1[SomeType, MyAssembly...]
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
----> System.InvalidCastException : Unable to cast object of type 'System.Collections.Generic.List`1[SomeType]' to type 'Iesi.Collections.Generic.ISet`1[SomeType]
最佳答案
这是因为 AutoMapper 目前不支持 ISet<>
集合属性。它在 ISet<>
的目标属性时起作用已经实例化(不为空),因为 ISet<>
实际上继承自 ICollection<>
,因此 Automapper 可以理解这一点,并将正确地进行集合映射。
当目标属性为 null 并且是接口(interface)类型时,它不起作用。你得到这个错误,因为 automapper 实际上发现它可以从 ICollection<>
分配。所以它使用通用 List<>
实例化属性,这是 automapper 必须创建新集合属性时的默认集合,但是当它尝试实际分配它时,它将失败,因为显然 List<>
不能转换为 ISet<>
对此有三种解决方案:
ISet<>
收藏并希望他们会添加它HashSet<>
.这可能会给 ORM 层带来一些麻烦,但可行 IValueResolver
, 因为提供的基数 ValueResolver
不会让您实例化该属性。这是我使用的代码片段:
public class EntityCollectionMerge : IValueResolver
where TDest : IEntityWithId
where TSource : IDtoWithId
{
public ResolutionResult Resolve(ResolutionResult source)
{
//if source collection is not enumerable return
var sourceCollection = source.Value as IEnumerable;
if (sourceCollection == null) return source.New(null, typeof(IEnumerable));
//if the destination collection is ISet
if (typeof(ISet).IsAssignableFrom(source.Context.DestinationType))
{
//get the destination ISet
var destSet = source.Context.PropertyMap.GetDestinationValue(source.Context.DestinationValue) as ISet;
//if destination set is null, instantiate it
if (destSet == null)
{
destSet = new HashSet();
source.Context.PropertyMap.DestinationProperty.SetValue(source.Context.DestinationValue, destSet);
}
Merge(sourceCollection, destSet);
return source.New(destSet);
}
if (typeof(ICollection).IsAssignableFrom(source.Context.DestinationType))
{
//get the destination collection
var destCollection = source.Context.PropertyMap.GetDestinationValue(source.Context.DestinationValue) as ICollection;
//if destination collection is null, instantiate it
if (destCollection == null)
{
destCollection = new List();
source.Context.PropertyMap.DestinationProperty.SetValue(source.Context.DestinationValue, destCollection);
}
Merge(sourceCollection, destCollection);
return source.New(destCollection);
}
throw new ArgumentException("Only ISet and ICollection are supported at the moment.");
}
public static void Merge(IEnumerable source, ICollection destination)
{
if (source == null) return;
var destinationIds = destination.Select(x => x.Id).ToHashSet();
var sourceDtos = source.ToDictionary(x => x.Id);
//add new or update
foreach (var sourceDto in sourceDtos)
{
//if the source doesnt exist in destionation add it
if (sourceDto.Key (sourceDto.Value));
continue;
}
//update exisiting one
Mapper.Map(sourceDto.Value, destination.First(x => x.Id == sourceDto.Key));
}
//delete entity in destination which were removed from source dto
foreach (var entityToDelete in destination.Where(entity => !sourceDtos.ContainsKey(entity.Id)).ToList())
{
destination.Remove(entityToDelete);
}
}
}
opt => opt.ResolveUsing(new EntitCollectionMerge<Entity,Dto>()).FromMember(x => x.ISetMember)
或者,如果您有很多这样的集合,您可以通过 typeMaps 将它们自动添加到所有集合中。
关于自动映射器将 IList<> 映射到 Iesi.Collections.Generic.ISet<>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8341297/
最近我将 NHibernate 库升级到了最新版本 4.0.3.4000。之后 - 在编译期间我遇到了与“Iesi.Collections.Generic.ISet”相关的问题。从细节中我了解到 -
尝试 nHibernate,它说它找不到 Iesi.Collections。 我可以从哪里得到这个?如果它如此重要,它不应该与 nHibernate 下载一起提供吗? 最佳答案 NHibernate
我有 2 个由 NHibernate 处理的类:AssetGroup、AssetAssetGroup 有一个 ISet _assets 集合。AssetGroup 的构造函数会说 _assets =
我在标题中提到的映射中遇到了一些问题。以下是详细信息: class MyDomain { public Iesi.Collections.Generic.ISet MySomeTypes{ ge
该论坛上有许多与 Iesi.Collections 相关的帖子。我试图寻找我的问题的答案,但未能找到解决方案。 我是 nHibernate 和 Iesi.Collections 的新手。 我有一个客户
我有一个类型的集合: Iesi.Collections.Generic public ISet ItemBindings { get; set; } ItemBinding 是 Domain.Mode
在最新版本的 Iesi.Collections 中缺少 Iesi.Collections.Generic.ISet。似乎有三种选择: 链接哈希集 只读设置 同步集 Iesi.Collections.G
我对 NHibernate 非常陌生,所以如果我在这里遗漏了一些微不足道的东西,我深表歉意。我目前正在阅读 packtpub 的一本名为“NHibernate 3 初学者指南”的书。我主要是按照书中的
我已经尝试解决这个问题一天了,但一无所获,所以我希望有人可能已经解决了这个问题。我找到的最接近解决方案的是 How to simply map an NHibernate ISet to IList
.NET 4 ISet<> HashSet<> 可以替换 NHibernate Iesi.Collections ISet 、 HashSet 吗?我正在使用 CaSTLe 代理和 NHibernat
我是一名优秀的程序员,十分优秀!