0

I want my mxml or actionscript components to be reusable and loosly coupled. I am wondering if it is good practice to use the FlexGlobals.topApplication to dispatch and listen for events. For instance I want my login component to dispatch events to the topApplication so when i reuse that component in a different project I won't have to change anything being all applications have a topApplication.
My other option is to have a separate static class to handle event dispatching but then I am creating a dependency on that static class. Any suggestions would be appreciated. Thanks.

  • Ever considered using an existing MVC framework? RobotLegs, for example, comes with a central event dispatcher and dependency injection... – weltraumpirat Feb 23 '12 at 21:37
  • I haven't really had a need for it being i am a sole developer but I think I will check it out, thanks. – Phxmobdev Developer Feb 23 '12 at 22:36
  • 1
    You'll be working far more efficiently, lone wolf or not, if you use tools that have been tested, used and optimized by many others ;) – weltraumpirat Feb 23 '12 at 22:40
  • @weltraumpirat I agree with that statement but I don't think any of the MVC libraries out there have really been that well tested or developed upon (lots of flux). Flex itself has, which is why I consider it a viable solution to many problems. I've tried my hand at RobotLegs and SpringAS and lots of other libraries but always find that they fall short with regard to being used with Flex modules or in other use cases that weren't originally accounted for. It just seems Java has had much longer to brew (pun totally intended) and the frameworks can be trusted to work in far more use cases :) – shaunhusain Feb 24 '12 at 00:30
  • 1
    @shaunhusain The Flex modules case is true for RobotLegs 1 (though there was modular-robotlegs - I've used that quite successfully), but should no longer be of any concern with RobotLegs 2, which has greatly improved in regards to modularization. – weltraumpirat Feb 24 '12 at 00:34
  • @weltraumpirat good to know I'll have to check that out at some point, however that sort of is supporting my point that these things are still a bit infantile. The same is true of SpringAS where they were planning to introduce a feature we needed in the next version but we had no guarantees on when that would actually be released, but I hear your points for sure (+1ing em). – shaunhusain Feb 24 '12 at 00:42
  • Flex itself actually _cannot_ have unit tests due to the way it has written (arguably it uses really bad practices in many places), whereas many of the automated dependency injection frameworks have excellent test coverage. – Amy Blankenship Feb 24 '12 at 01:38
  • @AmyBlankenship really because we use flex unit for unit testing and we don't use any third party MVC frameworks, admittedly we only have 108 tests on one of our libraries, not full coverage but that's its own issue http://opensource.adobe.com/wiki/display/flexunit/FlexUnit – shaunhusain Feb 24 '12 at 22:13
  • @AmyBlankenship, I think I was being sort of harsh in my last comment no disrespect meant just speaking from my current experience (about 3 years as an enterprise flex and sort of sometimes java developer) I've been to your blog and appreciate your knowledge, opinions, and help to the community – shaunhusain Feb 24 '12 at 22:54
  • shaun... Flex and FlexUnit are not the same thing. FlexUnit was primarily developed by Michael Labriola, who also led the charge to get Flex itself open sourced (in part so that it _could_ be brought into a state where it could be unit tested). You haven't lived until you have to try to work around a bug in UIDUtil by trying to wedge some compensating code into TileList. I am friends with many of the developers in the open source projects as well as some of the Adobe devs, and I know who writes the cleaner code. – Amy Blankenship Feb 24 '12 at 23:03

2 Answers2

2

I would recommend that you read about event propagation and have your login component dispatch the event to "whoever" catches it as it bubbles up through the hierarchy.

http://livedocs.adobe.com/flex/3/html/help.html?content=events_08.html

Stian
  • 1,261
  • 2
  • 19
  • 38
  • sure when it is in the hierarchy but what if there are 2 hierarchy's parallel to each other? Then i would be forced up to the topApplication and was wondering if that is bad practice? – Phxmobdev Developer Feb 23 '12 at 22:32
0

I have to agree with the answer by Stian here for the most part. With regard to weltraumpirat's comment I feel dependency injection can be great but also adds a lot of complication with regard to debugging/testing IMO and if you're not actually going to have different implementations of an interface just adds a lot of garbage code to look through without any real benefit. I feel like Spring on the service layer side works out well because you can change out implementations for the data access layer (DAO) if you switch DBs or something of that nature but it's hard for me to see the benefit on the front-end.

I would not recommend using the topLevelApplication as you'll end up with something like cairngorm where you have this humongous set of events/event handlers happening at the top level. Not to mention if you follow their suggested model you end up with a bunch of pointless event classes that simply define a string (there's better and worse ways to go about it using Cairngorm but I'm not a fan of what I've seen in the wild).

A developer at my company wrote a custom MVC "micro-framework" that works great for us where we can attach a controller to any display object to handle events for it, this works wonderfully but does require the initial overhead of developing/testing it. It's built on top of the existing Event scheme in Flex so our MVCEvent class extends Event (ours just bubble by default as we tend to want this for the types of events we're creating where the controller could be at any level above the UIComponent dispatching the event, and can always opt to turn off bubbling, however starting with the Event base class means we can utilitze the built in EventDispatcher dispatchEvent() method). He wrote just about everything using an interface to define the methods for each part and only assuming objects implement a given interface to be used in a particular context (as in IMVCEvent, IMVCCommand) this way if the built in framework implementation doesn't work for your particular scenario you just need to create a new class that implements the same interface (if extension also doesn't work for your case). This gives a huge amount of flexibility yet at the same time we're generally able to just re-use existing implementations of events, commands, or controllers. In each app we're just defining new views and commands for things that are specific to the business rules of the application.

So what's that all boil down to, I suggest you roll your own as a library then re-use that library for your many projects. You will know your own library in and out and can tweak it as you see fit quickly without trying to understand the many use cases someone designed their MVC framework to handle.

I realize this isn't an ideal solution in terms of speed to get something done now, but I think it really is the best solution for the long haul (it's been great for us that's really all I can say).

Amendment here to acknowledge the existing Flex MVC frameworks available and appease the crowd.

Robot Legs By the way see what the creator of robot legs has to say about using his code: His words not mine

Swiz

Mate

Stackoverflow question about flex frameworks

Community
  • 1
  • 1
shaunhusain
  • 19,630
  • 4
  • 38
  • 51
  • Wow thanks so much, all of the comments here definitely helped thank you so much. I couldn't agree more with everything you said. – Phxmobdev Developer Feb 23 '12 at 23:22
  • @PhxmobdevDeveloper thanks I'm glad you found this useful and didn't get angry I didn't go with the "normal developer answer." Although my answer is opinionated, the opinion is based on experience. We originally used Cairngorm and although it served the purpose it also made a mess of things. Anyhow always nice to get a Wow as a response, best of luck. – shaunhusain Feb 24 '12 at 00:35
  • If you're going to do this, you're better off using Robotlegs, which already has a "mini controller" for Views, known as a Mediator. If you use RL, you then have the choice of explicitly communicating through the dedicated Event bus or using the Display list for communication. – Amy Blankenship Feb 24 '12 at 01:41