Mate is one of the best framework i have ever played with. It is completely unobtrusive. My application code has no dependencies on the framework, it is highly decoupled, reusable and testable. Mate framework is developed by AsFusion.

Let me point out why i have switched over to Mate over Cairngorm

1. You can dispatch an event from anywhere in the view hierarchy and have it bubble up to the framework automatically, instead of having to have a direct line, like Cairngorms CairngormEventDispatcher or PureMVC’s notification system.

2. Mate is extremely light and readable, being a Flex framework which is built on the Event Map idea. This is a collation of handlers that can respond to all the incoming events and complete several actions that are appropriate for the target.

3. By using the Mate framework, you will need no more the Singletons. I have seen a quite a few people using Singleton pattern, when it is actually not needed in the application. Now Mate completely removes it, which actually makes development further easier.

4. Mate also uses a form of dependency injection (leveraging bindings) that makes it possible to connect your models to your views without either one knowing about the other. This is probably the most powerful feature of the framework.

5. The RIA development is most certainly more interesting and flexible with Mate.

My View on Cairngorm

Cairngorm is a bundle of anti-patterns that lead to applications that are tightly coupled to global variables.

Let me explain you the flow on how Mate really goes, its so easy and interesting like RajiniKanth sir Movie.

This tutorial explains how to get started with Mate. As an example, we’ll be sending two numbers to our manager class to add , receives the total value and stores it in the model for the view to show.

All Mate projects must have:

  1. One or more events (custom or built-in)
  2. One or more Event Maps

Let us go step by step

Create a new Flex project named AdditionExample. In the libs folder it creates, place the compiled framework SWC (Mate.swc). This will let you use all Mate classes and tags.Create a folder structure as displayed in the image below, i will explain you how the entire flow goes.

The custom event

Every Mate project is driven by events. In our example, when user enters two values and clicks on the Add button, we’ll create a new event containing that information that will be sent to the manager class to perform the addiiton. For this purpose, we create a custom event to indicate that the user wants to submit the data and retrieve the total.

Our event will be very simple and it will contain two property: the number1 and number2.

package com.vinoth.example.events
{
import flash.events.Event;

public class AdditionEvent extends Event
{
public static const GET: String = “getQuoteEvent”;

public var number1 : Number;
public var number2 : Number;

public function AdditionEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}

}
}

The code above assumes this event is contained within the package: com.vinoth.example.events

The event also contains a constant that we will use to specify the event type. One event can specify more than one event type. We also make this event bubble up by default (the second argument of the constructor). Otherwise, we will need to remember to specify it when we instantiate it.

The View

The user interface will only need two text input and a button.

Dispatching the Custom Event

When the user clicks the Add button, we’ll create the AdditionEvent and dispatch it:

package com.vinoth.example.events
{
import flash.events.Event;

public class AdditionEvent extends Event
{
public static const GET: String = “getQuoteEvent”;

public var number1 : Number;
public var number2 : Number;

public function AdditionEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}

}
}

The Event Map

EventMap is a place where the we place all the handlers for the events which the application creates. There are be n number of event map. Now let me put down before you on how to create an event map.

We need to create a MXML file with name”MainEventMap”. This MXML component must extend from EventMap.

<?xml version="1.0" encoding="utf-8"?>
<EventMap
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns="http://mate.asfusion.com/">

</EventMap>

This is how your blank event map file will look like. We’ll add the event map to our main Application file.

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”
xmlns:maps=”com.vinoth.example.maps.*”
>

<!– Styles______________________________________________________ –>

<mx:Style source=”/assets/styles/main.css”/>

<!– Event Maps __________________________________________________ –>

<maps:MainEventMap />

</mx:Application>

Now here a question might rise, how could i know whether my event map is receiving the events being dispatched.

<Debugger level="{Debugger.ALL}" />

This Tag does come handy and helps you in debugging whether you are going on the right way. Adding this to the event map looks like this.

<?xml version="1.0" encoding="utf-8"?>
<EventMap
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns="http://mate.asfusion.com/">

<Debugger level="{Debugger.ALL}" />

</EventMap>

Listening for AdditionEvent.GET

In our event map, we will listen for the addition event so that we can send the request to the manager class. We’ll add EventHandlers tag which will specify what event type we are listening too.

<EventHandlers type="{AdditionEvent.GET}" debug="true">

</EventHandlers>

At the top of the event map we’ll need to import the event class.

<mx:Script>
<![CDATA[
import com.vinoth.example.events.AdditionEvent
]]>
</mx:Script>

Inside this EventHandlers block, we’ll place the actions we want to perform when the event is dispatched. In our example we will call the method addValues in the AddManager Class.

<EventHandlers type=”{AdditionEvent.GET}” debug=”true”>
<MethodInvoker generator=”{AddManager}”
method=”addValues” arguments=”{[event.number1,event.number2]}”/>
</EventHandlers>

We are calling the method addValues on that service and sending the values coming from the event as an argument.

Creating our Business, the AddManager

package com.vinoth.example.business
{
import mx.controls.Alert;

public class AddManager
{

[Bindable]
public var totalValue:Number;

// ——————————————–
public function addValues(number1:Number,number2:Number):void {
totalValue = number1+number2;

}

}
}

In this Class we are getting the two numbers as arguments from the event class which dispatches and then we are doing our calculation. Once the value is calculated, we will inject the result in our view.

<Injectors target=”{YourViewPanel}”>
<PropertyInjector targetKey=”value” source=”{AddManager}” sourceKey=”totalValue” />
</Injectors>

This makes life simple, here we get the final result. You can have a look at the application and view source is enabled.

Application