In my last post I showed you how to install Weborb .Net connector, now I'd like to build a little example using this technology: we will create an object from the server side and send it to the client. It's a very simple example but I think it is the better way to understand the most important functionality of Weborb.
For this example we will use IIS 6.0 and .NET 2.0 for the server side, while for the client we use a simple Flex 3 application.
The server side classes that we build for this project are:
Person.vb
DBManage.vb
DBDelegate.vb
Person class is made by three attributes: idPerson (numeric), name (String) and surname (String). This class is the VO (value object) that we want to manage both in the server and in the client side, so we have to build an ActionScript Class which references the service side class from the client.
The DBManage class has the methods to manage the persistence of data and it will be the remote object called by our flex client, through a Remote Object.
Here is the code of the class:
<Serializable()> Public Class Person
Public idPerson As Integer
Public name As String
Public surname As String
End Class
Public Class DBManage
Function tryPerson() As Person
Dim p As Person
p = New Person()
p.idPerson = 1
p.name = "Mario"
p.surname = "Rossi"
Return p
End Function
End Class
Public Class DBDelegate
Public Function tryPersonWebOrb() As Person
Dim db As DBManage
db = New DBManage()
Return db.tryPerson()
End Function
End Class
with these classes we can create a small DLL file, called weborbTrial.dll.
Before building the client we have to configure the Virtual Directory in IIS in which we will install the Weborb classes and also the client application. So we have to create a virtual directory in our IIS with permission of write and read. For information on how to install Weborb in this Directory you can see my last post.
After the installation we can carry on building the client application. First of all we have to copy into the subdirectory “bin” of our Weborb directory the weborbTrial.dll file. After we can start to create a new Flex Project. Open your Flex Builder and create a new Project. In the first screen insert the name of the Project, select the Flex Checkbox and choose the ASP.NET server configuration and click Next.
In the second screen in the web application Run field insert the URL of our virtual directory and the field below insert the locale directory of the application; then in the last field insert the bin-debug directory of the application. Click finish and the project is created.
Before starting to write the client application, open the properties of our project and in the compiler section add this line: -services [ourDirectory]\WEB-INF\services-config.xml.
First of all we have to create the VO ActionScript class, corresponding to Person.java:
package vo
{
import mx.collections.ArrayCollection;
[Bindable]
[RemoteClass(alias="weborbTrial.Person")]
public class Person
{
public function Person(){}
public var idPerson:Number;
public var name:String;
public var surname:String;
}
}
Now we can create our client. For this example we use a simple button for call the method of the remote object defined in the Application. If the connection with the server works in the right way, we should see in the Panel the object created by the server class.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import vo.Person;
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
//Result Event of the Remote Object
private function exec(event:ResultEvent):void{
var p:Person = new Person;
p= event.result as Person;
Name.text = p.name;
Surname.text = p.surname;
}
//Fault Event of the Remote Object
private function fault(event:FaultEvent):void{
Alert.show(event.message.toString());
}
private function addPerson():void{
exampleService.tryPersonWebOrb();
}
]]>
</mx:Script>
<mx:VBox width="100%" height="100%" verticalAlign="middle" horizontalAlign="center">
<mx:Panel title="Weborb .NET Example">
<mx:Form width="20%" height="40%" paddingBottom="30" paddingLeft="30" paddingRight="30" paddingTop="30">
<mx:FormItem label="Name">
<mx:TextInput id="Name" />
</mx:FormItem>
<mx:FormItem label="Surname">
<mx:TextInput id="Surname" />
</mx:FormItem>
</mx:Form>
</mx:Panel>
<mx:Button click="addPerson()" label="Pick a Person from .NET Server"/>
</mx:VBox>
<mx:RemoteObject id="exampleService" destination="GenericDestination" source="weborbTrial.DBDelegate" fault="fault(event)">
<mx:method name="tryPersonWebOrb" result="exec(event)" />
</mx:RemoteObject>
</mx:Application>
It is important to focus our attention on the definition of the remote object. This object is connecting to server by a default destination and channel defined in the remote-config.xml file, but it is possible to create our destination and channel and we can also define the source in the destination's property in the remote-config.xml file.
It's no hard to see that the our client application could work well also with a different server backend, with only minor changes to the configuration parameters.
Enjoy yourself with Weborb!
Comments (1)
Good tutorial. Its complete write up. Easy to understand. I am going to configure .net server. I hope i can do it easily without any problem.
Posted by r4i software | November 25, 2009 6:59 AM
Posted on November 25, 2009 06:59