I have an android application that talks to a .NET server using WEB API web services. Currently, I have transfer objects on the server and on the client.
Java class
public class UserAction {
private String username;
private String action;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
}
My C# class on the server side
public class UserActionTO
{
public string Username { get; set; }
public string Action { get; set; }
}
I wish to share these models, between the two applications.
I found this question discussing sharing types but my situation is a bit different. Should I share types between a Web API service and its client ? What are the other options?
I am considering creating a C++ dll, which both my android application and C# application could use. Is this my best option?
EDIT I am using JSON to transfer the data
The best option is to use a standard platform-neutral format (usually either JSON or XML). This gives you immense freedom to vary both client and server any way you wish and makes development and testing easier, since you can inspect or generate content easily.
If you're asking about an easy mechanism for representing the content in-memory on multiple endpoints, it's perfectly reasonable to create a "client SDK" (many online services publish them for multiple programming languages each) that has these DTOs and perhaps some utilities and use the appropriate one server-side. It is not generally a good idea to get overly clever and do something like drop into native code; keep the code for the DTOs in the appropriate language (such as Java for Android, or perhaps C# if using Xamarin).
Since you're already using JSON for the data I would suggest you write once in .NET and create your sample result JSON objects. Then use http://www.jsonschema2pojo.org/ to create your POJOS it'll even include the generated annotation so you know not to modify the class.
Another advantage is you can format it with jackson or GSON annotation styles to serialize/de-serialize the objects with little effort.
If you want to go crazy you could model everything in JSON, then have a script that uses http://json2csharp.com/ to create your .NET and http://www.jsonschema2pojo.org/ for Java POJOs for you. But I'm not sure it would be worth the trouble :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With