Type Adaptation

Both JavaScript and ActionScript environments support a limited number of data types. For example, there is only one type representing a number, regardless whether the number is a short, an integer or a float. Complex types can be described as associative arrays, anonymous objects or specific instances of JS or AS classes. Since .NET is a strongly-typed environment, the arguments passed into the method on the client side must be adapted to the formal argument types of the server-side method. Consider the following example:

The code below returns the size of the array passed as the argument:

C# method
public int getArraySize( ArrayList arrayObject )
{
  return arrayObject.Count;
}

To invoke the method above from a rich client, the following code may be used:

Flash Client   AJAX Client
// create an instance of array
var arrayObject = new Array();
arrayObject[ 0 ] = "new york";
arrayObject[ 1 ] = "paris";
arrayObject[ 2 ] = "london";

var cnxn = NetServices.createGatewayConnection( "http://host:port/weborb.aspx" );
var remoteService = cnxn.getService( "ArraySizeService" );
remoteService.getArraySize( arrayObject );

// process method invocation response
function getArraySize_Result( result )
  {
  trace( "array size is " + result );
  }
  var webORBURL = "http://host:port/weborb.aspx" );

var remoteService = webORB.bind( "ArraySizeService", webORBURL );

var arrayObj = Array( "new york", "paris", "london" );

var arraySize = remoteService.getArraySize( arrayObj );

alert( "array size is " + arraySize );

In the example above, client code creates an instance of an ActionScript or JavaScript array, while the server-side method accepts Vector or ArrayList as an argument. WebORB automatically converts data received from clients to the types expected by the invoked method. The process of data type conversion is called type adaptation. WebORB's type adaptation capabilities supports conversion of all primitive types, collections, complex types and arrays. The same data type received from a client can be adapted to multiple data types from the backend object depending on the method's formal argument types. For example, if the server-side method above is to be rewritten as below, WebORB would automatically handle the change.

C# method
public int getArraySize( string[] arrayObject )
{
  return arrayObject.Length;
}

Below is a summary of all supported type adaptation mappings. Items in the Client Type column are the data types created in the client's ActionScript or JavaScript. Items in the .NET type columns are the data types to which the corresponding client arguments can be automatically converted. For example, if a client sends a String as an argument to a method, the corresponding .NET method argument can be any of the following types: string, System.Text.StringBuilder, char[], byte[], char, Boolean.

Client Type

If client sends an argument of a  type below, the object can be converted to the corresponding types on the right
.NET Type

If the client used the type on the left, WebORB can convert the object to the corresponding type in this column

Number Boolean
SByte
Byte
Int16
UInt16
Int32
UInt32
Int64
UInt64
Single
Double
Decimal
Char
String
System.Text.StringBuilder
System.IConvertible or any implementation of the interface
Array any type where IsArray is true
all instances of System.Collections.IList
System.Collections.Queue (and subclasses)
System.Collections.Stack (and subclasses)
System.Collections.SortedList (and subclasses)
 
String string
System.Text.StringBuilder
char[]
byte[]
char
System.Boolean
Object any .NET object with a default no argument constructor
XML Document (supported in Beta2) System.Xml.XmlDocument
System.Xml.XmlElement
System.Xml.XmlNode
string
Date System.DateTime
Int64
UInt64
Boolean bool
Boolean
string ("true" or "false")
undefined converted to null

For more information about type adaptation and argument serialization, refer to the links below: