解析“0”/“1”作为 (.NET 4.0 C#) Sync Framework 4.0 CTP 中的 bool
是否可能有某种类型转换器允许 Sync Framework 将字符串“0”/“1”视为 false/true 的布尔值。根据 bool.parse 文档,它不受默认所以有办法解决吗?
如果有人可以建议解决此问题的不同方法,请提供更多详细信息: 我有一个针对 Android 的自定义同步框架客户端实现,因为它使用 SQLite 数据库,所以我无法在表字段上强制执行某些严格的数据类型。我可以想出用“Is”前缀来指示布尔字段的约定,但这会很糟糕。另一件事是,SQLite 中的布尔值被视为数字类型,因此将布尔值 false/true 插入/更新到 SQLite 数据库会自动将它们转换为 0/1,我不想在 Android 端引入一些 TRUE/FALSE = 1/0 转换。
欢迎任何想法。
[更新]更多详细信息: 服务器端由一些使用 Microsoft Sync Framework 4.0 CTP 的服务组成。除了创建范围等之外,没有太多需要配置的内容。客户端生成变更集并使用 JSON 格式将其传输到服务器。变更集来自 SQLite 数据库(除了数字 0/1 之外,SQLite 没有任何本机布尔表示),因此在读取数据库时,没有任何迹象表明即将到来的数据是布尔类型。字段值作为带有数值(“0”或“1”)的字符串序列化为 JSON 对象,因此服务器端在尝试将其解析为 bool 时失败。
顺便说一句,如果进行单向客户端到服务器同步,则可以解决此问题。我手动将服务器端实体字段类型设置为字节,同步框架很好地使用它作为“位”数据库类型。不过,此解决方法不适用于服务器到客户端。
Is it possible to have some kind of type converter that allows Sync Framework to treat string "0"/"1" as boolean values for false/true. As per bool.parse documentation it is not supported by default so is there a way around it?
More details in case someone can suggest a different approach to this issue:
I have a custom Sync Framework client implementation for android and as it uses SQLite database there is no way for me to enforce some strict datatype on table field. I could come up with the convention to indicate boolean field with, say, "Is" prefix, but that would be nasty. Another thing is that booleans in SQLite are treated as numeric type thus inserting/updating boolean false/true to the SQLite database automatically converts them to 0/1 and I do not want to introduce some TRUE/FALSE = 1/0 conversion on android side.
Any thoughts are welcome.
[UPDATE] More details:
Server side consists of some services that are using Microsoft Sync Framework 4.0 CTP. There isn't a lot to configure except creating scope and so on. Client side generates changeset and transmits it to the server using JSON format. The changeset comes from the SQLite database (and SQLite doesn't have any native boolean representation except numeric 0/1), so when reading database there is no indication that coming data is of boolean type. Field value is serialized into JSON object as string with numeric value ("0" or "1") and thus server side fails while trying to parse it to a bool.
Btw it is possible to work around it if doing one-way client-to-server synchronisation. I manually set server side entity field type to byte and Sync Framework nicely uses it as a "bit" database type. This workaround doesn't work for server-to-client tho'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C#/.NET 中的常用方法是使用
Convert .ToBoolean()
The usual way in C#/.NET is to use
Convert.ToBoolean()
似乎你必须这样做,因为 SQLite 中没有布尔类型。
It seems you will have to since there is no boolean type in SQLite.
对于同步框架位,您是否尝试过实现拦截器来拦截更改并进行转换?请参阅:如何:使用以下命令扩展服务器上的业务逻辑拦截器
for the Sync Framework bits, have you tried implementing Interceptors to intercept the changes and do the conversion? see: How To: Extend Business Logic on Server using Interceptors
所以看来不可能以任何简单的方式做我要求的事情。最后,我选择向 android 同步库编写附加功能来解析 SQLite 表定义并使用字段描述来格式化传出同步数据。
感谢大家的努力。
So it seams it is not possible to do what I ask in any easy way. At the end I opted in writing additional functionality to the android sync library to parse SQLite table definitions and use field descriptions to format outgoing synchronisation data.
Thank you all for your effort.