CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Getting JSON from CSLA Business Objects.

rated by 0 users
This post has 16 Replies | 3 Followers

Top 50 Contributor
Posts 159
tarekahf Posted: Mon, Oct 26 2009 5:38 AM

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.

Top 10 Contributor
Posts 9,270

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.

Rocky

Top 50 Contributor
Posts 159
tarekahf replied on Mon, Oct 26 2009 3:16 PM
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.

Top 10 Contributor
Posts 9,270

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?).

Rocky

Top 50 Contributor
Posts 159
tarekahf replied on Mon, Oct 26 2009 3:51 PM
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.
Top 10 Contributor
Posts 9,270

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.

 

Rocky

Top 50 Contributor
Posts 159
tarekahf replied on Mon, Oct 26 2009 4:25 PM
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.
Top 50 Contributor
Posts 159
tarekahf replied on Mon, Oct 26 2009 4:31 PM
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.
Top 25 Contributor
Posts 422
tmg4340 replied on Mon, Oct 26 2009 4:38 PM

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

Top 10 Contributor
Posts 9,270

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?

Rocky

Top 50 Contributor
Posts 159
tarekahf replied on Mon, Oct 26 2009 4:51 PM

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.


Top 50 Contributor
Posts 159
tarekahf replied on Mon, Oct 26 2009 5:01 PM
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.
Top 50 Contributor
Posts 159
tarekahf replied on Tue, Oct 27 2009 3:20 AM

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.

Top 50 Contributor
Posts 184
rasupit replied on Wed, Oct 28 2009 12:03 PM
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




Top 50 Contributor
Posts 159
tarekahf replied on Thu, Oct 29 2009 4:54 AM
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.
Page 1 of 2 (17 items) 1 2 Next > | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems