Which Framework Should I Generate For easy JSON compliance using CodeSmith

409 Views Asked by At

Having just managed to score a copy of CodeSmith 5.2 at the same time that I'm starting on a web development project in ASP.NET I thought I would combine the two.

The site will need to make use of JSON based elements (either jQuery or ExtJS, haven't decided yet) for search/dropdown type behaviour.

I've done a bit of reading, and I see some common issues with JSON serialisation in netTiers regarding circular references, etc.

So it seems I've got a choice of netTiers, CSLA, NHibernate, KineticFramework or PLINQO by default.

I've used netTiers before.

What I would like is some pointers:

Which framework is easiest to generate compliant code with, and what tweaks need to be made either to the default settings, or to the templates themselves in order to get it to work.

1

There are 1 best solutions below

2
On BEST ANSWER

I've been using netTiers with ExtJs for some time and it is true that there are issues with circular references while encoding, but we seldom return entire entities and find that returning a subset of the entity data works well. We use C#; here's an example:

UserAccountService uaSvc = new UserAccountService();
UserAccount ua = uaSvc.GetByUserId(UserId)[0];
return Json(new { success = true, data = new { ua.UserName, ua.CustomerName, ua.Email } });

Where the UserAccountService and UserAccount are a service and entity generated using the CodeSmith netTiers templates and the Json() return function is part of Microsoft's MVC. You can substitute another encoder such as the Newtonsoft one if you're not using MVC.

This returns JSON which looks like this; Ext.js can easily consume this:

{"success": true, data:{"UserName":"Knight", "CustomerName":"Knight Inc.", "Email":"[email protected]"}}

You can also return a list or array of data, the following uses Linq to acheive it in a very compact way

return Json(new UserAccountService().GetAll().Select(u=>new  {u.UserID, u.UserName}));

This returns data like this:

[{"UserName":"Knight", "CustomerName":"Knight Inc.", "Email":"[email protected]"}, {"UserName":"George", "CustomerName":"George Inc.", "Email":"[email protected]"}]

As you can see, compliant JSON is easy to create from netTiers query results. I haven't used templates other than the netTiers ones, but I imagine you'd go down a similar avenue.