Thomas
Thomas

Reputation: 34218

windows form & design pattern

i am familier with mvc,mvp or mvvm patter. so i was searching google for implementing good design patter for win form apps. i found lots of article.few guys said mvc is good and few guys said mvp is perfect for win apps. i found a very small code which implement mvp in win apps. i go through the code and found that developer has to write lot of extra code to bind treeview or any control.

the code as follows

public interface IYourView
{
   void BindTree(Model model);
}

public class YourView : System.Windows.Forms, IYourView
{
   private Presenter presenter;

   public YourView()
   {
      presenter = new YourPresenter(this);
   }

   public override OnLoad()
   {
         presenter.OnLoad();
   }

   public void BindTree(Model model)
   {
       // Binding logic goes here....
   }
}

public class YourPresenter
{
   private IYourView view;

   public YourPresenter(IYourView view)
   { 
       this.view = view;
   }

   public void OnLoad()
   {
       // Get data from service.... or whatever soruce
       Model model = service.GetData(...);
       view.BindTree(model);
   }
}

please someone go through the code and help me to understand the flow because how code should be written in mvp patter that i dont know. thanks.

Upvotes: 0

Views: 2239

Answers (1)

squillman
squillman

Reputation: 13641

This code is already using the MVP pattern.

It declares an interface IYourView and a concrete class YourView that implements System.Windows.Form and this new interface. Essentially what that is doing is creating a new form with the added requirement that it also implements the BindTree() method defined in IYourView.

The YourView class (form) then has a dependency of YourPresenter in order to hook up the OnLoad event with the presenter, although I'd do it where the presenter subscribes to the form's OnLoad event instead.

The presenter YourPresenter takes as a dependency an instance of YourView and it can then use that instance in the rest of its logic.

Now, to use that you would follow a process similar to this:

  • Create a new instance of YourView (which then in turn creates the presenter)
  • Implement logic in the presenter (ie- create GetModel()) to create the Model that you want to bind to the tree
  • Call view.BindTree(model) in the presenter where model is what you just created in the previous step

So, create an instance of your view:

IYourView newView = new YourView();

Then in your presenter class:

Model model = GetModel();
newView.BindTree(model);

Upvotes: 2

Related Questions