Copy NHibernate POCO to DTO without starting a lazy load or high load

I need to create DTOs from NHibernate POCO objects . The problem is that POCO objects contain dynamic proxies that should not be copied to the DTO. I look forward to downloading all collections and links that I need to transfer in advance, I do not want NHibernate to start downloading reference collections that I did not download in advance.

Several similar questions on SO got answers that:

  • Suggest Session.GetSessionImplementation (). PersistenceContext.Unproxy ();
  • Offer to disable Lazy Loading.

In my case, the first sentence does not matter, because, in my understanding, it causes download impatience to replace the proxy. In fact, it does not even work - it does not delete proxies in my objects. (Any explanation why?)

The second sentence, disabling lazy loading, seems to cause all links and collections to load, basically loading the entire database. My assumption was that if lazy loading is disabled and I did not request a collection, it will not be loaded. (Is it right that NHibernate does not offer such an option?)

I am using NHibernate 3.3.1 with a good configuration.

To repeat my main question, I need to create DTOs that clear proxies copied from POCOs that contain proxies, and I do not want to load data behind these proxies.

, ValueInjecter/AutoMapper, .

# 1:

, , , , - , ValueInjecter. . DTO , POCOs. , POCOs, .

, , , ( , , DTO ). , , .

ValueInjecter, DTO , . , ValueInjecter .

, - , , - ValueInjecter POCO / ?

+3
4

ValueInjecter SmartConventionInjection ( )

, -

:

public class MapPoco: SmartConventionInjection
{
     protected override bool Match(SmartConventionInfo c)
     {
         return c.SourceProp.Name == c.TargetProp.Name;
     }
}
+1

, DTO Linq , O/R Mapper.

.

return from c in customers 
       select new CustomerDTO 
       {
          Name = c.Name , 
          Orders = c.Orders.Select (o => new OrderDTO {...} ) 
       };

, - . , , , , , DTO mem. ( , SQL- ..)

+5

ValueResolver AutoMapper:

/// <summary>
/// ValueResolver that will set NHibernate proxy objects to null, instead of triggering a lazy load of the object
/// </summary>
public class IgnoreNHibernateProxyValueResolver : IValueResolver
{
    public ResolutionResult Resolve(ResolutionResult source)
    {
        var prop = source.Type.GetProperty(source.Context.MemberName).GetValue(source.Value, null);
        var proxy = prop as INHibernateProxy;
        if (proxy != null && proxy.HibernateLazyInitializer.IsUninitialized)
        {
            return source.Ignore();
        }

        return source.New(prop);
    }
}
+2
source

Take a look at the projections in Introduction to QueryOver in NH 3.0

CatSummary summaryDto = null;
IList<CatSummary> catReport =
    session.QueryOver<Cat>()
        .SelectList(list => list
            .SelectGroup(c => c.Name).WithAlias(() => summaryDto.Name)
            .SelectAvg(c => c.Age).WithAlias(() => summaryDto.AverageAge))
        .TransformUsing(Transformers.AliasToBean<CatSummary>())
        .List<CatSummary>();
+1
source

All Articles