1

I'm having difficulty using AutoMapper to convert Object from Nhibernate queries into my DTO in the following conf

Let's say I have 4 class.

    class A
{
    //some fields of built-in type
}
abstract class B //Some class derived this one, but this is not important here
{
    //some fields of built-in type
    public A refA { get; set; }
}
class C
{
    //some fields of built-in type
    public B refB { get; set; }
}
class D
{
    //some fields of built-in type
    public B refC { get; set; }
}

I use AutoMapper to convert it to my DTO, lets assume for simplicity here that the DTO is an exact copy of these class. I want to send this through the wire, so before serializing, I ask AutoMapper to convert it in the DTO corresponding to the D-Class.

If I make these Object and configure the field my-self, when I call

Mapper.Map<T1,T2>(T1 source)

This is working. So my configuration AutoMap is working. More its also working with

Mapper.Map<IList<T1>,List<T2>

Very well.

Now I make these object, I put them in a Database and call a request to my SQL DB with Nhibernate to retrieve an IList (List of class D).

If I now try to convert it in DTO, it doesnt work anymore. I trace the code in AutoMap, it maps correctly all the built-in type field in class D and then it comes to the refC and here it crash somewhere.

I know about lazy-loading and the fact that Nhibernate just gimme a proxy of my ref to class C but I dont see how to solve this. Just so you know the NHibernateUtil.IsInitialized(refC) is true

Many Thanks

qwark
  • 493
  • 1
  • 4
  • 15
  • I used this SO post : http://stackoverflow.com/questions/11816732/copying-nhibernate-poco-to-dto-without-triggering-lazy-load-or-eager-load And had a look at what romans posted on github (in comments of the first answer: https://github.com/RomansWorks ) I improved his code to suits for my more general case. If someone interested, just ask me. It uses the ValueInjecter to resolve mapping with almost no configuration (all generic-reflec). – qwark Jan 29 '14 at 00:48

2 Answers2

2

You will have to unproxy your entities before passing it to automapper. This is basically the same issue as if you would run a Json serialization.

You can use

Session.GetSessionImplementation().PersistenceContext.Unproxy();

to unproxy something.

Or you disable lazy loading.

Or you do not use automapper and instead use standard transformations... e.g.

.Query().Select(p => new SomeDto(){ PropA = p.PropA, ...});
MichaC
  • 13,104
  • 2
  • 44
  • 56
  • I cant disable Lazy-Load because my DTO doesnt contain some huge lists of elements that each class contains. I just want some fields, so if I disable this, it will load all these useless lists. I don't like the unproxy way as it will like a no lazy-load thing and also I have to pass the Session around. So I guess I'll start to hard-code this myself. – qwark Jan 27 '14 at 18:03
0

You can also use another standard way:

resultSet = session.CreateCriteria(typeof(DataObject))
    .Add(query criteria, etc.)
    .SetResultTransformer(Transformers.AliasToBean<DTOObject>())
    .List<IDTOObject>()

Basically you don't have to iterate all of the props. It is enough to be the same all class props between your DTO and data object.

Draken
  • 3,134
  • 13
  • 34
  • 54