带有别名的Nhibernate投影到嵌套属性

时间:2015-04-07 07:58:16

标签: nhibernate nhibernate-projections

我有这样的DTO课程:

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; } }
}

我有一个像这样的投影列表:

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),
};

并使用query.Select(projections.ToArray()).TransformUsing(Transformers.AliasToBean<Aggregates>();

转换查询

在运行时,TransformUsing()会抛出异常:

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

这是非常期待的,因为NHibernate从成员表达式中选择属性的名称(没有前导成员访问权限)。 那么,我如何设置投影,这将填充属性Aggregates类的属性?

0 个答案:

没有答案