explorers' club

explorations in dev, science, sci-fi, games, and other fun stuff!

Tutorial: handling service validation errors & cairngorm – part 2

3 Comments

recap

So last we left off we had discussed the reference method for passing info back and forth to the relevant view notifying the user about needed data changes. If you need a refresher check part 1. This next way of handling service validation errors is very simple, however it requires a bit more work.

central event dispatcher method

This method requires two additional classes which we will call something like ServiceValidationEventDispatcher & ServiceValidationErrorEvent. I am not too good at naming things off the top of my head so feel free to lay into me on this one. The point is this: We want a globally accessible IEventDispatcher class we can tap into from both the views and the commands. And we want an event that we can attach some data to in order to pass it back. The SVED is pretty straight forward so I won’t create that here. Just know its an IEventDispatcher and I would make it a Singleton-type class. The new event class should probably look kinda like this:

package com.foo.events
{
     public class ServiceValidationErrorEvent extends Event
     {
          static public const SERVICE_VALIDATION_ERRORS:String = "serviceValidationErrors";

          public var errors:Array = [];
     }
}

hooking it up

We are hanging onto the interface from part 1 but we’re gonna modify it for this particular implementation. Instead of the passing the array to the handleValidationErrors, we are gonna pass it a ServiceValidationErrorEvent as the parameter.

Another change we do is instead of passing the view as a value on the Cairngorm event, we will tell the ServiceValidationEventDispatcher instance to register our handleValidationError method for the ServiceValidationErrorEvent:

ServiceValidationEventDispatcher.getInstance().addEventListener(ServiceValidationErrorEvent.SERVICE_VALIDATION_ERRORS, handleValidationError);

Now at this point we have a few choices. We need to make sure that we are doing our best to preserve memory and garbage collection. One way is to use a weakReference when adding the event handler. I am not a fan of this because a) we cannot control garbage collection and b) it leaves too many doors open for timing issues.

Another means would be to make sure that we always trigger the handleValidationError method on our view, checking the errors array’s length. For one reason we will be removing the event handler from the SVED. But this means then we always have to trigger it from the SVED within the command regardless of the response’s success. The other way would be to have yet another handler on the view for successful validation so that you can go ahead and remove the handler. This means fattening up our interface. There are so many little things you could do here, but the main purpose is that we are using events to trigger out handlers rather than direct triggering via a reference.

summary

I really don’t dig this implementation because it adds more classes and more crap to remember to do. But here is a pro/con list for this method:

pros:

  • better OOP than the ref method
  • decouples the view from the command
  • leverages the event driven nature of the flash player

cons:

  • adds additional weight to your project
  • prone to timing issues
  • more stuff to remember to do

Ok so part 3 is coming next. It will basically be a blending of the two previous methods. See ya then

Advertisements

3 thoughts on “Tutorial: handling service validation errors & cairngorm – part 2

  1. Pingback: Tutorial: handling service validation errors & cairngorm - part 3 « explorers’ club

  2. very nice approach basically i would say better separation between view and command.
    but i don’t agrees with cons you have suggested, it will just add two or three more classes, i don’t think we do have to remember lots of extra things,

  3. well,if you talk about AS2/ARP I agree, important thing. And if you talk about extra things like icons, it will be never less for the users, but any of those additional weight that will add to your project might be not very actual or cheap. Of course it depends on what project you have and how profitable it is. But in general this post is quite interesting. Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s