Getting JSON from CSLA Business Objects.

Getting JSON from CSLA Business Objects.

Old forum URL: forums.lhotka.net/forums/t/7875.aspx


tarekahf posted on Monday, October 26, 2009

I am considering using CSLA .NET DataSource as input to YUI DataSource http://developer.yahoo.com/yui/datasource/, to get results as JSON from the CSLA DataSource.

I am just wondering if any work has been done on this before ?

In other words, I want to add a Shared Method to an existing CSLA Business Object to return the object content as JSON.

Appreciate your feedback/comments.

Tarek.

RockfordLhotka replied on Monday, October 26, 2009

A JSON interface is the same as an XML interface - in both cases you are talking about building an interface on top of your objects.

Chapter 21 in Expert 2008 Business Objects talks about building service interfaces on top of objects. While I don't specifically discuss JSON (or REST), the concepts from the chapter are valid for those technologies just as much as for XML services.

tarekahf replied on Monday, October 26, 2009

Hi Rocky,

Thanks a lot for your helpful reply.

I need your feedback on the following:

1. What do you think about this approach: integrating YUI Controls and .NET Controls (CSLA.NET Objects) to take advantage of the power of both technologies ?


2. It is good that YUI DataSource controls also supports XML Interfaces. Looks like it does not support calls to Web Services (SOAP). I have the book Expert VB 2005 Business Objects. Do I still need to get Expert 2008 Business Objects to write XML Interface against CSLA Objects ? Becuase I am still on CSLA for .NET 2.0. Do I have to upgrade to write XML Interface for YUI DataSource ? Could you please give me some hints on this direction based on the resources availble to me now ?

Appreciate your help.

Tarek.

RockfordLhotka replied on Monday, October 26, 2009

I am not familiar with this YUI library, and don’t have time to research it now, so I don’t know that I can answer.

 

No, you do not need to upgrade CSLA. You can read Chapter 11 of the Expert 2005 Business Objects book and the concept is the same.

 

JSON or XML services should be considered to be an interface over your objects, just like HTML is consider to be an interface. Business objects should not know how to generate the interface – that way leads to disaster (are you going to have ToXml(), ToJson(), ToHtml(), ToWindowsForm(), ToXaml() and other methods on every object?).

tarekahf replied on Monday, October 26, 2009

So you are saying that I should not add methods like: ToJson() or ToXml() for each object ? May I know why ?

Do you mean that I should implement a wrapper class over each object that will generate the required interface ?

I was thinking to add a Method just like that "ToXML()" for each object, and write a regular ASPX Page that will create the object based on parameted from the Query String, and then get the XML and use Respone.Write() to send the XML result back to YUI DataSource.

Appreciate your feedback.

Tarek.

RockfordLhotka replied on Monday, October 26, 2009

I think my previous post should have been clear.

 

If you add ToXml(), then ToJson(), why not ToHtml()? And ToAjaxHtml() and ToXaml()?

 

You are mixing the concept of interface into your business layer, which breaks down the architectural separation of concerns.

 

What happens when you learn that the way you render your XML or Json doesn’t match someone’s needs? Do you have ToJson() and ToJson2() and ToJson3()?

 

Putting interface code into the business layer breaks the layered architecture concept, and leads to unmaintainable code.

 

tarekahf replied on Monday, October 26, 2009

Ok, I see your point.

What do you suggest to implement such interface ?

Should I create another class (or another Object) which will be a wrapper to the original Business Object which will have only the required methods such as ToXML(), ToJson(), or ToHTML() ... and then later, if different requirements are needed, then we can add ToJson2(), ToJson3() or ToXML2() ...etc ?

After I create this new wrapper class, then the only way I see to provide the XML and/or Json Interface is by creating another ASPX Page that will invoke the Wrapper Class methods to get the required XML/Json or whatever, then use Respone.ContentType and Respone.Write() to send the results to YUI DataSource consumer.

I simply need to get an XML Representation to my CSLA.NET Business Object, so that it can be consumed by YUI DataSource Control.

The reason why I am asking here is to get advise on how best to implement such requirements.

Appreciate your advise.

Tarek.

tarekahf replied on Monday, October 26, 2009

The reason I am asking on the recommended approach is that I was told once that I must write HTTP Handlers if I want to provide such Interface to Business Objects.

But, I see that HTTP Handlers are a bit complicated for such requirements.

Tarek.

tmg4340 replied on Monday, October 26, 2009

You will probably be well-advised to read the chapters on XML services and CSLA, and to get familiar with XML Web services.  Since you're still in VS 2005, you'll have to roll your own JSON support, but it basically works the same as an XML service.  Essentially, you are building another interface on top of your BO's, much like an ASPX page.  However, in the XML case, it's an ASMX page which handles the XML translation for you.  If you need JSON, then you can build ASPX pages and use Response.Write.  If you want to handle both types within a single page, then you can do your XML translation in an ASPX page as well, but you'll have to do it by hand, just like you do your JSON format.

