Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automatically generate javascript object model from c# object

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 = "";  
}  
  1. The JSON string is not a named function.
  2. All elements are quoted and in the JSON format which would require manual parsing of the string to get it into the format I need.
  3. You cannot new-up an instance of Cat like below because of #1

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.

like image 966
TugboatCaptain Avatar asked Nov 06 '11 16:11

TugboatCaptain


People also ask

Can you do OOP with JavaScript?

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.

What does .get do in JavaScript?

The get syntax binds an object property to a function that will be called when that property is looked up.

What is the best way to create an object in JavaScript?

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 {}.

How does one create a new object in JavaScript?

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.


2 Answers

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.

like image 179
TugboatCaptain Avatar answered Oct 27 '22 17:10

TugboatCaptain


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.

like image 21
Grant H. Avatar answered Oct 27 '22 17:10

Grant H.