Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing parameters between MVP views

Status
Not open for further replies.

dafyddg

Programmer
Dec 2, 2003
92
GB
Hi all,

Seemigly simple question but can't satisfy myself with the correct answer.

Two MVP based views. On one view you click on an item in a list. Click a button and then the details of that item are displayed in another view. My question is how does the summary view pass through the state to the detail view. It can't be the views that pass the details through as they shouldn't be aware of the model (i'm intending on passing the model through). Should the presenters be aware of each other somehow, or should the presenters talk to each other through the interfaces of the views?

I hope this makes sense.
 
Did you mean MVC? If so, the view invokes the controller with the parameter, which in turn invokes the other view. The views only know about the model, and how to render it. The controller handles the screen navigation.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
No he meant MVP (Model-view-presenter) which doesn't exist anymore but is now split up in Passive view and supervising controller. Typical for .Net application.

Christiaan Baes
Belgium

My Blog
 
the easiest would be to have a presenter which interacts with both views directly.

a more scalable, but definately more complex would be to register the events with an event aggregator. the aggregator would manage what objects are notified when a event fires. this allows the views/controls to be independent of each other. only the aggrgator has knowledge of both objects.

i haven't spend any time with this since my training, but here it is in C#
Code:
using System;

namespace LabExercises.Events
{
    public interface IEventAggregator
    {
        int NumberOfSubscribers { get; }
        int NumberOfPublishedEvents { get; }
        void SubscribeToEvent(EventKey key, EventHandler handler);
        void PropagateEventToAllSubscribers(EventKey key);
        void RegisterEvent(EventKey key, object subject, string nameOfRealEvent);
    }
}

using System;
using System.ComponentModel;

namespace LabExercises.Events
{
    public class EventAggregator : IEventAggregator
    {
        private EventHandlerList allSubscribers;
        private int numberOfSubscribers;

        public EventAggregator()
        {
            allSubscribers = new EventHandlerList();
        }

        public void SubscribeToEvent(EventKey key, EventHandler handler)
        {
            numberOfSubscribers++;
            allSubscribers.AddHandler(key, handler);
        }

        public int NumberOfPublishedEvents
        {
            get { return 0; }
        }

        public void PropagateEventToAllSubscribers(EventKey key)
        {
            PropagateEventToAllSubscribers(key, this, EventArgs.Empty);
        }

        public void RegisterEvent(EventKey key, object subject, string nameOfRealEvent)
        {
            subject.GetType().GetEvent(nameOfRealEvent).AddEventHandler(subject, CreateHandlerFor(key));
        }

        private EventHandler CreateHandlerFor(EventKey key)
        {
            return delegate(object sender, EventArgs e) { PropagateEventToAllSubscribers(key, sender, e); };
        }

        public void PropagateEventToAllSubscribers(EventKey key, object sender, EventArgs e)
        {
            Delegate rawSubscribers = allSubscribers[key];
            EventHandler subscribers = (EventHandler) rawSubscribers;
            subscribers(sender, e);
        }

        public int NumberOfSubscribers
        {
            get { return numberOfSubscribers; }
        }
    }
}

namespace LabExercises.Events
{
    public class EventKey
    {
        public static readonly EventKey SomethingHappened = new EventKey();

        private EventKey()
        {
        }
    }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
i have no idea

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top