Modify or Override the default t4 templates in MVC

When you create a new View or Controller in a mvc project. It is T4 templates to generate the piece of code for a new view or controller.

image

image

The templates are located in

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3

or

C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3

We can bring these templates to our local project by dragging the “CodeTemplate” folder to Visusal Studio from Windows File Explorer.

image

You will see some compilation errors, these will be fixed after you have clear all the templates’ property “CustomTool”. Just clear the string out.

You can add your own templates as well. Next time when you add a new view, or controller, you will see the project is using your local templates to create them.

This is very helpful and I have Learned this trick from Scott Hanselman.

Here is the blog entry from him, if you want to read more about it.

http://www.hanselman.com/blog/ModifyingTheDefaultCodeGenerationscaffoldingTemplatesInASPNETMVC.aspx

Advertisements

Create a MVC 3 web solution template – Part 1, Create template projects

I am implementing a web application visual studio 2010 template at work. So it saves everyone’s time on a fresh start project. I decide to have 3 layers, presentation layer – web mvc3, business layer, and data access layer.

I have created a MVC3.WebTemplate solution with 3 projects, Business.Template , Data.Template and Web.Template. Let’s see what I have included in the three layers solution.

image

1. Web.Template

image

1.1 CodeTemplates

This folder contains the t4 templates to generate codes for views and controllers. I have included this in the project and it overrides the MVC default one. For more information, please read this blog https://kevww.wordpress.com/?s=t4+template

Have done this provides me ability to create my own t4 templates in the future, eg. in this template, I have modified the default template to make all controllers inherit a BaseController. When you create a new controller using the scaffolding template, it will create YourController : BaseController.

In the future I will create some view templates to make use of Twitter.Bootstrap and save you some time on styling the views.

1.2 Nuget Packages

I have included the latest packages for Jquery, Modernizr, Elmah, Ninject, Twitter.Bootstrap, etc.

<packages>
  <package id="elmah" version="1.2.0.1" />
  <package id="elmah.corelibrary" version="1.2.1" />
  <package id="Elmah.MVC" version="1.3.2" />
  <package id="EntityFramework" version="4.3.0" />
  <package id="jQuery" version="1.7.1" />
  <package id="jQuery.UI.Combined" version="1.8.17" />
  <package id="jQuery.Validation" version="1.9" />
  <package id="jQuery.vsdoc" version="1.6" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" />
  <package id="Modernizr" version="1.7" />
  <package id="Ninject" version="2.2.1.4" />
  <package id="Ninject.MVC3" version="2.2.2.0" />
  <package id="Twitter.Bootstrap" version="2.0.0" />
  <package id="WebActivator" version="1.4.4" />
</packages>

Of course, you know Nuget will download all the packages in a Package folder in your solution folder. You don’t need to include those files into your template. You can easily restore the Nuget packages to a new solution thsi is created from the template.

image

2. Business.Template

image

Not much to talk about for this project, really it should contain all your business logics.

3. Business.Template

image

3.1 DataContext

For data access, I am using EntityFramework 4.3. I have included a empty context edmx file with t4 templates to generate DbContext & POCOs.

If you want do CodeFirst, you can remove my stuff under Data folder and put your POCOs there.

3.2 Repositories

I have included a generic base repository. To help you start building your repositories. It has the default CRUD operation methods which you can inherit from.

————————————————————————————-

This is all about my project template, please refer to blog part 2 for the VS Template Creation.

Download the sample vstemplate from http://mvc3projecttemplate.codeplex.com/

Life Saving Links

IDE tools

Productivity Power Tools

http://visualstudiogallery.msdn.microsoft.com/d0d33361-18e2-46c0-8ff2-4adea1e34fef

Codesion (Secure project hosting website, free for 1 developer @ 200 MBs )

http://codesion.com/

Visual Studio (dark color themes, easy on eyes!)

http://weblogs.asp.net/scottgu/archive/2010/04/29/download-and-share-visual-studio-color-schemes.aspx

Fiddler (Http tool)

http://fiddler2.com/fiddler2/

FireBug (javascript and html tool!)

http://getfirebug.com/

Web Developer plugin

(Firefox) https://addons.mozilla.org/en-US/firefox/addon/web-developer/

(Chrome) https://chrome.google.com/webstore/detail/bfbameneiokkgbdmiekhjnmfkcnldhhm

Dependency Injections:

Unity ( IoC ) http://unity.codeplex.com/

Structure Map http://structuremap.net/structuremap/

Ninject http://ninject.org/

AutoFac http://code.google.com/p/autofac/

Unit Testing, Test Driven Development (TDD), Mock Frameworks

Moq http://www.asp.net/mvc/mvc4

Rhino Mocks http://ayende.com/blog/tags/rhino-mocks

Type Mock http://www.typemock.com/

Logging Tools

NLog http://nlog-project.org/

Log4Net http://logging.apache.org/log4net/

Elmah http://code.google.com/p/elmah/

MVC Tools

MVC 4 preview http://www.asp.net/mvc/mvc4

MVC mini profiler http://code.google.com/p/mvc-mini-profiler/

Glimpse http://getglimpse.com/

MvcContrib http://mvccontrib.codeplex.com/

Chirpy Zippy http://www.weirdlover.com/2010/05/22/download-chirpy-zippy-visual-studio-addin-for-js-css-and-less-files/

T4 Template Mvc http://mvccontrib.codeplex.com/wikipage?title=T4MVC

Asp.net Sprite and Image Optimization Framework http://aspnet.codeplex.com/releases/view/65787