Nosql 指数型判别器与RavenDB多态型的性质

Nosql 指数型判别器与RavenDB多态型的性质,nosql,ravendb,Nosql,Ravendb,考虑存储在RavenDB中的以下(简化)域模型: public abstract class PriceCalculation { // omitted for brevity } public class CostBasedPriceCalculation : PriceCalculation { public decimal Margin { get; private set; } } public class FixedPriceCalculation : PriceCalcula

考虑存储在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检索并持久化到RavenDB后,展平对象将映射到域对象。我考虑过使用中间对象还有其他原因,比如能够处理映射类中的所有序列化问题,以及有一个用于重新分解的缓冲区,但我一直在回避它。有没有办法避免这个中间对象,直接基于原始对象模型创建索引?

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

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

然后正常查询。

谢谢。有没有办法按价格计算类型进行查询?我发现该类型可以像这样索引:s.PriceCalculation[“$type”]。为了回答这个问题,应该注意这个索引必须通过IndexDefinition上的MapString属性声明。