You don't build your BO's to deal with that interface - you use the interface to shape what your BO's return.  That doesn't mean you create wrapper classes for each of your BO's - the code-behind of your service page is doing the wrapping for you.  If you need multiple data shapes from your BO, those become multiple methods on your service page, or different service pages.  But you don't do anything to your BO's.

HTH

- Scott

tarekahf replied on Monday, October 26, 2009

tmg4340,

May thanks for the reply.

When you say XML Service, you actually mean a Web Service ? Using SOAP Format ?

I am very much familiar with developing Web Services as a wrapper for CSLA .NET Business Objects. Very easy job for me. But the problem here (as per my understanding) is that the YUI DataSource does not understand the results of SOAP Based XML Reply (Web Service), and in turn, cannot consume such XML web services.

It appears to me that I have to develop a wrapper of some kind that will generate the XML representation of the Business Object as per the format required by the YUI DataSource Control.

Appreciate your feedback/advise.

Tarek.


RockfordLhotka replied on Monday, October 26, 2009

Yes, you should create another object that maps the business layer information to the interface layer.

 

This is what an ASP.NET Page (aspx, etc) does to get business object properties into HTML, and is exactly what you should do for a service.

 

You can do this as you suggest, with an aspx page that writes out the stream. Or an asmx page. Or as you say, an HttpHandler.

 

But a lot of people do this sort of thing with an aspx page.

 

Technically you don’t need some other object either – the generation of XML or JSON can be done in the aspx page.

 

Which almost makes sense if you think about it – an aspx page usually renders HTML, you are just having it render JSON or XML.

 

And if you want a page with a different format you create a different aspx page right? So if you need different XML or JSON you’d just create a different aspx page for that too. Otherwise how does the calling app know what it is asking for?

tarekahf replied on Monday, October 26, 2009

Rocky,

Thanks a lot ! I feel now confortable with my direction. Looks like my understaning is correct. I will not use HttpHanldlers since they look complicated to me and I do not see the added value.

I can't wait to see the power of YUI Controls with my CSLA .NET Objects !

Tarek.

tarekahf replied on Tuesday, October 27, 2009

I found the following additional resource related to this topic for those who are interested:

http://developer.yahoo.com/yui/examples/datatable/dt_xhrpostxml.html (for this exmaple, the source is XML and is located here)

http://yuiblog.com/blog/2008/10/15/datatable-260-part-one

http://mattberseth.com/blog/2008/09/dynamic_data_experimenting_wit.html (this example uses .NET 3.5, Json Built-in support in .NET, Web Service, and YUI DataTable.)

Tarek.

rasupit replied on Wednesday, October 28, 2009

If your web service only need to support Json and not Soap, you should be able to convert CSLA object to json with very little/no pain.  Json serializer works just fine with CSLA objects.

There are two ways you can do this. 1. Simply returns CSLA object on your ScriptMethod within asmx. 2. use System.Web.Script.Serialization.JavaScriptSerializer to generate json object string that you can render however you like within your aspx page.

You can also use System.Web.Script.Serialization.ScriptIgnoreAttribute to exclude properties from serialization.  I use this attribute to exclude all the IsXXX properties to reduce the footprint.

HTH,
Ricky




tarekahf replied on Thursday, October 29, 2009

Ricky,

Thanks for the helpful reply. It is really great to know that Json is natively supported by .NET 3.5. I was investigating Json.Net and Jayrock for .NET 2.0. Can you imagine the effort I saved now ?

The problem in my case is that I am on VS 2005 + .NET 2.0 + CSLA 2.0. What do I need to do now ?

Do you think I can also upgrade to .NET 3.5 & CSLA .NET 3.X and still keep on using VS 2005 ?

My plan is to start upgrading gradually until I am sure everything is OK.

Maybe I can Just download .NET 3.5 then add a reference to the .NET Assembly in my project ? Everything else is then supposed to work properly ?

I hope it is going to be that easy.

Appreciate your help.

Tarek.

rasupit replied on Thursday, October 29, 2009

Tarek,

ASP.NET Ajax is supported since .NET 2.0.  Unlike .NET 3.5, the Ajax extension comes in a separate download.

Here is the download site of ASP.NET Ajax for ASP.NET 2.0

HTH,
Ricky

tarekahf replied on Saturday, April 03, 2010

Just a quick update on this topic.

With EssentialObjects Grid Control (and probably other controls too), there is no need to serialize Csla Objects to XML nor Json. EO.Web Controls supports CslaDataSource out of the box.

See this link for more details:

http://forums.lhotka.net/forums/p/8758/41666.aspx#41666

Tarek.

Copyright (c) Marimer LLC