Looking for existing, proven, solutions for quickly generating a client-side javascript object model that represents an existing c# object. I imagine there is a T4 template or some other approach out there but I lack the terminology to find it. I'm not talking about serialization to get the JSON representation of an existing c# object instance or anything to do with deserialization. I simply want to generate the javascript object model's for 20+ c# objects and I want to be able to re-generate them at a moments notice if the c# code changes.
Over-simplified example of what I'm looking for:
C# code:
[Serializable()]
public class Cat
{
public string Name { get; set; }
public string Breed { get; set; }
}
Javascript object model to be generated:
function Cat()
{
this.Name = "";
this.Breed = "";
}
@Baszz
JSON is a text-based standard for data interchange and that's not what I'm looking for. I need to generate a client-side API of 20+ objects that I can put in a javascript file and link that script to my various web pages.
The JavaScriptSerializer can spit out a string like below from a c# object:
{ "Name": "Hayden", "Breed": "Rabbit” }
But this is not the same thing as:
function Cat()
{
this.Name = "";
this.Breed = "";
}
var myCat = new Cat();
Not a lot of comments so I’m guessing everyone does this by hand or not at all. Looking at creating my own T4 template to parse the c# files and generate my client-side API’s.
That said, constructors and prototypes can be used to implement class-based OOP patterns in JavaScript. But using them directly to implement features like inheritance is tricky, so JavaScript provides extra features, layered on top of the prototype model, that map more directly to the concepts of class-based OOP.
The get syntax binds an object property to a function that will be called when that property is looked up.
Using an Object Literal This is the easiest way to create a JavaScript Object. Using an object literal, you both define and create an object in one statement. An object literal is a list of name:value pairs (like age:50) inside curly braces {}.
To create an object, use the new keyword with Object() constructor, like this: const person = new Object(); Now, to add properties to this object, we have to do something like this: person.
After countless searches I could not find anything close to what I’m looking for. Clearly everyone is caught up in the JSON buzz word the past few years and no one is auto-generating client-side object models. I looked at Codesmith and at T4 templates. Neither has any built in mechanisms for parsing code files. Both require you to jump into reflection to get at properties and their types which rests 100% on the developers shoulders. Which begs the question once you jump through that hoop of writing all that reflection code what benefit does Codesmith or T4 templates give you? Absolutely nothing.. You mind as well place your reflection code in a re-usable class library and call it from console application and that’s exactly what I’ve done.
This is an older question, but you could try sharp2Js. It's a library designed to reflect on your classes and generate javascript objects with a couple of utility functions.
Running it against the example you provided (and outputting the string it produces in a T4
template or otherwise):
string modelOutput = Castle.Sharp2Js.JsGenerator.
GenerateJsModelFromTypeWithDescendants(typeof(Cat), true, "example");
Produces:
example = {};
example.Cat = function (cons, overrideObj) {
if (!overrideObj) { overrideObj = { }; }
if (!cons) { cons = { }; }
var i, length;
this.name = cons.name;
this.breed = cons.breed;
this.$merge = function (mergeObj) {
if (!mergeObj) { mergeObj = { }; }
this.name = mergeObj.name;
this.breed = mergeObj.breed;
}
}
The extra metadata in there is some scaffolding to support collections and complex types with the ability to create inherited objects to override behavior, etc.
Note: I am the maintainer of sharp2Js
, and it's new and doesn't do a lot yet, but perhaps it can help for scenarios like yours.
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