RavenDB - computed properties

I have a document with multiple computed properties. Properties that do not have a setter and can call other methods in my class to return a result, for 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 am using a simple index to search for orders by customer, date and some properties in a vehicle document using a lucene query and a transformer to generate my view models. I've looked at the results of the script and I'm not sure if it will 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 querying these documents?

I simplified GetOrderLinesTotal, in fact, this is a complex method that takes into account many other properties when calculating the total.

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

+3


source to share


2 answers


I realized that this is more of a document design issue than an attempt to do something that RavenDB was not going to do. I simplified my document and used map / shorthand to solve the problem.



+2


source


I think your situation is similar to the problem I ran into once. I am using an attribute JsonIgnore

on my public get object and using a private backing field that I am including in the serialization using the attribute JsonProperty

. You should hopefully apply an idea like this:



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

      

+1


source







All Articles