As of version 0.3, AMF.NET supports typed objects as inputs into your function. This further enables
AMF.NET to act as an invisible gateway between ActionScript and business layer. Although previous versions
had experimental support, the 0.3 model is more flexible and powerful. However, it requires more work to
set up.
The previous support fot typed objects (which is still supported), required the ActionScript class to have
a special field named
__NetType. This field would need to be the fully quantified .NET class name.
An instance of the type would dynamically be created, and all other ActionScript fields would be mapped
to their .NET counterpart (based on their name).
Although automatic, this approach is clumsy, requiring special knowledge of .NET in ActionScript and an agreed
upon naming convention between the two layers.
The new approach requires that you write your own mapper (called a Reconstitutor) that'll create
an .NET class based on an ActionScript input (represented as a Hashtable). AMF.NET contains an IReconstitutor
interface which you must implement for each type you want mapped. A BitmapReconstitutor is also provided
to serve as an example.
Step 1
The first, and most crucial step is to create your own IReconstitutor for your particular type. This class
should exist within your project - not within AMF.NET. The IReconstutor interface exposes 2 methods and 1 property
which you must implement: ParametersAreValid, Reconstitute and RegisteredName.
For this example, we'll create a simple Reconstitutor for a User class. For more examples, I suggest you take a look
at the BitmapReconstitutor.
RegisteredName is the name of the registered ActionScript class (more on this later). This will be used to load the appropriate
Reconstitutor for a given typed object:
public string RegisteredName
{
get { return "User"; }
}
ParametersAreValid validates that the Hash contains all the necessary information to actual create our .NET class:
public bool ParametersAreValid(Hashtable parameters)
{
if (parameters == null)
{
return false;
}
if (!parameters.ContainsKey("__name") || !parameters.ContainsKey("__address") || !parameters.ContainsKey("__id"))
{
return false;
}
return true;
}
Reconstitute will actually create and return your typed object:
public object Reconstitute(Hashtable parameters)
{
int userId;
//this check could be moved to ParametersAreValid
if (!Int32.TryParse(parameters["__id"].ToString(), out userId))
{
return null;
}
User user = new User(userId);
user.Name = parameters["__name"];
user.Address = parameters["__address"];
return user;
}
Step 2
The next step is to make AMF.NET aware of your Reconstitutor. This is accomplished via your application's configuration file (web.config or app.config).
Directly below your root
configuration node, register the AMF.NET configuration section:
<configSections>
<sectionGroup name="Fuel">
<section name="AmfNet" type="Fuel.AmfNet.AmfNetConfiguration, Fuel.AmfNet" />
</sectionGroup>
</configSections>
(note, if you already have a configSection, you'll want to add the inner nodes into your existing one)
Now add the AMF.NET section before the closing
configuration node:
<Fuel>
<AmfNet>
<reconstitutor>
<add type="MyCompany.MyProject.User, MyCompany.MyProjectDllName" />
</AmfNet>
</Fuel>
You'll need to replace the
type with the typeName for your Reconstitutor (it should be
NameSpace.Class, AssemblyName).
Step 3
The final step is to register your class within ActionScript. The name you give your class
MUST match your reconstitutors RegisteredName property:
Object.registerClass( "User", com.fuel.user );