Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I' trying to isolate a webservice in its own class, and I plan to add separate classes to each webmethod there is in the webservice. So far, this is what I've got. And it works, I have this feeling tickling that I've missed something. (Except for the invisible variable declarations down here, I didn't want to clog the page).

The following is my webservice instatiation class and its fault handler.

 public class CfdWS
        {

            [Bindable] 
            private var model:ModelLocator = ModelLocator.getInstance();

            public function loadWebService():WebService{
                var webService : WebService = new WebService();
                webService.wsdl = model.configXML.cfdwsWSDL;
                webService.addEventListener(FaultEvent.FAULT, onWebServiceFault);
                webService.loadWSDL();
                return webService;
            }

            private function onWebServiceFault(event:FaultEvent):void{
                var fault: Fault = event.fault;
                var message:String = "\ncodigo: " + fault.faultCode;
                message += "\nDetalle: " + fault.faultDetail;
                Alert.show("Error de webservice:" + message);
            }
        }
    }

The following is my webservice method call class. I have written only what I think is the essential code for the question.

public class GeneratePDF extends CfdWS{

    public function generatePDF():void{
                webService = loadWebService();
                webService.addEventListener(LoadEvent.LOAD, doGeneratePDF);
            }
            private function doGeneratePDF(event:LoadEvent):void{
                webService.generatePDF.addEventListener(ResultEvent.RESULT, generatePDFResultHandler);
                webService.generatePDF(pdfData);
            }
            private function generatePDFResultHandler (event:ResultEvent):void{
                // After getting what I want, I remove the event listeners here.
            }
}

Please don't murder me. This is my first attempt at "good practices" I've ever tried.

I'm trying to re-write an application that is already in production while on testing phase. Testing for the next version I mean.

share|improve this question

1 Answer 1

I don't see why would you put every method of the service in a separate class. A "method" is a function of the class. I imagine you wanted to decouple your code, but doing it this way you will force a lot of overhead:

  • the service is instantiated for every 'method' called, and then, hopefully, garbage collected (as you remove event listeners and there's no more references to the service left)
  • because of above, the service is stateless; with time you may want to add some functionality like caching, but you'd need to change whole code structure for that
  • CfdWS - not descriptive name; your way of decoupling code will force you to make three or even five times more classes then you normally would, so I would expect a hell on the file-naming level
  • really, dividing to so many classes is not a good idea - you don't want to switch between files all the time; try to put related code in one Class, and if it grows big, create some helper classes

I think you already understand the benefit of a good MVC implementation, try Robotlegs, it really makes a life easier: http://www.robotlegs.org/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.