HttpService Vs Remote Objects - apache-flex

I have a flex application and need to show the real time data into the chatrs and datagrids.
Eralier we are used Httpservices to showing the real time data and historical data into charts and datagrids. But now we are going to replace the Httpservices to remote objects.
So which places generally need to change. I have a little bit idea about remote objects.
Thanks,
Ravi

If you need to display real time data (or "near real time") you should use some kind of pushing mechanism - take a look on BlazeDS and read about polling and streaming.
If you just need to replace your webservices with remote objects you will need to replace the code dealing with the xml response (extracting data etc) with the code dealing with the objects returned by the remote calls. It is not mandatory to use strongly typed objects, but it will help.

If you are going to replace your HTTPService with RemoteObject, some questions you need riposte yourself.
What framework are you going to implement, if any then check their RemoteObject Invoker Tag if any.
Your resultEvent and FaultEvent will vary according to the framework you are going to apply.
If you are going with Flex default RemoteObject
Then you need to replace all your HTTPService with RemoteObject tags.
Your backend code also requires some changes with business logic should get into methods with the result of function or method returning an object.
Finally a suggestion.
Instead of going with Remote Objects, why not go with Webservice. You can re use the components somewhere else too.
Updated links about Cairngorm
http://www.adobe.com/devnet/flex/articles/cairngorm_pt5_03.html
http://www.jeffryhouser.com/index.cfm/2007/2/19/Learning-Cairngorm-Part-3
http://www.asfusion.com/blog/entry/hello-world-cairngorm-example
http://justjoshn.com/entry/contact-manager-part-2-cairngorm-example
Thanks

Related

Passing ViewModel from Presentation to Service - Is it Okay?

In one of my views, I have a ViewModel which I populate from two tables, and then bind a List<ViewModel> to an editable GridView (ASP.NET Web Forms).
Now I need to send that edited List<ViewModel> back to the Services layer to update it in the database.
My question is - is it Okay to send the ViewModel back to Services, or should it stay in the Presentation? If not - should I better use a DTO? Many thanks.
Nice question !
After several (hard) debates with my teammates + my experience with MVC applications, I would not recommend to pass viewmodel to your service / domain layer.
ViewModel belongs to presentation, no matter what.
Because viewModel can be a combination of different models (e.g : 1 viewModel built from 10 models), your service layer should only work with your domain entities.
Otherwise, your service layer will end up to be unusable because constrained by your viewModels which are specifics for one view.
Nice tools like https://github.com/AutoMapper/AutoMapper were made to make the mapping job.
I would not do it. My rule is: supply service methods with everything they need to do their job and nothing more.
Why?
Because it reduces coupling. More often than not service methods are addressed from several sources (consumers). It is much easier for a consumer to fulfil a simple method signature than having to build a relatively complex object like a view model that it otherwise may have nothing to do with. It may even need a reference to an assembly it wouldn't need otherwise.
It greatly reduces maintenance effort. I think an average developer spends more than 50% of his time inspecting and tracking existing code (maybe even much more). Now everybody knows that looking for something that is not there takes disproportionally much time: you must have been everywhere to be sure. If a method receives arguments (or object with properties) that are not used directly or further down the call stack you or others will walk this long road time and again.
So if there is anything in the view model that does not play a part in the service method, don't use it to call the method.
Yes. I am pretty sure it is ok.
Try to use MS Entity Framework and it will help you allots.

Selective Explicit Loading in WCF Data Service

I'm about to implement a web service for my database, perhaps using WCF Data Services. Some of the objects I need to make available have child objects that need to be present for the objects to be useful. But because of lazy loading in the Entity Framework, those child objects are not going to be automatically loaded.
I'm going to be calling this service using JSON, and I don't want to have to specify the $expand option in each call. And it's not clear to me where I would use the LoadProperty method (same link), since I'm just writing the InitializeService method and letting the framework do the rest.
Is there a way to configure it to explicitly load some child objects and not others?
WCF Data Services currently doesn't support auto-expand on the server. The client always has to ask for expansions.
You could implement some kind of a workaround around the WCF DS, by modifying the incoming request. So for example if the client sends request for ~/Products you could modify it before it gets to WCF DS and let it process ~/Products&$expand=Category and that way effectively achieve auto-expand. But for such a service to be robust, you would have to parse the query URL and only add the expand if there's not already one in there and so on.
The other way is if its always necessary for the child object to be present, can we make the child object complex types instead of entities, so that they always come along with the parent. Is there a strong reason for the child objects to be individual entities?
Hope this helps.
Thanks
Pratik

How can one use existing tools to facilitate the process of changing data on the server using the DataGrid?

It is very simple to retrieve data from a database and display it inside a DataGrid. However, what are the current practices to push the changes in the DataGrid back to the database? One can achieve this by inserting a lot of meta information, however, it is very tedious and not reusable.
There are libraries for persisting data across tiers, but as you say it isn't a simple task. LiveCycle DS ships with this capability when used in conjunction with Hibernate on the server. GraniteDS and Tide can perform a similar function, but for the most part you are left rolling your own. If you are using one of the many MVC frameworks available (I use PureMVC) it isn't difficult to create this type of functionality. Kind of tedious, but not difficult.
Which server side technology would you be using? You need to use HttpServer, WebService or RemoteObject's in Flex to push data to this server side app and that would then update the database.
Unless of course we're talking about a Flex/AIR Desktop App where connecting to databases directly is possible.
You can use RemoteObject, if you have a Java class on the server side that takes care of database connection and provides method to update database.
Just Create an ArrayCollection object from modified datagrid and pass it while doing a method call.
I think making changes to the database is the easiest part of this whole thing. The hardest part is making the DataGrid know exactly from which database table each cell comes from. If you're the one writing the query, then you can probably return a lot of meta-data, such as the private key and table name for each cell.

