Injecting Mediator into Command - is this possible?

seansutton's Avatar

seansutton

11 Jun, 2013 07:38 PM

Hi,

In my application I inject mediators into commands. I do this fairly often to get information on the state of the mediators' views (i.e. I do not set anything on / via mediators) and perform other actions based on the results. This is all pretty simple and synchronous.

Unfortunately, whenever I then call commands with one or more mediators injected into them I get an error:

"Error: Injector is missing a rule to handle injection into property..."

Why does this error only appear when mediators are injected into commands? It does not happen when ONLY events and/or models are injected, so I am confused.

Many thanks in advance!

  1. 1 Posted by Stray on 11 Jun, 2013 08:04 PM

    Stray's Avatar

    Because views and mediators can be many (you might have 3 instances of SomeView, each with a SomeViewMediator) it doesn't make sense to inject mediators into other objects - after all, which of the many mediators that meet that injection rule would you want to inject?

    If you really need to pass a reference to the mediator and you know that it's safe to do so, you can map it in the injector yourself (in the mediator). Don't forget to unmap it when the mediator is destroyed.

    hth

    Stray

    On 11 Jun 2013, at 20:38, seansutton <[email blocked]> wrote:

  2. 2 Posted by seansutton on 12 Jun, 2013 11:18 AM

    seansutton's Avatar

    Hi Stray. Thanks for your reply :)

    Hmmm... so the error I am getting happens EXACTLY because mediators shouldn't be directly injected into commands? i.e. this is by design?

    I wonder how clear this is for RL users. Some sources I've read seem to make this a little unclear (though it may be my slow brain!!):

    The RL best practices says : 'commands may ... Be injected with Models, Services, and Mediators to perform work on directly'. Should this be revised / is it inaccurate?

    Also - unless I missed it - I didn't see this issue discussed in your RL book. It seemed logical to me to assume that mediators (if used as singletons) should be retrievable via DI just like models and events. In my experience, 90% of mediator and view combinations exist only once and last the duration of the app's existence. Are my use cases unusual?

    ... Maybe I'm getting a little distracted by how easy it is to access Mediators from commands with Pure MVC...

    Anyway - back to my original question: Instead of finding and interrogating mediators in commands, should I instead have the mediator update a model earlier in the process, and have the command query the model instead?

    This seems a little awkward as there is no real state change to the app, and it seems fair for the mediator to asked directly.

    Thanks again for any further enlightenment!!

  3. 3 Posted by Stray on 12 Jun, 2013 11:30 AM

    Stray's Avatar

    Yes, the instructions must be out of date (sorry, not my department, not that any of it is my department any more - I just picked up your query in my email). Mediator injection was removed a couple of years ago when it became apparent that the many-to-many relationship rendered injection-by-default unstable.

    As I said, one single line of code in your mediator makes it available for injection. Just do:

    injector.mapValue(MediatorType, this);

    and that fixes it, as well as signifying clearly to other developers (or yourself in future) that this mediator is being accessed elsewhere.

    Personally I don't think I've ever injected a mediator anywhere - unlike PMVC mediators, RL Mediators are meant to be disposable, exist solely for the purpose of wiring views in to the app events system and shouldn't have an API that other objects could use anyway. So, yes, usually I would dispatch data events from the mediator that update a model somewhere, and have commands query that model.

    But if you choose to use mediators as ViewControllers that's your choice - it's just that the MediatorMap was never intended to produce ViewControllers. The various view-related maps in RL2 provide much better support for the ViewController way of working (either along side mediators or instead of them).

    We possibly didn't mention it directly in the book - we were restricted in terms of page count - but the fact that we did talk about how your Mediator is just a delivery man and should never have an API does lean towards there being no point in injecting it. :)

    Anyway, one line of code doesn't seem like much of a hardship - if your app truly is disposable then the shortcut won't cost you much, and it's easy enough to refactor later.

    hth,

    Stray

    On 12 Jun 2013, at 12:18, seansutton <[email blocked]> wrote:

  4. 4 Posted by seansutton on 12 Jun, 2013 01:22 PM

    seansutton's Avatar

    Hi Stray,

    Thank you once again for your detailed reply. I have a much better understanding now :)

    Also - if you state in the book that mediators 'should never have an API' then this is a strong hint, yes.

    Thanks again!!

  5. Ondina D.F. closed this discussion on 13 Jun, 2013 09:38 AM.

  6. seansutton re-opened this discussion on 13 Jun, 2013 05:28 PM

  7. 5 Posted by seansutton on 13 Jun, 2013 05:28 PM

    seansutton's Avatar

    Ondina I'm sorry to re-open this discussion

    I'm using Robotlegs 1.5.2 and there does not seem to be a straightforward way of getting to the injector from a mediator.

    Am I being really stupid? Am I missing something?

  8. Support Staff 6 Posted by Ondina D.F. on 13 Jun, 2013 05:54 PM

    Ondina D.F.'s Avatar

    Ondina I'm sorry to re-open this discussion

    Hehe, no problem.

    Am I being really stupid? Am I missing something?

    You’re not stupid!!

    You have to inject the injector into the mediator

  9. Support Staff 7 Posted by Ondina D.F. on 13 Jun, 2013 05:56 PM

    Ondina D.F.'s Avatar

    like this:

    [Inject]
    public var injector:IInjector;
    
  10. 8 Posted by seansutton on 13 Jun, 2013 06:17 PM

    seansutton's Avatar

    Doh. You are a super star :) Thanks so much to yourself and Stray for the quick replies :)

  11. Support Staff 9 Posted by Ondina D.F. on 13 Jun, 2013 06:28 PM

    Ondina D.F.'s Avatar

    Haha.. Stray is THE super star! Not me.

    You're welcome :)

    You can close the discussion yourself.

  12. seansutton closed this discussion on 14 Jun, 2013 08:34 AM.

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