Fredrik Normén's Blog - NSQUARED²
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Microsoft Most Valuable Professional
     .Net Framework - ASP.Net - Architecture - Development
NOTE: This list of posts will only list the 15 latest posts, to see the rest, select from the Archives located in the menu to the left. The RSS will only list the 10 lastest posts.
Model View Presenter - Update and problem.

Category:  Design and Architecture

Based on some issues while I tried to update a post in my blog, I had to remove the whole post, which also includes the comments :(

 

I got one very good comment from Tomas (Don’t know who he is ;)). In my first post about the MVP pattern I mentioned that I have the Save and Delete methods specified in the View interface:

 

public interface INewsView : IView
{
     int NewsID { get; }

     string NewsTitle { get; set; }

     string Body { get; set; }

     bool Display { get; set; }

     void SaveNews();

     void DeleteNews();
}

 

The bad thing with that solution is that the methods will be public, so the controllers can also make a call to them:

 

 public class NewsController : ViewController, INewsController
{
        private const int NO_ID = -1;
       
        private INewsRepository _newsRepository;


        public NewsController(IView view) : base(view)
        {
            this._newsRepository = new NewsRepository();
        }

        private void Save()

        {

               this.NewsView.SaveNews();  //Stack Overflow Exception

        }

 

        ...

}

 

In that case it will create a circular call that will later throw a Stack overflow exception. The idea with the implementation was to make sure the Mock View’s can sort of “simulate” actions. If the “Action” method is added to the View interface, even the Mock View need to implement them and that make it easy to write tests like:

 

myMockView.SaveView();

 

Which will make a call to the Controller’s SaveNews method. The SaveView method, in my first example will be called by the OnClick event of the Save button. Now after the good notice from Tomas, they are removed. So to “simulate” the Click of a button, we need to add the Action’s to the Mock View instead. I would like to thank Tomas for his comments.

Posted: Tuesday, April 24, 2007 - 14:43 GMT+1    Print     E-mail    Comments (5)
   fredrik.nsquared2.com - 2007