Flex - XML Serialization and De-Serialization of nested Object structures

Our Flex app would like to work with requests and responses as object graphs (nothing unusual there) e.g. response becomes the model of some view, and would be a structure with several layers of nesting.
** Now, ideally we would like to use the same client (and server) side objects for different message formats e.g. XML and AMF, and have a pluggable serialization/de-serialization layer (!)
AMF has serialization and matching of client to server using
[RemoteClass(alias="samples.contact.Contact")]
but it seems there is no equivalent for XML.
I am (somewhat optimistically) looking for a neat way of serializing the object graph to XML, to send through a HTTPService from the client.
For responses, the default 'object' and 'E4X' provide some de-serialization. This is handy, but of course we don't have the niceties of unpacking the XML back into specific AS classes like we do with AMF.
Any suggestions?
(did have one idea come through about wrapping/casting object as XML or XMLList - this does not seem to work, however)
Update:
Both these libraries look useful, and I will very likely use them at some point.
For now, I really need the simplicity of re-using the metadata set for the AMF3 serialization which we are using in any case ([RemoteClass],[Transient])
.. so the best option at the moment is AMFX - used Flex Data Services for AMF transfer using XML - classes in mx.messaging.channels.amfx package - only drawback at the moment is any Externalizable class is transformed into a Hex byte stream - and ArrayCollection is Externalizable! (hoping to workaround by serializing the internal Array in a subclass ..)
Hope that's useful to someone ..
Regarding the Xml serialization I can give you a starting point (as biased as it may be, though :D).
I am working on a project that allows for automatic conversion of AS3 objects to and from xml. It basically uses annotations on the model objects you use for communication in order to construct the xml structure or populating an object from xml.
It is called FlexXB and you can check it out at http://code.google.com/p/flexxb/.
I started this project cos I got into the same issues at work (namely I have a server that communicates through xml) and I hoped it be of use to someone else.
Cheers,
Alex
Yet another project: FleXMLer (http://code.google.com/p/flexmler/).
It has both the straightforward attitude of asx3m where you can just call:
new FleXMLer().serialize(obj);
Or you can customize XML element names, skip elements and tweak the way arrays and hash tables are serialized.
Would appreciate your input.
checkout asx3m project at http://code.google.com/p/asx3m
It's an AS3 port of Java XStream serialization library and works pretty well.
I made it because I had to connect to a server platform that used XStream for exchanging data objects and put a lot of work in it.
It can be extended to serialize AS3 objects to any format (JSON for example) and could leverage power of user defined metatags.
Cheers,
Tomislav
There's a library including JSON available from Adobe, too. And since ActionScript is a superset of JavaScript ... and JSON is increasingly supported cross-framework ...

Is there a tool to capture an objects state to disk?

What I would like to do is capture an object that's in memory to disk for testing purposes. Since it takes many steps to get to this state, I would like to capture it once and skip the steps.
I realize that I could mock these objects up manually but I'd rather "record" and "replay" real objects because I think this would be faster.
Edit: The question is regarding this entire process, not just the serialization of the object (also file operations) and my hope that a tool exists to do this process on standard objects.
I am interested in Actionscript specifically for this is application but...
Are there examples of this in other
programming languages?
What is this process commonly called?
How would this be done in
Actionscript?
Edit:
Are there tools that make serialization and file operations automatic (i.e. no special interfaces)?
Would anybody else find the proposed tool useful (if it doesn't exist)?
Use case of what I am thinking of:
ObjectSaver.save(objZombie,"zombie"); //save the object
var zombieClone:Zombie = ObjectSaver.get("zombie"); // get the object
and the disk location being configurable somewhere.
Converting objects to bytes (so that they can be saved to disk or transmitted over network etc.) is called serialization.
But in your case, I don't think that serialization is that useful for testing purposes. When the test creates all its test data every time that the test is run, then you can always trust that the test data is what you expect it to be, and that there are no side-effect leaking from previous test runs.
I asked the same question for Flex a few days ago. ActionScript specifically doesn't have much support for serialization, though the JSON libraries mentioned in one of the responses looked promising.
Serialize Flex Objects to Save Restore Application State
I think you are talking about "object serialization".
It's called Serialization
Perl uses the Storable module to do this, I'm not sure about Actionscript.
This used to be called "checkpointing" (although that usually means saving the state of the entire system). Have you considered serializing your object to some intermediate format, and then creating a constructor that can accept an object in that format and re-create the object based on that? That might be a more straightforward way to go.
What is this process commonly called?
Serializing / deserializing
Marshalling / unmarshalling
Deflating / inflating
Check out the flash.utils.IExternalizable interface. It can be used to serialize ActionScript objects into a ByteArray. The resulting data could easily be written to disk or used to clone objects.
Note that this is not "automatic". You have to manually implement the interface and write the readExternal() and writeExternal() functions for each class you want to serialize. You'll be hard pressed to find a way to serialize custom classes "automatically" because private members are only accessible within the class itself. You'll need to make everything that you need serialized public if you want to create an external serialization method.
The closest I've come to this is using the appcorelib ClassUtil to create XML objects from existing objects (saving the xml manually) and create objects from this xml. For objects with arrays of custom types it takes configuring ArrayElementType Metadata tags and compiler options correctly as described in the docs.
ClassUtil.createXMLfromObject(obj);
CreateClassFromXMLObject(obj,targetClass);
If you're using AIR, you can store Objects in the included local database.
Here's a simple example using local SQLite database on the Adobe site, and more info on how data is stored in the database.

Resources