Flex.NET remoting methodology – coding standards and best practises

27 03 2008

I’ve been dabbling with different methodologies into Flex.NET remoting, and trying to find a coding standard that works and is appropriate for this technology.

I’ve provided my solution in the hope that it will spur comments and generate other opinions in this rather new field.

One of the annoying things with Flex remoting is having to define the same object twice – once in C# and once in ActionScript 3 (AS3). Obviously as good coders, we know this to be a no-no.

Of course, there are codegen solutions out there, but I’ve been dissatisfied with them, and prefer the manual touch.

This is what I’ve come up with:

  1. In Visual Studio, create a DataSet that interfaces with the database, exposing specific TableAdapters to each of your objects.
  2. In both Flex and .NET, create an object that spans across the two platforms. As it’s defined twice, use some codegen tool to generate the class in both C# and AS3.
  3. In your .NET library, create a “Service” for the action that provides the ability to interface between the client (Flex) and the server (.NET). This service talks to the DB via the above mentioned TableAdapters.
  4. In your Flex application, create a “Service” that mimics the methods of the service in .NET that you want to access

Don’t worry, I never read those summaries either. Here’s the example that explains it all:

Problem: I want to modify a User object across the platform

Solution:

  1. Create my DAL by adding a DataSet to my Service Layer (the assembly that will handle all the .NET interfacing). In the DAL, create a UserTableAdapter and expose a Get method called : GetUserByID(int userID).
  2. Create the object in C# and is AS3 . Note the use of the compiler directive “RemoteClass” in the AS3 is the syntax for WebORB’s remoting solution.
    C# (User.cs)

    namespace JustinJMoses.Examples.RemotingExample.Objects
    {
    public class User
    {
    public int UserID = -1;

           public String GivenNames;

           public String Surname;

           public String Email;
    }
    }

    AS3 (User.as)

    package com.justinjmoses.examples.remotingexample

    {

    [RemoteClass(alias=”JustinJMoses.Examples.RemotingExample.Objects.User”)]

    public class User

    {

    public var UserID:int = -1;

    public var Email:String;

    public var GivenNames:String;

    public var Surname:String;

    }

    }

  3. Make a service for the User in C# – UserService.cs

    using JustinJMoses.Examples.RemotingExample.Objects;

    using JustinJMoses.Examples.RemotingExample.DAL;

    using JustinJMoses.Examples.RemotingExample.DAL.MyDALTableAdapters;

    namespace JustinJMoses.Examples.RemotingExample.Services
    {

    public class UserService
    {

    public User Load(int UserID)
    {

    UserTableAdapter adapter = new UserTableAdapter();

    MyDAL.UserDataTable table = adapter.GetUserByID(UserID);

    MyDAL.UserRow row = (MyDAL.UserRow)table.Rows[0];

    User u = new User();

    u.UserID = row.UserID;

    u.GivenNames = row.GivenNames;

    u.Surname = row.Surname;

    u.Email = row.Email;

    return u;

    }

    }

    }

  4. Make a service for the Flex app in AS3: UserService.as

    package com.justinjmoses.examples.remotingexample
    {

    import mx.rpc.events.ResultEvent;

    public class UserService extends BaseService
    {

    public static function Load(onResultFunction:Function,userID:int):void
    {

    init(“UserService”);

    remoteObject.Load.addEventListener(ResultEvent.RESULT,onResultFunction);

    remoteObject.Load(userID);

    }

    }

    }

    that extends from the base service class BaseService.as

    package com.justinjmoses.examples.remotingexample
    {

    import mx.controls.Alert;

    import mx.core.Application;

    import mx.rpc.events.FaultEvent;

    import mx.rpc.events.InvokeEvent;

    import mx.rpc.events.ResultEvent;

    import mx.rpc.remoting.mxml.RemoteObject;

    public class BaseService

    {

    protected static var remoteObject:RemoteObject = null;

    protected static function init(serviceName:String):void

    {

    remoteObject = null;

    remoteObject = new RemoteObject(“GenericDestination”);

    remoteObject.showBusyCursor = true;

    remoteObject.addEventListener(FaultEvent.FAULT, BaseService.onFault);

    remoteObject.source = “JustinJMoses.Examples.RemotingExample.Services.” + serviceName;

    }

    protected static function onFault (event:FaultEvent):void

    {

    Alert.show(event.fault.faultString, “Error”);

    }

    }

    }

  5. The function can now be called statically in AS3

    private function onCrtComplete():void
    {

    UserService.Load(onUserLoaded, 12);

    }

    private function onUserLoaded(evt:ResultEvent):void
    {

    var user:User = User(evt.result);
    }

NOTE: This soln is done via WebORB remoting. Syntax includes the remote object destination of “Generic Destination”, and the compiler directive “RemoteObject()” in AS3.

Advertisements