Consider the following (simplified) domain model stored in RavenDB:
public abstract class PriceCalculation
{
// omitted for brevity
}
public class CostBasedPriceCalculation : PriceCalculation
{
public decimal Margin { get; private set; }
}
public class FixedPriceCalculation : PriceCalculation
{
public decimal FixedPrice { get; private set; }
}
public class ProductPricingStrategy
{
public string ProductId { get; private set; }
public PriceCalculation PriceCalculation { get; private set; }
}
The stored entity is ProductPricingStrategy and I would like to be able to query the collection by price calculation type as well as price calculation specific variables. For example, I would like to get the set of all product pricing strategies having a cost based price calculation with margin less than 0.2. In other words, I would like to be able to query the following projection:
public class FlattenedProductPricingStrategy
{
public string PriceCalculationType { get; set; }
public decimal? FixedPrice { get; set; }
public decimal? Margin { get; set; }
}
The brute force approach would be to store a flattened class hierarchy more closely matching the projection instead of the domain object model directly. Upon retrieval from and persistence to RavenDB the flattened object would be mapped to the domain object. I’ve considered using an intermediate object for other reasons, such as being able to handle all serialization concerns in the mapped class as well as having a buffer zone for re-factoring, however I’ve been steered away from it. Is there a way to avoid this intermediate object and create an index based on the original object model directly?
You need to define an index like this:
And then just query it normally.