15.4. Delegate Classes

Now that you have the event and command classes created, you are going to create a delegate to access the service.

In the com.FlexBlog.delegates package create a class named RegisterUserDelegate and edit it to match the following:

package com.FlexBlog.delegates
{
    import com.FlexBlog.valueobjects.UserVO;
    import flash.errors.SQLError;
    import mx.rpc.IResponder;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import sql.FlexBlogDatabaseManager;
    public class RegisterUserDelegate
    {
        private var responder:IResponder;
        public function RegisterUserDelegate(responder:IResponder)
        {
            this.responder = responder;
        }
        public function register(user:UserVO):void{
            var dbManager:FlexBlogDatabaseManager =
FlexBlogDatabaseManager.getInstance();
            try{
            var userAdded:int =  dbManager.registerUser(user);
            var event:ResultEvent = new
ResultEvent(ResultEvent.RESULT,false,true,{added:userAdded})
            this.responder.result(event)
            }catch(error:SQLError){
                var faultEvent:FaultEvent = new FaultEvent(FaultEvent.FAULT);
                this.responder.fault(faultEvent);
            }
        }
    }
}

Here we are seeing the first occurrence of interaction with the backend using the SQL utility classes. The register method takes in a UserVO as a parameter and passes it to the registerUser method of the FlexBlogDatabaseManager.

The registerUser method returns an integer indicating the success or failure of the add user operation. If an account with the supplied user name and password exists, you will get back a value of −1; ...

Get Professional Cairngorm™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.