The Architect

A Technical Architects Thoughts

January 27th, 2010

Recently I have been making the transition from PureMVC to Parsley, and have found many parallels regarding de-coupling and separation of concerns.

Just to provide some brief background:

PureMVC is essentially an observer Pattern.

  • The Model is a collection of proxies that proxy a data model.
  • The View is a collection of mediators that Manage the visuals (usually Components or mxml views).
  • The Controller is a collection of stateless commands that usually feed results from a remote call to a proxy or manage an update in a view through a proxy result.
  • Views and Proxies are referenced through a facade, that at its core is a singleton (e.g. facade.retreiveProxy(MyProxy.Name) as MyProxy);
  • Communication happens through a notification buss, where a dispatch of a notification will initiate a command, or activate a handler in a mediator.
  • A Proxy can issue a notification but not handle one.
  • A Command is instantiated through the dispatch of a facade registered command to notification.
  • A Mediator can issue and respond to notifications it has been told to respond to.
  • Parsley is an IOC Container and Messaging Framework

    As it is a Dependancy Inject pattern, it is a style of object configuration in which an objects fields and collaborators are set by an external entity. Parsley achieves this through allowing the coder to specify a configuration that holds information about what is to be injected and what message handlers may exist. But the code base of a project would be decorated by Injection tags, or Message tags.  There is NO direct coupling to the framework.    But the most noticeable difference between the two frameworks is the dependancy that exists on the PureMVC framework.

    PureMVC is a framework where you are constantly extending classes or implementing interfaces from the framework itself, thus leaving the project completely dependant on the framework, making it difficult to re-factor if a better framework were to come along.

    For example a command in PureMVC would have all this code to contend with:

    public static const MY_NOTIFICATION:String = "myNotification";

    // Facade declaration

    facade.registerCommmand(Notification.MY_NOTIFICATION, MyCommand);

    //Command Declaration

    public class MyCommand extends PureMVCSimpleCommand {

    public override function execute(notification:INotification):void {

    var med:MyMediator = facade.retrieveMediator(MyMediator.Name) as MyMediator;

    var proxy:Proxy = facade.retrieveProxy(MyProxy.Name) as My Proxy;

    proxy.doSomethingToUnderlyingModel();

    med.presentationModel = proxy.presentationModel;

    }

    }

    In the example above the code is heavily coupled to the framework, and is not easy to reuse as it uses specific class implementations ignoring the benefits of polymorphism.

    The same is achieved via the following in Parsley:

    // for the sake of demonstrating polymorphism

    Presentation Model implements IModel

    MyView implements IView

    // The configuration for the current context

    (mx:Object)

    (parlsey:Object type={PresentationModel}/)

    (parlsey:Object type={MyView}/)

    (parsley:Object type={MyCommand})

    (MessageHandler method="execute" scope="local"/)

    (/parsley>

    (/mx:Object)

    public class MyCommand {

    [Inject]

    public var model:IModel;

    [Inject]

    public var view:IView;

    public function execute(event:DataEvent):void {

    model.doSomething(event.data);

    view.model = model;

    }

    }

    The parsley example does a number of things well:

  • It makes the command itself reusable by implementing the interfaces specified.
  • The config can specifici any model or view to be injected as long as they implement the specified interfaces
  • The code’s simplified.
  • Apart from a couple of tags it is not coupled to the framework.
  • A lot of code is cut out, and it is even possible to scope the code down further from this example.
  • It handles the event provided by flex not a third party communication strategy.
  • The handler is called based on the event Type (you can specify it via the event name using a selector, but I prefer to do it via typing)!
  • The event is handled in the local context, thus being ignored by the global context of the application.
  • PureMVC does indeed do what it says on the tin, but in order to do so a huge dependancy is built on the framework itself, and a lot of code is needed to setup your application.  There is a danger with Parsley that a not so experienced developer could strongly couple models and views together as there is no clear convention like PureMVC, but on the flip side it is extremely easy to avoid as the object dependencies are not applied until specified by an external container.  And there is no reason not to keep the views and models separated with parsley.

    The Parsley framework is largely based on the excellent reflection framework (SpiceLib) available from spicefactory.org, which makes the injection model not only possible but also possible to extend.  Jens Halm (creator of SpiceLib and Parsley), makes it easy for developers to extend the framework via a clear documentation and supplies the source code as well.  If a developer wanted to create a new Tag for some kind of injection and is not sure how to go about it, just look to the online documentation and the code for the Parsley library.

    Conclusion

    It was a pleasure to have developed with PureMVC over three years, and the supporting documentation is the best I’ve seen.  Before Parsley existed I had to choose a framework for my developers in a previous role, and amongst its then competition Cairngorm it was an easy choice.  It clearly separated models from views and dealt with many good coding and architecting principles.  But there is an expensive future cost; unfortunately a huge dependancy is built on the framework, it would be mammoth task to re-factor away from PureMVC should you find something better or PureMVC is no longer supported.  PureMVC does provide a mechanism of reusing your code, but is diminished by the fact that it is easy to create mediators, proxies and even commands that are to specific to the context that they are intended for.  It has recently added the ability to allow communication between modules via its new pipe library, but its extremely cumbersome to use and almost impossible to debug.

    Parsley as a framework is impressive, as it manages to IOC ;-) itself away from the developers code base.  If in the future I need to migrate to a different framework, I only need to change the tags or manipulate the configuration to work with the new framework; but 95% of the code base does not have to change.  The developer is not forced to inherit or implement from the framework itself.  As a result of a decoupled framework there is no reason why this could not be used in congestion with other frameworks.  But Parsley unlike many other frameworks provides a clear and intuitive messaging buss.   Parsley also takes into account the the context of an application area, and enables the developer to dispatch or handle an event in the scope of a local context, parent, custom or global context.  This provides a communication mechanism between different parts of your application, using flex’s own event api.  For example when dealing with modules, each module will have its own individual scope, but can communicate with each other via a custom scope which contains the context of each module.  Parsley is powerful in terms of what it allows the developer to do using the clear approach of IOC.  There are almost no dependancies in the structure, and it facilitates a lot of functionality with very little code.  Classes can be reused using the injection framework and polymorphism, with a minimal effort.

  • admin (4)
  • 2 Responses to “ PureMVC to Parsley ”

    1. I am to a great extent impressed with the article I have just read. I wish the writer of http://www.sammur.com can continue to provide so much worthwhile information and unforgettable experience to http://www.sammur.com readers. There is not much to tell except the following universal truth: If a pointer can’t possibly be dereferenced…it will be. I will be back.

    2. Hey man I just wanted to say thanks for taking the time to create something worth my time to read. I am all over the net and I see so much useless junk that is just created for the sake of putting something new on their page. It takes devotion to create good stuff, thanks for caring.

    Leave a Reply