Reputation: 10408
I have a library with some objects that I use both from C# and JavaScript. There are several objects, let's call one of them Foo, with the same basic implementation in C# and JavaScript. When I want to transmit an object of this type from the server to the browser, I simply call Foo.ToJson() to serialize this object to JSON and then revive the object on the browser side with a safe eval() operation.
This is all well and good, but the library is becoming complex and the need to keep the JavaScript and C# code bases synchronized is increasingly difficult and error-prone. I'm interested in ideas for simplifying this architecture or making it easy to maintain. Thoughts?
Upvotes: 1
Views: 311
Reputation: 1892
Use DTOs. Data Transfer Objects.
This way if your JScript or C# objects change, your DTOs don't necessarily have to change unless that property is immediately required at the client. And even then, you have a clear separation of what is intended to be Serialized and what is not.
Dave Ward at Encosia speaks a lot about this.
Upvotes: 1
Reputation: 86326
Upvotes: 1
Reputation: 29742
have you considered making your "shared code" into a web service, this way any of your applications can access it, and you can make everything distributed to help with performance.
Just one solution.
http://en.wikipedia.org/wiki/Web_services
Upvotes: 2
Reputation: 47809
You could look at using script#: http://projects.nikhilk.net/ScriptSharp
Since your classes are all coded in C#, you could reuse the same .cs file for both the server and client projects. that way, when you make changes, it's automatically compiled into the javascript :-)
Upvotes: 3