RavenDB Transformer Include 文件列表

RavenDB Transformer Include List of documents

我有点坚持将 include 与 RavenDB Transformer 一起使用。假设我有以下文档 classes:

public class Processor
{
    public string Id { get; set; }
    // other properties
}

public class Job
{
    public string Id { get; set; }
    public string ProcessorId { get; set; }
    // other properties
}

她是我的视图模型:

public class ProcessorStatsViewModel
{
    public string Id { get; set; }
    public int JobCount { get; set; }
    // other properties
}

在我的变形器中,我想查询处理器文档存储并在作业存储中执行包含以查找具有匹配处理器 ID 的每个作业。我发现的所有搜索结果都描述了当处理器 class 具有 JobId 列表时如何执行此操作。有没有办法在 RavenDB 中做到这一点?

我想要的变压器看起来像:

public Processors_StatsViewModel()
{
    TransformerResults = procs => 
        from p in procs
        let jobs = Include<Jobs>(p.Id) // how can i specify something like where p.Id == j.ProcessorId ?
        select new
        {
            p.Id
            JobCount = jobs.Count
            // other stuff
        }
}

所有 Transformer LoadDocument、Include 和 Recurse 方法都希望被查询的 class 具有列表引用 ID,但在我的情况下需要相反的东西。

这是我什至可以在 RavenDB 中做的事情还是我遗漏了什么?

你不能只用一个转换器和你当前的领域模型做你想做的事。如果处理器确实知道它的工作,您可以使用类似于您拥有的变压器来完成此操作。

但是,您可以使用 Map/Reduce 索引然后在 Map/Reduce 索引的结果上使用 Transformer 来实现类似的效果。这完全取决于您要呈现的内容 "other stuff",但这是一种获取所有进程及其作业计数然后使用转换器添加更多信息的方法:

Map/Reduce 按处理器获取作业数的索引:

public class Jobs_ByProcessor : AbstractIndexCreationTask<Job, Jobs_ByProcessor.ReduceResult>
{
    public class ReduceResult
    {
        public string ProcessorId { get; set; }
        public int JobCount { get; set; }
    }

    public Jobs_ByProcessor()
    {
        Map = jobs => from job in jobs
                      select new ReduceResult
                      {
                          ProcessorId = job.ProcessorId,
                          JobCount = 1
                      };

        Reduce = results => from result in results
                            group result by result.ProcessorId
                                into g
                                select new
                                {
                                    ProcessorId = g.Key,
                                    JobCount = g.Sum(x => x.JobCount)
                                };
    }
}

变形金刚:

public class ProcessorJobTransformer : AbstractTransformerCreationTask<Jobs_ByProcessor.ReduceResult>
{
    public ProcessorJobTransformer()
    {
        TransformResults = results => from result in results
            let processor = LoadDocument<Processor>(result.ProcessorId)
            select new
            {
                Id = result.ProcessorId,
                Name = processor.Name,
                JobCount = result.JobCount
            };
    }
}

这会给你这样的结果: Id 和 JobCount 来自索引的 Reduce 结果,Name 来自 Transformer(通过 LoadDocument)。

但是,如果您需要此结果但需要工作文档中的更多信息,则可能需要完全不同的路线。

希望对您有所帮助!