Nhibernate projection with alias to nested property

1.2k Views Asked by At

I have a DTO class like this:

public class Aggregates
{
    public Aggregates()
    {
        Sales = new StatisticAggregates();
        Refund = new StatisticAggregates();
    }

    public int ChecksAmount { get; set; }
    public StatisticAggregates Sales { get; set; }        
    public StatisticAggregates Refund { get; set; }    
    public int TotalAmount { get { return Sales.Amount - Refund.Amount; } }
    public double TotalPrice { get { return Sales.Price - Refund.Price; } }
}    

public class StatisticAggregates 
{
    public int Amount { get; set; }    
    public double Cash { get; set; }    
    public double Cashless { get; set; }    
    public double Price { get { return Cash + Cashless; } }
}

And I have a projections list like this:

Aggregates alias = null;
new List<IProjection>
{
    Projections.RowCount().WithAlias(() => alias.ChecksAmount),
    Projections.Sum(() => sale.Total.Amount).WithAlias(() => alias.Sales.Amount),    
    Projections.Sum(() => sale.Payment.Cash).WithAlias(() => alias.Sales.Cash),
    Projections.Sum(() => sale.Payment.Cashless).WithAlias(() => alias.Sales.Cashless),
    Projections.Sum(() => sale.Total.RefundAmount).WithAlias(() => alias.Refund.Amount),
    Projections.Sum(() => sale.Refund.Cash).WithAlias(() => alias.Sales.Cash),    
    Projections.Sum(() => sale.Refund.Cashless).WithAlias(() => alias.Sales.Cashless),
};

and transforms query with query.Select(projections.ToArray()).TransformUsing(Transformers.AliasToBean<Aggregates>();

In runtime TransformUsing() throws an Exception:

Could not find a setter for property 'Amount' in class 'Namespace.Domain.Services.Aggregates'

This is quite expected, because of NHibernate select the name of the property from a member expression (without leading member access). So, how can I setup projections, which will fill in property of property Aggregates class?

0

There are 0 best solutions below