Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate javascript objects from C# dto classes

Tags:

javascript

c#

What is the best way to go about syncing the server poco/dto object definitions in my spa application? so if i have a class with a list of address then on client we would like to have an object template that we can use to create an address instance to insert into the address list. Obviously our object graph is much larger and changes a lot during development so keeping such things in sync by hand is not a winning solution.

c# dto class

puclic class dto{
dto()
{
 addressList = new List<address>();
}
 puclic List<address> addresses {get;set;}
 public string otherField{get;set;}
}

public class address{
 public string street{get;set;}
 public string city {get;set;}
}

javascript objects

var AddressClass = function(){
 this.street ="";
 this.city = "";
};

var Dto = function(){
 this.addressList = [];
 this.otherField = "";
};

some where in scope add to the addressList array

$scope.dtoClass.AddressList.push (new AddressClass() );

so the goal is an address class on the server and an addressClass on the client.

like image 840
Aaron Fischer Avatar asked May 21 '26 17:05

Aaron Fischer


1 Answers

sharp2Js is a small library that can create javascript objects that mirror your C# POCO classes and can be easily used to generate js files using T4 templates

Here is the url on github https://github.com/castle-it/sharp2Js

like image 146
vibs2006 Avatar answered May 24 '26 06:05

vibs2006