Struggling with the design of my web application involving JSON, ASP.NET, TypeScript/JavaScript, and AngularJS.
In Brief:
Seeking best practices for transmitting data from server to client via JSON, utilizing the JSON string on the client side to generate objects.
The structure of my WebServerAPI project (ASP.NET) includes:
- Controllers
- DataController (REST API)
- Model
- A
- Type
Overview of model classes:
public class A {
public property int Id {get; set;}
public property string Name {get; set;}
public property Type Type {get; set;}
}
public class Type {
public property int Id {get; set;}
public property string Name {get; set;}
}
Each model class's ID corresponds to a primary key in a database table.
DataController structure:
public class DataController : ApiController {
// ...
// GET api/a
public IEnumerable<A> Get()
{
// fetches all As from the database
// creates instances of A and Type as required
// (if A has a 'Type' the corresponding Type will also be fetched and set)
return facade.GetAll<A>();
}
// ...
}
The Get() method of the DataController returns a JSON result.
Current JSON result:
[
{"Id":1,
"Type": {"Id":1, "Name":"name1"}
},
{"Id":2,
"Type": {"Id":2, "Name":"name2"}
},
{"Id":3,
"Type": {"Id":1, "Name":"name1"}
}
{"Id":4,
"Type": {"Id":2, "Name":"name2"}
},
{"Id":5,
"Type": {"Id":2, "Name":"name2"}
},
]
Considering alternative ways of structuring the JSON data for efficiency.
Possible solutions:
[
{"Id":1,
"TypeId": {"Id":1}
},
{"Id":2,
"TypeId": {"Id":2}
},
{"Id":3,
"TypeId": {"Id":1}
}
{"Id":4,
"TypeId": {"Id":2}
},
{"Id":5,
"TypeId": {"Id":2}
},
]
Another approach could be combining available types and objects in the same JSON result:
[
{"Types":
[
{"Id":1, "Name":"name1"},
{"Id":2, "Name":"name2"},
]
},
{"As":
[
{"Id":1,
"TypeId": {"Id":1}
},
{"Id":2,
"TypeId": {"Id":2}
},
{"Id":3,
"TypeId": {"Id":1}
}
{"Id":4,
"TypeId": {"Id":2}
},
{"Id":5,
"TypeId": {"Id":2}
}
]
}
]
Exploring optimal practices for handling such data transmission scenarios. Considering implications for memory usage and CPU resources when creating multiple objects versus references.
To summarize: Looking for best approaches for sending data between server and client using JSON strings to generate objects effectively.