Monday, December 29, 2008

My Add MvpView VS Template

So, I did a presentation at work on the MyDumbView (MVP Passive View / Observer pattern) blog post I made a few weeks ago. After the presentation, I heard a complaint about the amount of work required to wire up a view. While I don't think it's a very valid point (you'd write just about the same amount of code in a code-behind, it would just be untestable). It did get me to thinking... Why not make a VS template with a wizard that implements the view for you?

So I did.


  • Allows you to browse an assembly, and chose view to implement
  • Supports multiple "templates" for aspx markup
  • "Wires up" all views properties and events according to template definition
  • "News up" a presenter, so views with value types will "just work" without any hand coding!
  • Generates a populated resx file for each view created


To install it, just download the zip file, unzip and run the Install.bat file.  What does it do?

Creates C:\AddView and copies AddView.dll
Creates C:\AddView\Templates and copies Templates.xml
Copies MVPView.Zip to your visual studio ItemTemplates folder (it'll do both vs 2005 and 2008).
Adds AddView.dll to your GAC (this is required by VS)

Of course you can uninstall it by undoing all the steps above.

Lets Use it

If you haven't read the MyDumbView post yet, now would be a good time to, because that's the project I'm adding a new file to. Basically, this template will write all the UI code needed for an MVP view to work. You can then spend the time all the time you want prettying it up, or let a UI guy or gal do that for ya.

We're going to add a new View to the Web Project. To do so we simple right click on the web project and chose "Add New Item".

Down at the bottom under "My Templates" there will be a new item titled "MVP View". Select it. Go ahead and get rid of that Space in the file name, it's annoying. I just named my MVPView.aspx


A form will pop up that allows you to select the assembly that has the View you wish to implement. Select that assembly ( <SolutionDir>\References\MyDumbView.dll) and in the right hand tree browse to the View you wish to implement (MyDumbView.Presentation.Views). If the presenter is a different namespace, you can specify the presenters namespace as well (MyDumbView.Presentation.Presenters). You can also select different templates to use to generate the HTML. The Templates are located in C:\Templates\Templates.xml, and can be modified as much as you wish, or you can add templates to the file easily. Just take a look at it.

Once we've filled in everything, Select OK.

Vola! An aspx file, the codebehind and App_LocalResource\FileName.aspx.cs are generated. Yep, they "just work"!

The aspx is resource file driven, so you can change the text by just editing it, and of course you can hand edit the aspx to your hearts content.

The idea here was NOT to let a tool generate all the UI, it was simply make it easier to practice a pattern that I think is very effective. Of course this won't work for everything, but it sure saves a lotta hand coding.

As it's currently written, it will throw not implemented exceptions for Array properties (very similar to how Resharper implements interfaces), since those will typically control drop downs or repeaters and require some hand coding on your part.


No comments:

Post a Comment