开发者

NHibernate fetching/automapper issues

开发者 https://www.devze.com 2023-02-14 21:25 出处:网络
Okay so I have an issue at the moment which is either down to AutoMapper, my NHibernate query or Domain/DTO design.

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<LazyLoadRes开发者_如何转开发olver>().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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号