2014-09-05 132 views
3

我有一个具有几个计算属性的文档。有没有setter和可以拨打我的课等方法属性返回结果,例如:RavenDB - 计算属性

public class Order 
{ 
    public string CustomerId { get; set; } 

    public string VehicleId { get; set; } 

    public DateTime OrderDate { get; set; } 

    public decimal CommissionPercent { get; set; } 

    public List<OrdersLines> OrderLines { get; set; } 

    public decimal Total 
    { 
     get { return GetOrderLinesTotal() + SomeDecimal + AnotherDecimal; } 
    } 

    public decimal GetOrderLinesTotal() 
    { 
     return OrderLines.Sum(x => x.Amount); 
    } 
} 

我用一个简单的索引来搜索使用Lucene查询的客户,日订单和车辆文件的一些属性和一个变压器来创建我的视图模型。我看过脚本索引结果,我不确定它是否适用于这种情况。

public class ViewModel 
{ 
    public string OrderId { get; set; } 
    public string CustomerName { get; set; } 
    public string VehicleName { get; set; } 
    public string Total { get; set; } 
} 

当我查询这些文档时,如何从Total属性中获取计算值?

我简化了GetOrderLinesTotal的一些内容,实际上它是一个复杂的方法,在计算总数时需要考虑很多其他属性。

我只获取创建或更新文档时序列化的计算值。

回答

2

我意识到,这更多的是一个文件设计问题,而不是试图做一些RavenDB并不意味着做。我简化了我的文档并使用map/reduce来解决问题。

+0

你愿意分享你的解决方案吗? – absynce 2016-02-19 19:58:20

1

我认为你的情况类似于我曾经遇到的问题。我在我的公共获得属性上使用JsonIgnore属性,并使用私有后台字段,我使用JsonProperty属性在序列化中包含该字段。你应该能够希望申请一个类似的想法:

/// <summary> 
/// The <see cref="Team" /> class. 
/// </summary> 
public class Team 
{ 
    /// <summary> 
    /// The ids of the users in the team. 
    /// </summary> 
    [JsonProperty(PropertyName = "UserIds")] 
    private ICollection<string> userIds; 

    // ...[snip]... 

    /// <summary> 
    /// Gets the ids of the users in the team. 
    /// </summary> 
    /// <value> 
    /// The ids of the users in the team. 
    /// </value> 
    [JsonIgnore] 
    public IEnumerable<string> UserIds 
    { 
     get { return this.userIds; } 
    } 

    // ...[snip]... 
} 
+0

我不认为这是我有同样的问题。我的问题是属性Total是一个计算属性,它调用了我的类的另一个方法。由于我通过类使用Transformer永远不会实例化,并且不会返回正确的计算值。 – JCoder23 2014-09-05 09:21:47