RavenDB中的索引类型鉴别器和多态类型的属性

时间:2012-08-13 21:19:03

标签: nosql ravendb

考虑存储在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; }
}

存储的实体是ProductPricingStrategy,我希望能够通过价格计算类型以及价格计算特定变量查询集合。例如,我希望获得所有产品定价策略的集合,其中基于成本的价格计算的利润率小于0.2。换句话说,我希望能够查询以下投影:

public class FlattenedProductPricingStrategy
{
  public string PriceCalculationType { get; set; }
  public decimal? FixedPrice { get; set; }
  public decimal? Margin { get; set; }
}

蛮力方法是将更平坦的类层次结构与投影而不是域对象模型直接匹配。从RavenDB中检索并持久化后,展平对象将映射到域对象。我考虑过使用中间对象是出于其他原因,例如能够处理映射类中的所有序列化问题以及具有用于重新分解的缓冲区,但是我已经远离它了。有没有办法避免这个中间对象并直接基于原始对象模型创建索引?

1 个答案:

答案 0 :(得分:1)

您需要定义一个这样的索引:

from s in docs.ProductPricingStrategy
select new
{
  s.PriceCalculation.Margin,
  s.PriceCalculation.FixedPrice
}

然后只是正常查询。