NHibernate 获取/自动映射器问题
好的,我现在遇到一个问题,要么是 AutoMapper、我的 NHibernate 查询,要么是 Domain/DTO 设计。
我遇到的问题是,当我进行提取时,例如 ObjectA 包含 ObjectB 的列表,而 ObjectB 具有其父 ObjectA 的属性。当我有一个对 ObjectA 的 ObjectB 属性进行急切获取的查询时,我可以无限地继续 ABAbABAB 等等。
这意味着当我尝试将域对象映射到包含相同交易的 DTOA 时,DTOA 具有 DTOB 列表,并且 DTOB 具有其父 DTOA 的属性。返回此信息时,我的服务超时,因为我相信我正在使用 AutoMapper 将 DomainA 映射到 DTOA,然后因为 DTOA.DTOB.DTOA.DTOB 等已填充其无限序列化。
无论如何,对于我确信是一个古老的经典问题的最佳解决方案是什么,但我正在努力寻找正确的内容来输入我的老朋友谷歌。我可以让 AutoMapper 忽略子级中的父实例,最好是我认为如果我可以让 Nhibernate 获取列表但在父属性上保留代理。最糟糕的解决方案是使用特定场景或特殊逻辑的对象来更改域对象。
任何帮助表示感谢。
编辑 - 代码
映射代码
Mapper.CreateMap<DTOA, DomainA>();
Mapper.CreateMap<DomainA, DTOA>()
.ForMember(dst => dst.AProperty,
opts =>
opts.ResolveUsing<LazyLoadResolver>().FromMember(src => src.AProperty));
域对象 DomainA
/// <summary>
/// Data Transfer Object, object representing a user
/// </summary>
public class DomainA
{
/// <summary>
/// Gets or sets the clans.
/// </summary>
/// <value>The clans.</value>
public virtual IList<DomainB> AProperty{ get; set; }
}
域对象 DomainB
/// <summary>
/// DTO for clan members
/// </summary>
public class DomainB
{
/// <summary>
/// Gets or sets the ID.
/// </summary>
/// <value>The ID.</value>
public virtual int ID { get; set; }
/// <summary>
/// Gets or sets the user.
/// </summary>
/// <value>The user.</value>
public virtual DomainA BProperty{ get; set; }
}
Nhibernate 查询
return session.QueryOver<DomainA>()
.Where(a => a.ID == id)
.Fetch(a=> a.AProperty).Eager
.List<DomainA>().FirstOrDefault();
WCF 服务返回语句
return AutoMapper.Map<DomainA, DTOA>(returnedDomainA);
通过获取运行该查询以及 dto 域结构和自动映射器配置意味着我的服务超时且没有错误,我假设它尝试序列化并且是无限循环,没有获取和列表为空,当然一切都很好
Okay so I have an issue at the moment which is either down to AutoMapper, my NHibernate query or Domain/DTO design.
The problem I have is that when i do a fetch, for example ObjectA contains a list of ObjectB and ObjectB has a property of its parent ObjectA. When I have a query which does an eager fetch on the ObjectB property of my ObjectA then i can go on infinitely forever A.B.A.b.A.B.A.B and so on.
This means that when I try to map the domain object to DTOA which contains the same deal, DTOA has a list of DTOB and DTOB has a property of its parent DTOA. My services time out when returning this because I believe I'm using AutoMapper to map DomainA to DTOA and then because DTOA.DTOB.DTOA.DTOB etc is populated its infinitely serialising.
Anyway whats the best solution to what I'm sure is an age old classic issue but I'm struggling to find the right things to type into my old friend google. Can I get AutoMapper to ignore the parent instance in the child, preferrably even I think if i could get Nhibernate to fetch the list but keep a proxy on the parent property. The worst solution would be a domain object change with objects for specific scenarios or speical logic.
Any help appreciated thanks.
EDIT - CODE
Mapping code
Mapper.CreateMap<DTOA, DomainA>();
Mapper.CreateMap<DomainA, DTOA>()
.ForMember(dst => dst.AProperty,
opts =>
opts.ResolveUsing<LazyLoadResolver>().FromMember(src => src.AProperty));
Domain object DomainA
/// <summary>
/// Data Transfer Object, object representing a user
/// </summary>
public class DomainA
{
/// <summary>
/// Gets or sets the clans.
/// </summary>
/// <value>The clans.</value>
public virtual IList<DomainB> AProperty{ get; set; }
}
Domain object DomainB
/// <summary>
/// DTO for clan members
/// </summary>
public class DomainB
{
/// <summary>
/// Gets or sets the ID.
/// </summary>
/// <value>The ID.</value>
public virtual int ID { get; set; }
/// <summary>
/// Gets or sets the user.
/// </summary>
/// <value>The user.</value>
public virtual DomainA BProperty{ get; set; }
}
Nhibernate query
return session.QueryOver<DomainA>()
.Where(a => a.ID == id)
.Fetch(a=> a.AProperty).Eager
.List<DomainA>().FirstOrDefault();
WCF Service return statement
return AutoMapper.Map<DomainA, DTOA>(returnedDomainA);
Running that query with fetching and that dto domain structure and auto mapper configuration means my services times out with no errors, I assume as its trying to serialise and is endlessly looping, without the fetch and the list being null, all works fine of course
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,我决定使用两个选项来解决此类问题
选项 A
更改用于映射 DomainB 的 AutoMapper 以忽略作为 DomainA 实例的 DomainB 上的属性
这意味着映射将是一个方向而不是双向
选项 B
删除来自 DomainB 的 DomainA 属性!在考虑了我希望系统执行的操作后,我决定删除这些链接回父级的属性
So I've decided that the two options to solve such an issue
Option A
Change the AutoMapper for mapping of DomainB to ignore the property on DomainB that is an instance of DomainA
This would mean the mapping would be one direction and not bidirectional
Option B
Remove the DomainA property from DomainB! After consideration of what I wanted the system to do I've decided to remove these properties linking back to parents