Geeks With Blogs
Lee Brandt's Blog You're only as smart as your last line of code

Originally, I had used JP's method from this MSDN article for connecting the Views and Presenters. It works very well except it leads to a lot of very similar code.

   1:  public partial class _default : Page, IDefaultView
   2:  {
   3:    private DefaultPresenter _presenter;
   4:    protected override void OnInit(EventArgs e)
   5:    {
   6:        base.OnInit(e);
   7:        _presenter = new DefaultPresenter(this);            
   8:    }
   9:    // other properties and methods for the IDefaultView interface
  10:  }
Then I came across David Hayden's article on the patterns & Practices web site and found the connective tissue I needed so that I didn't have 
to manually tell my presenter every time who the actual view was. It consists of a View Base class:
   1:  public abstract class View<ViewType, PresenterType> 
   2:      : Page where PresenterType 
   3:      : Presenter<ViewType> where ViewType : class
   4:  {
   5:      public PresenterType Presenter { get; set; }
   6:   
   7:      protected virtual void Page_Init(object sender, EventArgs e)
   8:      {
   9:          // could also use IOC container to handle this
  10:          Presenter = Activator.CreateInstance(typeof(PresenterType)) as PresenterType;
  11:          Presenter.View = this as ViewType;
  12:          Presenter.InitializeView();
  13:      }
  14:  }

and a Presenter base class

   1:  public abstract class Presenter<ViewType>
   2:  {
   3:      public ViewType View { get; set; }
   4:      public virtual void InitializeView(){}
   5:  }

then initializing my views like so:

   1:  public partial class _default : View<IDefaultView, DefaultPresenter>, IDefaultView

Seems to work very well. It may be old news to some developers, but it was new to me. Check out David and JPs articles.

~Lee

Posted on Saturday, November 8, 2008 11:12 AM | Back to top


Comments on this post: ASP.NET MVP (Model-View-Presenter)

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Lee Brandt | Powered by: GeeksWithBlogs.net