0

好的,所以我正在使用 Couchbase 2.0 和最新的 .NET 客户端。基本上我正在写的是一个跟踪目标的项目(一个美化的待办事项清单)......

我设法将目标对象作为 JSON 文档存储在 couchbase 中,然后将其反序列化回 POCO,但我的问题是如何自动查找链接的文档并填充 subGoal List<Goal>

不确定这种自动反序列化是否可以在没有一些逻辑在代码本身中处理它的情况下实现,但任何指针表示赞赏,干杯!

JSON

{
    id: "goal_1",
    name: "goal 1",
    description: "think of some better projects",
    subGoals: [goal_2, goal_3]
}

C#

var goal = client.GetJson<Goal>(id);
return goal;

这是 POCO

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;

namespace stuff.Models
{
    public class Goal
    {
        protected DateTime _targetDate;

        /// <summary>
        /// Name of the goal
        /// </summary>
        [JsonProperty("name")]
        public String Name { get; set; }

        /// <summary>
        /// Full description of the goal
        /// </summary>
        [JsonProperty("description")]
        public String Description { get; set; }

        /// <summary>
        /// Target date for completing this goal
        /// </summary>
        [JsonProperty("targetDate")]
        public DateTime? TargetDate
        {
            get
            {
                return _targetDate;
            }
            set
            {
                // target date must be later than any sub-goal target dates
            }
        }

        /// <summary>
        /// Any sub-goals
        /// </summary>
        [JsonProperty("subGoals")]
        public List<Goal> SubGoals { get; set; }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Description"></param>
        /// <param name="TargetDate"></param>
        /// <param name="SubGoals"></param>
        public Goal(String Name, String Description, DateTime? TargetDate = null, List<Goal> SubGoals = null)
        {
            this.Name = Name;
            this.Description = Description;
            this.TargetDate = TargetDate;
            this.SubGoals = SubGoals;
        }
    }
}
4

1 回答 1

2

没有自动方法可以在一个“加入”查询中获取相关文档,但是您可以使用整理视图。所以给定如下一组目标(注意添加了一个类型属性):

{"name":"goal 1","description":"This is a parent goal","subgoals":["goal2","goal3"],"type":"goal"}
{"name":"goal 2","description":"This is a child goal","type":"goal"}
{"name":"goal 3","description":"This is another child goal","type":"goal"}
{"name":"goal 4","description":"This is another parent goal","subgoals":["goal5","goal6","goal7"],"type":"goal"}
{"name":"goal 5","description":"This is a child goal","type":"goal"}
{"name":"goal 6","description":"This is a child goal","type":"goal"}
{"name":"goal 7","description":"This is a child goal","type":"goal"}

然后,您将编写一个输出每个父目标及其子目标的视图:

function (doc, meta) 
{
  if (doc.type == "goal" && doc.subgoals)
  {
    emit([meta.id, 0], null);
        for(var idx in doc.subgoals)
        {
          emit([doc.subgoals[idx], 1], null);
        }    
  }
}

此视图的输出是一组行,其中每一行包含一个由父 ID 和一个 0 组成的键,然后是其子 ID 和一个 1(0 和 1 确保行正确排序):

{"id":"goal1","key":["goal1",0],"value":null},
{"id":"goal1","key":["goal2",1],"value":null},
{"id":"goal1","key":["goal3",1],"value":null},
{"id":"goal4","key":["goal4",0],"value":null},
{"id":"goal4","key":["goal5",1],"value":null},
{"id":"goal4","key":["goal6",1],"value":null}

在 .NET SDK 教程中,我描述了如何查询整理视图(啤酒厂及其啤酒)。

http://www.couchbase.com/docs/couchbase-sdk-net-1.2/collat​​edviews.html

于 2013-01-14T17:29:10.450 回答