Visual Studio project templates: a (possible) boon for micro services

During my talk on micro services at Leeds Sharp recently, it dawned on me that Visual Studio project templates are not something very well known. So here’s a quick primer on them, which can be useful for a large number of similar projects – a usual requirement for micro services. For our implementation of micro service we went down the route of using shared libraries to control their structure and make them easier to setup. This needed some custom code e.g. base repository for Entity Framework classes, wrapper for API calls etc; which was repeated for every micro service – hence a need for project templates.

Any Visual Studio project can be converted to a template by clicking File –> Export Template… This will get a pop up like the one shown below:

Project Template Wizard Start

Micro Services Project

The areas outlined in red are customisations in the above sample micro service project that are likely to be repeated in any new micro service. This includes a line that needs to be added to the NinjectWebCommon.cs file in Ninject MVC to make it work with a Web API project, the kind of project we used for micro services. These and any other customisations, including any Nuget packages etc will be included in the project template created. On clicking next we get to the final screen:

Project template wizard

This gives us the option of adding icon & preview image and some other details. The output location, outlined in red, is where Visual Studio looks for any such templates to show when getting us the new projects wizard. Click Finish and we are done. So the next time a new project is required, the new template (with the chosen icon) is visible:

Project template in action

Now we click OK after putting the name and location of our new micro service and we get the new project with the required custom items:

Micro Service Project template implemented

That’s about it, there’s the option to have multiple projects in a template – more like a solution template, but it’s not possible via a wizard and was too much effort for what we needed. A bit of googling will set you on the road if you need something along those line e.g. multiple custom projects per solution.

So there you have it, a way to ease initiation/propagation of something like micro services. There is the obvious pain of having to update all the previous projects for things missed out or implemented incorrectly, but that’s just something waiting to be learnt with or without templates.