【问题标题】:.NET Core Cosmos Private Setter for ID Not PopulatingID 未填充的 .NET Core Cosmos 私有设置器
【发布时间】:2018-09-05 14:27:33
【问题描述】:

我是 .NET Core 的新手,如果这个问题不太适合 Stackoverdlow,请原谅。我将 CosmosDB 与 .NET Core 2.1 一起使用。我有一个简单的类,我坚持下面的一个集合。

public class Customer
{
    public string Id { get; private set; }
    public string FirstName { get; private set; }
    public string LastName { get; private set; }

    public Customer(string firstName, string lastName) {

        if (string.IsNullOrWhiteSpace(firstName) || string.IsNullOrWhiteSpace(lastName))
        {
            throw new ArgumentException("First and Last name are required");
        }

        this.FirstName = firstName;
        this.LastName = lastName;
    }
}

请注意私有集,因为 Id 是由数据库自动生成的,不应由调用者设置。

当我保存记录并检索它们时,不会填充 Id 属性。但是,如果我将 setter 更改为 public,它就可以正常工作。这是一个非常简单的示例,但理想情况下,我应该能够将 Id setter 设为私有,因为它在类外应该是不可变的。我过去在 Java 中使用过像 Hibernate 这样的库,因为该字段是通过反射设置的,所以效果很好。

.NET Core 和 CosmosDB 有没有办法处理私有 setter?在尝试实施 OOP/DDD 方法时,我可以看到这在像 Order 这样的域上会出现问题。

public class Order
{
    public int Id { get; private set; }
    public IList<LineItem> LineItems { get; private set; }

    public void AddLineItem(LineItem lineItem) {
        // do some business logic, encapsulatng the line items
        // IE don't just let the caller have free reign 
    }
}

public class LineItem 
{
    public string SKU { get; set; }
    public int Qty { get; set; }
    public decimal PricePerUnit { get; set; }
}

【问题讨论】:

  • 您可以尝试将属性[JsonProperty("id")] 添加到Customer.Id 吗?
  • 另外,你还不如完全摆脱 setter..?
  • 在客户的 Id 属性上添加属性就可以了,只要保留私有设置器就可以了。如果 setter 被完全删除,则会发生相同的问题。我猜这与 JSON 映射库的工作方式有关。
  • 我已经添加了可以进一步解释和帮助您的答案

标签: c# .net-core azure-cosmosdb


【解决方案1】:

由于 CosmosDb 具有预定义的属性 id,因此您需要 JSON 序列化程序来绑定它,并且由于这区分大小写,以下是允许您执行此操作的属性:

public class Customer
{
    [JsonProperty("id")]
    public string Id { get; private set; }
    // other members
}

除此之外,我更喜欢添加另一个属性来存储任何未映射的属性

/// <summary>
/// Any unmapped properties from the JSON data deserializes into this property.
/// </summary>
[JsonExtensionData]
public IDictionary<string, JToken> UnmappedData { get; set; }

因此,至少在调试时,我会意识到由于区分大小写、拼写错误等原因可能遗漏的任何属性。

实际上,我的 CosmosDb 模型的基类如下所示:

/// <summary>
/// Implements auto generated Id property for CosmosDb Model
/// </summary>
public abstract class BaseModel
{
    /// <summary>
    /// PK for this model. (apart from the ResourceId which is internally generated by CosmoDb)
    /// If the user does not set this, the SDK will set this automatically to a GUID.
    /// </summary>
    [JsonProperty(PropertyName = "id")]
    public virtual string Id { get; set; }

    /// <summary>
    /// Any unmapped properties from the JSON data deserializes into this property.
    /// </summary>
    [JsonExtensionData]
    public IDictionary<string, JToken> UnmappedData { get; set; }
}

【讨论】:

    猜你喜欢
    • 2020-03-15
    • 2022-02-18
    • 2018-09-18
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    相关资源
    最近更新 更多