好的,所以我正在使用 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;
}
}
}