Forums » General

PureMVC Mediator Equivalent

(7 posts)
  1. ZaBlanc
    Member

    I'm very familiar with PureMVC, and particularly love the Mediator concept. I'm trying to formulate the Mate equivalent.

    The Mediator is fired just like a Command, by a notification. The difference is that the Mediator knows about views. A command does not.

    One thing I see is that the <Injectors> supports a <PropertyInjector>, which looks like it sets a field for EVERY view of that type. This is roughly what I want, except for two things:

    1) I have to set a bindable variable to have this fired.
    2) It's setting a field, not calling a method.

    So, two questions:

    1) Is there a way to set up an Injector so it fires a method (ala BindSetter) on my View rather than setting a variable?

    2) Can I have the method fire straight from an EventHandler, rather than having to first set a field so the Injector fires?

    It seems <Injectors> only supports PropertyInjector, though the docs "suggest" otherwise. Is this true?

    Posted 1 year ago #
  2. I would answer no to both your questions. There might be ways of doing it, but that would negate many of the benefits of using Mate. PureMVC and Mate are very different application frameworks and many things are fundamentally different.

    As I have understood PureMVC, mediators are used to update the views when the model has changed. The way they do this is by respoding to events and pushing data into the views. In Mate this is instead done using bindings. The tags Injectors and PropertyInjector is the means by which these bindings are set up.

    In response to an event you should call methods on a manager object, and let the view be notified that the manager has changed by way of bindings.

    Say that you have an event "removeEmployee", this could be caught by an EventHandlers which contains a MethodInvoker that calls a method called "removeEmployee" on a manager of the type EmployeeManager. The empoyee manager updates it's state accordingly, removing the employee from its list of employees. This doesn't update the view directly, in fact the view isn't involved in this process at all.

    Elsewhere in the event map we can set up an Injectors tag for an EmployeeListView. The tag contains a PropertyInjector that binds the view's "employeeList" property to the employee manager's "employeeList" property.

    This means that there is no need for a mediator which explicitly updates the view. The view gets updated implicitly when the manager object changes. This leads to less "glue code" and a more decoupled architecture. The downside is that it is more indirect and it can be hard to understand how things happen, when and why (and why not) they happen.

    I hope that this explains some of what you were wondering about.

    By the way, there is one aspect of PureMVC's mediators that isn't covered in the discussion above. Mediators also insulate the views from the rest of the application. There is no equivalent in the Mate framework, but Nahuel and I have been discussing how to use the Presentation Model pattern in Mate (see http://mate.asfusion.com/forums/topic.php?id=40), although I think we've agreed to call it "model adapter". The idea is that there is an object mediating between the view and the application. It adapts the application model to something that the view can use, and it translates the actions the user performs on the view into application events (a.k.a. "gestures"). The fundamental difference to PureMVC's mediators is that the view knows about the adapter, but the adapter doesn't know about the view. In fact, the adapter only knows about the things that are injected into it (by the same Injectors as in my example above, that is, instead of the view being bound to the employee manager, the adapter is, and the view binds to the adapter).

    Posted 1 year ago #
  3. ZaBlanc
    Member

    Thanks, this is a lot of information to go on. I've come to grips with the fact that my mind will have to think differently after a year of PureMVC.

    PureMVC Mediators can do anything they want when notified. So, they can push data (if they haven't dine any binding) or they can directly manipulate the view by, say, disabling fields, moving, etc.

    With Mate, it seems that the only notification to a view of anything is via data binding, or Injectors. That's it. It seems limiting. I'd likely be more inclined to rely on singleton models and use bindSetters. But, I'm not ruling out that what I do with Mediators can't necessarily be redone to fit the Mate model.

    Posted 1 year ago #
  4. Yes, there is a lot to digest. I'm working on an example that will use different way to do the same thing so you can compare what it is best for you.
    A quick note: If you want to use mediators and you are comfortable with them you can still use them. You can inject the view in your mediators like this.

    <Injectors target="{MyView}">
        <MethodInvoker generator="{MyMediator}"
            method="setView"
            arguments="{event.injectorTarget}" />
    </Injectors>

    In this way, your mediator will have access to the view. And you can call your mediator from a set of <EventHandlers>. So it works in both ways.

    The down side of use mediators is that the mediator knows too much about the view. If you need to test a mediator you need to create a view.
    You can, of course, use interfaces to decouple them and mock up a view. The idea is that you have multiples ways to do that. Theo (iconara) showed you another way to do it. You don't need to follow that approach, but it would be good for you to take a look (go to the links that Theo posted) and understand the pros and cons of each way.

    Posted 1 year ago #
  5. There is the Dispatcher tag which you can use in your views to listen to events, that's actually a way to (at least implicitly) call a view directly. What you do is that you dispatch an event in the event map using EventAnnouncer and listen for it in the view using a Dispatcher. Didn't think of it before, but I'm not sure I think it's such a good idea. It works, but you get a more coupled architecture.

    Mate is very much more indirect than PureMVC (or Cairngorm), but that isn't something negative. Indirection is usually a sign of a less coupled architecture, which means easier reuse, testing, etc.

    Posted 1 year ago #
  6. A quick note Theo, the tag in the view is the "Listener" not the "Dispatcher".

    Posted 1 year ago #
  7. Ah, sorry, I remembered wrong.

    Posted 1 year ago #

RSS feed for this topic

Reply

You must log in to post.