Solved

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

Posted on 2009-04-02
27
960 Views
Last Modified: 2013-12-14
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?
0
Comment
Question by:MSFanboy
  • 13
  • 6
  • 5
27 Comments
 

Author Comment

by:MSFanboy
Comment Utility
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.
0
 
LVL 21

Expert Comment

by:MogalManic
Comment Utility
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).
0
 

Author Comment

by:MSFanboy
Comment Utility
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?


0
 

Author Comment

by:MSFanboy
Comment Utility
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

0
 
LVL 21

Expert Comment

by:MogalManic
Comment Utility
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.
0
 

Author Comment

by:MSFanboy
Comment Utility
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?

0
 

Author Comment

by:MSFanboy
Comment Utility
kind = find

see above...
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
How may I help?
0
 

Author Comment

by:MSFanboy
Comment Utility
if this question was seriously meant, why do you ask it at all? Havent you read my first post?
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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?
0
 

Author Comment

by:MSFanboy
Comment Utility
@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();

0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:MSFanboy
Comment Utility
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.
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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.
0
 
LVL 21

Expert Comment

by:MogalManic
Comment Utility
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).
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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.
0
 

Author Comment

by:MSFanboy
Comment Utility
@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?

0
 

Author Comment

by:MSFanboy
Comment Utility
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...
0
 
LVL 21

Expert Comment

by:MogalManic
Comment Utility
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


0
 

Author Comment

by:MSFanboy
Comment Utility

@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 :)
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
Comment Utility
It looks to me like you have just changed the responsibility from the presenter to the view, which in my opinion, goes against the grain of the MVP pattern.  The view and model should be able to stand on its own.  The view should have the UI elements, the model should have the data elements, and the presenter should have the "brains" of the operation.  It should be up to the presenter to do all the work.  I fail to see any advantage, because you still have coupling if the view calls anything on the presenter.
0
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 250 total points
Comment Utility
I agree with TheLearnedOne.  
The advantages of the MVP pattern is as follows:
 - Testability:  You can test the Model Object and the Presenter Object in isolation (generally you cannot test the view because most of its functionality is visual)
 - encapsulation :You are separating what changes into other classes.  If the business rules change all that you need to change is the presenter.
 - Swapability:  The same model/Presenter can be used for multiple Views.  
    For example, you could have multiple Views of a contact list:Phone List; Address List;One contact at a time;...
0
 

Author Comment

by:MSFanboy
Comment Utility
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??
0
 

Author Closing Comment

by:MSFanboy
Comment Utility
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.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now