【问题标题】:Which data types should a client model use for offline sync with the Azure mobile apps SDK?客户端模型应使用哪些数据类型与 Azure 移动应用 SDK 进行离线同步?
【发布时间】:2016-07-15 04:24:13
【问题描述】:

我正在使用 Azure 移动应用服务 SDK 中的离线同步功能。

我知道 SDK 最近发生了各种变化。我想根据最新规范定义客户端模型,但不确定要使用哪种类型。

这些是离线同步元数据属性,通常出现在大多数示例/教程中:

[JsonProperty(PropertyName = "id")]
public string Id { get; set; }

[Version]
public string Version { get; set; }

[CreatedAt]
public DateTimeOffset CreatedAt { get; set; }

[UpdatedAt]
public DateTimeOffset UpdatedAt { get; set; }

[Deleted]
public bool Deleted { get; set; }

但是一些documentationexamples,以及各种官方(太多了!)samples/quickstarts on GitHub,使用了组合类型。

所以我在很多地方也看到过这种情况:

[JsonProperty(PropertyName = "id")]
public Guid Id { get; set; }              // Guid is used here, not string

[Version]
[JsonProperty(PropertyName = "version")]  // Needed? I assume the attribute is enough
public byte[] Version { get; set; }       // byte[] is used here, not string

在幕后一切都是通过 REST 调用和字符串来完成的。所以我假设客户端 SDK 执行各种类型转换。

但我不希望我的应用程序在未来某个时刻发生莫名其妙的轰炸,当事情发生变化时。那么我应该使用哪些官方支持的类型?

【问题讨论】:

    标签: c# azure azure-mobile-services


    【解决方案1】:

    客户端 SDK 只需要一个字符串 Id 字段(应命名为“Id”,除非您想跳过一堆麻烦)。字符串的值可以是任何唯一的字符串。服务器 SDK 默认使用字符串转换的 GUID。使用离线同步时,客户端 SDK 还会生成字符串 GUID,除非指定了 ID。当使用 online IMobileServiceTable API 时,客户端让服务器生成 ID。

    其余字段是可选的,应该具有您列出的类型,或者可以转换为它们。

    这是您的数据模型,在各个领域都有 cmets:

    // required, should be called Id, *must* be of type string when using offline sync
    // When not using offline sync, should be string-convertible.
    // When using offline sync, the client SDK uses a new string-converted GUID for this, unless 
    // an ID is specified.
    // When using offline sync, Ids are not mutable, so use something that can be client generated
    public string Id { get; set; }
    
    // optional. Using a Version field opts you into optimistic concurrency, where 
    // the server will reject updates that are done against an older version
    // of an object. This means you need a conflict handler.
    // To use a client-wins policy, remove this property from your client object
    [Version]
    public string Version { get; set; }
    
    // optional. Cannot be set on the client, will be sent from the server
    [CreatedAt]
    public DateTimeOffset CreatedAt { get; set; }
    
    // optional. Cannot be set on the client, will be sent from the server
    [UpdatedAt]
    public DateTimeOffset UpdatedAt { get; set; }
    
    // should generally not be used in the client object at all. This field tracks 
    // which objects have been deleted so that they are automatically purged
    // from the client's offline sync store
    [Deleted]
    public bool Deleted { get; set; }
    

    【讨论】:

    • 这是一个很好的参考...所有不同的问题都集中在一个地方...谢谢!
    • 虽然关于Id - 根据上面的评论,我可以使用Guid(而不是string)。对于Version,我想使用byte[](而不是string)。我的数据模型对我来说似乎更自然,但是,这样做是不是在自找麻烦?
    • 是的,因为您需要将字符串转换为服务器不需要的 GUID。 @adrian-hall 实际上意味着您应该使用 GUID 的字符串表示形式,而不是实际的 GUID 类型。
    • @hbob 我在答案中添加了有关客户端 ID 生成的更多详细信息。
    • @hbob 没错,响应可以作为任意字符串发回(或者可以通过直接更新数据库来创建新记录),并且该字符串可能无法转换为 GUID。此外,如果您使用默认本地存储实现的离线同步,我相信 ID 的 GUID 可能会失败或导致细微的错误。对于版本,字节[] 可能会起作用,但字符串更安全。您永远不需要在客户端将其作为字节数组进行操作。
    【解决方案2】:

    看起来您几乎可以对id 列使用任何东西,只要它是唯一的。 Even an email address!(感觉像 NoSQL...)我决定使用 Guid,因此所有实体在创建时都是一致的(无需等待服务器的响应)。

    我已经为version 列确定了一个字节数组,因为它似乎完全从服务器返回。

    编辑:这是完全错误的。请参阅下面的评论和上面的新接受答案。

    【讨论】:

    • 从技术上讲,不正确。您的 Id 字段在所有客户端中必须是唯一的 - 必须没有两个客户端引用具有相同 ID 的两个不同对象的风险。 Guid 是一个不错的选择。
    • 啊,是的,这是有道理的。两个用户可能决定使用同一个电子邮件地址!感谢您的澄清。按照您的建议,我正在使用 Guid(而不是字符串)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多