views:

427

answers:

2

We are creating some WCF services that will initially be consumed by .NET clients but in the future will be consumed to Java clients.

As such we want to avoid using any data types in the interface that Java does not support.

The specific one we know of is nullable value types.

One suggestion is that we can support these by using a string to represent, for example, a nullable int. And empty string therefore represents null, otherwise there is a requirement that the string must be parsable as an int.

Can anyone recommend a better alternative, or is this what you would do?

+2  A: 

I'd hope that if you can expose a nullable integer through WCF, that anything consuming that from Java will use the wrapper types - Integer instead of int, Byte instead of byte etc. Of course these are reference types whereas nullable value types in .NET are still value types, so you'll get more GC pressure and it'll be generally less efficient, but there's not a lot you can do about that given that Java doesn't support user-defined value types.

Jon Skeet
Are there any Java guys out there who can verify what happens if you generate a proxy class for a service that exposes a nullable int?
Richard Ev
How are you exposing the WCF service to start with?
Jon Skeet
@Jon - I am nut sure what you mean by "how"...
Richard Ev
Well, as a REST API, or with SOAP, or something else? As I understand it, WCF can expose a service in many different ways - although I don't have much WCF experience myself. (Marc Gravell may be able to help more.) It will also depend on what's consuming the service on the Java side - e.g. Axis.
Jon Skeet
OK - we're exposing this using SOAP. However, we may also expose it as a RESTful service as well.
Richard Ev
Okay. So how does WCF expose nullable value types in SOAP? I suggest you just try exposing a simple service using nullable value types, then run Axis against it and see what happens.
Jon Skeet
+2  A: 

One approach that was used in a project I was involved in a few years ago (also .NET / Java combination) was to add indicator properties: NumProperty and NumPropertyIsNull. It wasn't particularly pretty, but it worked.

As a side node, I think that in the end we were not able to find one single business case where the difference between 0 and null actually mattered.

Fredrik Mörk
+1 for "NumProperty and NumPropertyIsNull", -1 for "not able to find one single business case where the difference between 0 and null actually mattered". 0 and null are different values and can have the different means. For example: 0 = total count is zero, NULL = total count is not known.
TcKs
@TcKs: I am well aware that 0 an NULL are not the same; I only said that in that particular project, we realized that there was no *practical* difference. That will of course vary from project to project, but it is well worth to take into account. If the difference does not matter for the application; then you should perhaps not spend time designing a solution for it. In another project the difference may be significant.
Fredrik Mörk
@Fredrik Mörk: If is no difference between NULL and 0, why support NULL in variables/columns/etc...?
TcKs
@TcKs: that was exactly my point... Start off by figuring out whether it makes sense to differ between NULL and 0 in your application. If not, don't spend time solving it.
Fredrik Mörk