首页 文章

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

提问于
浏览
2

考虑存储在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 回答

  • 1

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

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

    然后只是正常查询 .

相关问题