2014-09-05 126 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