Mapping a mediator to an Interface

Thijs Broerse's Avatar

Thijs Broerse

30 Aug, 2010 10:30 AM

Hi, for a project I need automatic mediator mapping based on an Interface of the view. Since the current version of Robotlegs doesn't support this, I build it myself. What I did:

Create an Interface "IMediatorMappable":

package org.robotlegs.core 
{
    public interface IMediatorMappable 
    {
        function get type():Class;
    }
}

And made some changes in the 'createMediator' method and the 'onViewAdded' method in the MediatorMap class (see attachment, changes are between "// TyZ Hack start" and "// TyZ Hack end")

    protected override function onViewAdded(e:Event):void
    {
        if (mediatorsMarkedForRemoval[e.target])
        {
            delete mediatorsMarkedForRemoval[e.target];
            return;
        }
        var config:MappingConfig = mappingConfigByViewClassName[getQualifiedClassName(e.target)];

        // TyZ Hack start
        if (!config && e.target is IMediatorMappable)
        {
            config = mappingConfigByViewClassName[getQualifiedClassName(IMediatorMappable(e.target).type)];
        }
        // TyZ Hack end

        if (config && config.autoCreate)
        {
            createMediator(e.target);
        }
    }

    public function createMediator(viewComponent:Object):IMediator
    {
        var mediator:IMediator = mediatorByView[viewComponent];
        if (mediator == null)
        {
            var viewClassName:String = getQualifiedClassName(viewComponent);
            var config:MappingConfig = mappingConfigByViewClassName[viewClassName];

            // TyZ Hack start
            if (!config && viewComponent is IMediatorMappable)
            {
                config = mappingConfigByViewClassName[getQualifiedClassName(IMediatorMappable(viewComponent).type)];
            }
            // TyZ Hack end

            if (config)
            {
                injector.mapValue(config.typedViewClass, viewComponent);
                mediator = injector.instantiate(config.mediatorClass);
                injector.unmap(config.typedViewClass);
                registerMediator(viewComponent, mediator);
            }
        }
        return mediator;
    }

To get this working the view object should implement IMediatorMappable and return the Interface for the Mediator mapping.

Example:
I have a 'Parcel' object[1] and a 'IParcel' interface and a 'ParcelMediator'. The Parcel class implements IParcel and IMediatorMappable and returns IParcel in the type getter:

public function get type():Class
{
    return IParcel;
}

Now I can automatically map all objects of type IParcel to the ParcelMediator:

this.mediatorMap.mapView(IParcel, ParcelMediator, IParcel);

I would like to know what you think about this solution? And I also wonder why something like this isn't already in Robotlegs. Maybe I overlooked something?

[1] The Parcel class is always used as base class, since there are multiple parcels in my project

  1. Stray closed this discussion on 16 Feb, 2011 04:50 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac