I'm trying to isolate a webservice in its own class, and I plan to add separate classes to each webmethod there is in the webservice. What I have so far works, but 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).
Webservice instantiation 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.
}
}
I'm trying to re-write an application that is already in production while on testing phase (testing for the next version I mean).
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/