Is injecting a model into a service an anitipattern
-
30-06-2021 - |
Question
Some services in my Robotlegs app require parameters. I'm wondering which solution is better:
- Mediators pass parameters to services
- Services aquire parameters from injected models
Solution
As Creynders suggested, it depends on the scope of these variables, if they are const, model, or user input.
A great resource for me has been the ActionScript Developer's Guide to RobotLegs: http://books.google.ca/books/about/ActionScript_Developer_s_Guide_to_Robotl.html?id=PFA2TWqZdSMC&redir_esc=y
This is my usual workflow:
- View dispatches a custom event and passes parameters to the event.
- Mediator listens to the Event and re-dispatches it.
- Context maps the event to a command.
- Command injects the event, any necessary models, and the service.
Command calls the service, passing any necessary parameters. In the example below, I am passing a variable from the LoadLicenseEvent and from the ITokenModel to the service call. I use commandMap.detain() and commandMap.release() to keep the command alive until the service call is complete. The base class ServiceModuleCommand handles the fault event.
public class LoadLicenseCommand extends ServiceModuleCommand { [Inject] public var event:LoadLicenseEvent; [Inject] public var service:ILicenseService; [Inject] public var tokenModel:ITokenModel; [Inject] public var licenseModel:ILicenseModel; public override function execute():void { commandMap.detain(this); var token:TokenVO = tokenModel.getToken(); var asyncToken:AsyncToken = service.getLicense(token.Id, event.id); asyncToken.addResponder(new mx.rpc.Responder(resultHandler, faultHandler)); } private function resultHandler(e:ResultEvent):void { var license:LicenseWebViewVO = e.result as LicenseWebViewVO; if (license) { licenseModel.license = license; dispatchToModules(new RunWidgetEvent(WidgetType.getWidgetId(WidgetType.LICENSE))); } commandMap.release(this); }