3
votes

I have a document with a few computed properties. Properties that have no setter and can call other methods on my class to return a result, example:

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

I use a simple index to search for Orders by customer, date and some properties on the Vehicle document using lucene query and a Transformer to create my view models. I've looked at Scripted Index Results and I'm not sure if it would apply in this case.

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

How do i get the computed value from the Total property when I query these documents?

I've simplified the GetOrderLinesTotal some what, in fact it is a complex method that takes a lot of other properties into account when calculating the total.

I only get the computed value that serialized when the document was created or updated.

2

2 Answers

2
votes

I realized that this is more a of a document design problem rather than trying to do something RavenDB was not meant to do. I simplified my document and used a map/reduce to solve the problem.

1
votes

I think your situation is similar to a problem I once encountered. I use a JsonIgnore attribute on my public get property and use a private backing field, which I include in serialisation using the JsonProperty attribute. You should be able to apply a similar idea hopefully:

/// <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]...
}