Code Generation of ASP.NET Web Applications - Model-View-Presenter Visual Studio Item Templates
by David Hayden ( Florida ASP.NET C# SQL Server Developer ), Filed: Code Generation
Code Generation and code generation tools are a necessary evil these days to create custom web applications for clients. I take a lot of pride in my application as all good developers do, so you have to find those forms of code generation that you can use and yet still maintain quality, extensibility, and easy-to-maintain applications.
One of my favorite ways of using code generation is creating project and item templates for visual studio. I mentioned creating item templates in a few ways in the past:
The Guidance Automation Extensions and Guidance Automation Toolkit created by Patterns & Practices is a richer way to add code generation into Visual Studio, but it isn't as easy and there is not much guidance on how to do it.
Anyway, I use Model-View-Presenter in all my web applications but can't stand to create the initial structure over and over. To avoid this, you can just create item templates for you typical views, presenters, controllers, etc. so you don't have to constantly add the basic plumbing / format from scratch.
They key is creating your visual studio item and project templates to your development style and coding standards, of course. If you were to think about your typical view class, at the very least they may have to get access to the presenter, pass itself as the view, and call initialize when appropriate. Your bare-bones web page view template for model-view-presenter may look like:
namespace $rootnamespace$
{
public partial class $safeitemrootname$
: BasePage, I$safeitemrootname$View
{
$safeitemrootname$Presenter _presenter;
#region I$safeitemrootname$View
#endregion
protected void Page_Init(object sender, EventArgs e)
{
_presenter =
new $safeitemrootname$Presenter(this);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
this._presenter.Initialize();
}
}
}
Obviously you can do much more than this and change the style to suit your development, but the key is that it is no fun typing this basic plumbing over and over again. Give your hands, eyes, wrists, etc. a break and think about that code you write over and over and toss in some visual studio item templates for that extra code generation. They key is not only the productivity, but also the consistency of all your views, presenters, controllers, etc.
News Feed: David Hayden ( Florida ASP.NET C# SQL Server Developer )
Filed: Code Generation