将“0”/“1”解析为bool in(。NET 4.0 c#)同步框架4.0 CTP。

时间:2021-05-21 21:46:23

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?

是否可能有某种类型转换器允许同步框架将字符串“0”/“1”作为false/true的布尔值。根据布尔值。默认情况下不支持解析文档,有办法绕过它吗?

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.

如果有人对这个问题有不同的建议,可以提供更多的细节:我有一个针对android的自定义同步框架客户端实现,由于它使用SQLite数据库,所以我无法在表字段上强制执行一些严格的数据类型。我可以用“Is”前缀来表示布尔域的约定,但那样会很麻烦。另一件事是,在SQLite中,布尔值被视为数字类型,因此将布尔false/true值插入到SQLite数据库中,自动将它们转换为0/1,而我不希望在android方面引入一些true / false = 1/0的转换。

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.

[更新]更多细节:服务器端包含一些使用Microsoft Sync Framework 4.0 CTP的服务。除了创建范围等之外,没有太多可配置的。客户端生成变更集并使用JSON格式将其传输到服务器。changeset来自SQLite数据库(而SQLite除了0/1之外没有任何本地布尔表示),所以在读取数据库时,没有迹象表明即将到来的数据是布尔类型的。字段值被序列化为带有数值(“0”或“1”)的字符串,因此服务器端在尝试将其解析为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'.

顺便说一下,如果进行单向客户到服务器的同步,那么可以围绕它工作。我手动将服务器端实体字段类型设置为byte,同步框架很好地将其用作“位”数据库类型。这种变通方法不适用于服务器对客户端tho'。

4 个解决方案

#1


2  

The usual way in C#/.NET is to use Convert.ToBoolean()

c# /中常见的方法。NET将使用Convert.ToBoolean()

#2


0  

I do not want to introduce some TRUE/FALSE = 1/0 conversion on android side

我不想在android端引入一些真/假= 1/0的转换

It seems you will have to since there is no boolean type in SQLite.

因为SQLite中没有布尔类型,所以您似乎必须这么做。

boolean active = cursor.getInt(column_idx)==1;

#3


0  

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

对于同步框架位,您是否尝试过实现拦截器来拦截更改并进行转换?参见:如何:使用拦截器在服务器上扩展业务逻辑

#4


0  

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.

所以这似乎不可能以任何容易的方式去做我所要求的事情。最后,我选择编写android sync库的附加功能来解析SQLite表定义,并使用字段描述来格式化传出的同步数据。

Thank you all for your effort.

谢谢大家的努力。

#1


2  

The usual way in C#/.NET is to use Convert.ToBoolean()

c# /中常见的方法。NET将使用Convert.ToBoolean()

#2


0  

I do not want to introduce some TRUE/FALSE = 1/0 conversion on android side

我不想在android端引入一些真/假= 1/0的转换

It seems you will have to since there is no boolean type in SQLite.

因为SQLite中没有布尔类型,所以您似乎必须这么做。

boolean active = cursor.getInt(column_idx)==1;

#3


0  

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

对于同步框架位,您是否尝试过实现拦截器来拦截更改并进行转换?参见:如何:使用拦截器在服务器上扩展业务逻辑

#4


0  

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.

所以这似乎不可能以任何容易的方式去做我所要求的事情。最后,我选择编写android sync库的附加功能来解析SQLite表定义,并使用字段描述来格式化传出的同步数据。

Thank you all for your effort.

谢谢大家的努力。