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

Actions

Information

2 responses

30 06 2009
BestbuyerNC

Hi Justin. Has any of your opinions that you posted here changed? I am looking for best practices. Would your opinion after some time still be as stated here? Thanks.

30 06 2009
Justin J. Moses

Hello there BestbuyerNC,

Good question. Actually, my opinion has changed somewhat.

These days I typically use LINQ to Flex (.NET 3.5). As such, I don’t use .NET DataSets anymore but rather LinqToSql or LinqToEntities. Using LINQ, I don’t need to write my object classes in C# anymore as they are automatically created via LINQ (eg. User.cs from above is now automated).

I regularly use WebORB .NET to provide the interface between Flash & .NET, though there is also the option of FluorineFx.

Then in Flex, instead of writing those Service classes above, I would typically use a framework – such as Cairngorm or Mate – which provide their own mechanisms for calling remote services.

Have a look through my blog for the entries on LINQ to Flex, these may be of interest to you.

Hope this helps,
justin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: