Link to home
Start Free TrialLog in
Avatar of MSFanboy
MSFanboyFlag for Germany

asked on

Best Practice in MVP Pattern - how would you pass userdata from VIEW to the Model class?

Hello,

my user enters data. Depending on this value I have to filter my Table data in the Database.

Would you get the userdata from the VIEW and pass to the MODEL?
Avatar of MSFanboy
MSFanboy
Flag of Germany image

ASKER

would you get the user data from the presenter class and forward it to the model where the entered values are used as filter parameter for the database tables for some old dbase tables... hope its more clear now.
Here is an example of a MVP pattern:
  http://blog.vuscode.com/malovicn/archive/2006/10/10/Model-View-Presenter-_2800_MVP_2900_-pattern.aspx

The view only talks to the presenter.  The Presenter forwards the requests to the model.

The reason for this is because the view should not know the details of how the model is implemented.  That is the presenter's job.  If the view has a field call txtFirstName, it passes the value to the (and other values) to the SetCustomerData() method.  It is the presenter's role to translate the txtFirstName to the [First Name] column in the model.  It is the Model's job to transfer the [First Name] to whatever is the persistance (the DB).
ok right what u said I have done called the below method in the presenter to get the filtered table passing the userYear to filter table.

private DatabaseConnection con;
ADSDO o = con.GetConnection(userYear);

another question also concerning best practice and i havent found a answer yet...

Do I pass the user data from the View to the Presenter via events OR

Do I fetch the user data from the ober the view interface methods out of the Presenter?


I have just browsed your link u gave me MogalManic:

I had a good laugh when I read this:

        private void searchButton_Click(object sender, System.EventArgs e)
        {
            _presenter.SetCustomerData();
        }

The view is getting data from the presenter ??? NEVER!!! The Presenter is passing data to the view  like IView.BindCustomer(customer);





public partial class UserViewForm : Form, IUserView
 
    {
 
        private UserViewPresenter _presenter;
 
        public UserViewForm()
 
        {
 
            InitializeComponent();
 
            _presenter = new UserViewPresenter(this);
 
            _presenter.SetCustomerData();
 
        }
 
       
 
        private void searchButton_Click(object sender, System.EventArgs e)
 
        {
 
            _presenter.SetCustomerData();
 
        }
 
        #region IUserView Implementation
 
                  // The same implementation like in WebForm case
 
                  // Wire up the control text properties to interface members
 
        #endregion
 
    }

Open in new window

It should be BOTH ways.  The view is sending the Search Criteria to the Presenter.  The Presenter uses the SearchCriteria to load the requested Customer.  The Presenter sends to the view the loaded customer.

As for the method/event question.  I guess it would depend on the implementation of the view.  You could use events if you wanted the view to be updated asyncronously (I.E. you did not want to wait for the DB query to finish).  The most straightforward way would be through method calls.
quote:"It should be BOTH ways.  The view is sending the Search Criteria to the Presenter.  "

YES, BUT... the view should NOT send the search criteria to the presenter with that syntax:

presenterObject.presenterMethod(string searchcriteria);

it should happen via EVENTS considering the official MVP pattern I kind also browse on MS msdn sites.

DO YOU agree?

kind = find

see above...
Avatar of Bob Learned
How may I help?
if this question was seriously meant, why do you ask it at all? Havent you read my first post?
1) I answer a lot of questions, and there are a lot of questions that people abandon, so I try to PING a question, to see if the asker is still involved.

2) You aren't getting answers, but I don't know what you expect.

3) Is there any value in any of the previous comments?

4) What kind of solutions/suggestions would be help you the most?
@TheLearnedOne:

quote:"It should be BOTH ways.  The view is sending the Search Criteria to the Presenter.  "

YES, BUT... the view should NOT send the search criteria to the presenter with that syntax:

presenterObject.presenterMethod(string searchcriteria);

it should happen via EVENTS considering the official MVP pattern I kind also browse on MS msdn sites.

DO YOU agree?

concerning MVP pattern someone shouldnt access the Presenster via presenter.Method();
but WHY NOT if there is already an object in the View class? It doesnt make sense to instantiate a presenter object just to register the events in its constructor instead of directly accessing the presenter methods like presenter.method();

In the MVP pattern, the presenter should do all the work--it should know about both the model and the view.  If you use MVP with the Observer pattern, then you define events in the model, and the view.  When the presenter needs to be notified of something that it needs to take care of, the event would be raised.  For the model, that would be something setting a property value, and for the view, it would be something changing state on a UI element.

What you get with this model, is a separation between the model and the view, bridged by the presenter.  In that way, you could define multiple views to display the same model.
that did not answer my question.
I do NOT use observer pattern as I do c# not java. In Windows Forms you do MVP and one of its kind like passiv view or supervising controller.
The controller (presenter) should still be the responsible agent that would get the information from the model, and pass to the view.  There should be separation between model and view.
If you call the "Fowler Passive View" the "official MVP" (http://www.martinfowler.com/eaaDev/PassiveScreen.html), then:
  - Yes interaction from the view to the Presenter is handled through events.
  - The event is the implementation of the "Observer Pattern".  The observer pattern is NOT java specific.

In Fowler's example (http://www.martinfowler.com/eaaDev/uiArchs/mvp-seq.gif) the text field has a TextChanged event which triggers the presenter to perform some action.

If you model after Microsoft's MVP pattern http://msdn.microsoft.com/en-us/library/cc304760.aspx, as this article does:
 http://msdn.microsoft.com/en-us/magazine/cc188690.aspx

The above article uses an event that is triggered on the presenter (also the Observer pattern).
It is difficult to really know what is the "Best Practice" here, but I prefer the Observer Pattern (Publish/Subscribe), since there is loose coupling between the class instances defining the MVP pattern.
@MogalManic

you seem to have knowledge :)

if you speak of the microsoft mvp pattern I guess you spoke about that:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    presenter = new ViewCustomerPresenter(this);
    this.customerDropDownList.SelectedIndexChanged += delegate
    {
        presenter.DisplayCustomerDetails();
    };
}


right?  what I like about the MS MVP that I call the presenter in the view what makes sense because else the presenter object is nearly unemployeed except the recieving of "this"...
I also asked this far above of this thread now I have the answer: Microsoft MVP Pattern

Do you know which MVP Pattern (Fowler/MS) is more used or prefered in companies?

and why needs the view to know the presenter? if I instantiate a presenter object in the view`s constructor my view knows the presenter. Actually this is not wanted? Am a bit unsure...
The view needs to know the presenter in order to call its methods or register for its events(I still think methods are OK, one reason is that you cannot register for events on the presenter from Visual Studio's Form Designer).  The view has to somehow tell the presenter that the user requests a different sort of data or the view has changed the data.

As for which pattern is better.   They are actually the same.  I quoted the Microsoft pattern because it had examples in c#.  Martin Fowler is one of the early evangelists for Object Design patterns and Extreme Programming.  I have his book "Patterns of Enterprise Architecture" (http://martinfowler.com/books.html#eaa) which I recommend.

Microsoft's MVP pattern is used in its ASP.NET MVC 1.0 which was released recently:http://www.microsoft.com/downloads/details.aspx?FamilyID=53289097-73ce-43bf-b6a6-35e00103cb4b&displaylang=en



@TheLearnedOne
quote:"... I prefer the Observer Pattern (Publish/Subscribe), since there is loose coupling between the class instances defining the MVP pattern."
I do not agree with that. I am arrived at the moment at a point where I question everything concerning software architecture. I like patterns, layers etc... 
But where do you see the advantage in the Publish/Subscribe Pattern compared to this method:

I describe now my WAY and will tell what advantage it has to your observer Pattern :

View:
MainPresenter presenter = new MainPresenter();

AdsExtendedReader reader  = presenter.View_GetAllArticles();
DataGridView.DataSource = reader;


Presenter:
View_GetAllArticles()
{
   AdsExtendedReader reader = DoQuery...
    return reader;

 // with MVP I have to do this
_view.BindReader(reader);
}

Not that there is a misunderstanding. I am also using events with publish/subscribe, but for some cases I just do not want them or see any advantage in using it.
With the above WAY I see those advantages:
1.) NO interface needed
2.) my presenter does not know the View or IView (interface of View)- this I call loose coupling or actually NO coupling)
3.) NO public BindReader(AdsExtendedReader reader) method in the View is needed to declare which I have to call in the presenter - see above -
4.) No events + delegates used
Observer Pattern (after Fowler)
With MVP Fowler you have NONE of the above advantages instead of you have to write many code bloating your application. 

What do you think TheLearnedOne about my thoughts? Please share your wisdom with me, I want to learn :)
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
when the view has to say to the presenter :" give me this data now at this point because I have a long running task in a thread" then I use a Delegate with for example a return value AdsExtendedReader
IView:
public delegate AdsExtendedReader ArticlesListEventHandlert(int numberOfList);
event void ArticlesListEventHandler ArticlesListEvent;
View:
public event void ArticlesListEventHandler ArticlesListEvent;
if(ArticlesListEvent != null) var reader =  ArticlesListEvent(5);

Would you do it different?
How can I communicate between TWO Form Window according to MVP, I find no information about that??
I have understood all what you said but still have open questions to MVP so I make for every question a new Thread not to split the information around...
Thank you above all MOgalManic and TheLeanrnedOne.