Creating a Visual Studio template
Even by using Visual Studio, in all projects comes a time when you do repetitive things. This is especially true when you are having a microservice architecture. Depending on the project’s size you’ll need to create multiple small and independent projects that will play a microservice role for you.
At it’s core, a microservice architecture is more code-oriented than SOA services. Breaking your code into separate services and libraries, doesn’t automatically make your app as being a part of a microservice architecture, but for sure it’s the first step to get there.
Some might say the it brings you overhead, or that the gains vs drawbacks aren’t so many, but I know for sure you need to change your mindset a little bit and step out of your monolithic comfort.
When you want to step into the microservices world and you have a big team, you’ll often encounter different types of approaches when writing code, or naming or even in a new project infrastructure.
This is not a bad thing at all, but I think having your team brainstorm a uniform approach in the project creation phase, to create a common look and feel for all future microservices will save you a few time ticks at a certain point.
For every project you’ll make in think kind of infrastructure, you’ll want pretty much the same thing, and you’ll want the same packages and maybe a certain configuration and folder structure.
To ensure a certain degree of consistency across the team/teams creating a Visual Studio project template will come to your aid.
First, you’ll need to install Extensibility Tool extension that will allow you to create VSIX projects, Item templates and Project templates
Then, create new project that will be your template base, and install the NuGet packages you need and the folders and files that represent your infrastructure.
Let’s say you’ll use Dapper as a MicroOrm, you want to log in a file and ElasticSearch with Serilog and also you’ll want to configure that, and also because you’re developing an API you’ll want to document it as nicely and easy as possible.
Maybe you’ll need a hosting.json file to be able to map your API to a specific port to have more control in a hosted environment. Let’s say your template base will be something similar to this:
Then, you decide that this infrastructure suits your needs, and you’re ready to export this as a template.
Go to Project menu in Visual Studio and – > Export Template
Because you want to be able to use your project as a future template for Visual Studio select “Project template”
When you click finish, this export will be saved in C:\Users\username\Documents\Visual Studio 2017\My Eported Templates as a zip file.
Then the hard work begins because right now Visual Studio contains a bug that you’ll need to overpass.
Close Visual studio, extract the archive you just exported and edit the MyTemplate.vstemplate manifest and add <CreateInPlace>true</CreateInPlace> node.
Zip the whole project and move to the next step(let’s hope this hack won’t be necessary soon).
Creating the VSIX extension
Under Extensibility node you’ll find the VSIX project, select it, and create a new project.
Import the zip file you just ‘fixed’, and include it in this solution.
Right click the zip file, go to Properties and under Build Action , Copy to Output Directory -> Copy always.
If you think you’re done, you’re not, because you need to create the general properties of the final VSIX file.
For this, double click the source.extension.vsixmanifest file in solution to edit the metadata and assets.
Editing the Assets
The assets section is the main part of this step because in it you’ll basically indicate the resources used for your installable extension.
Add new Asset:
Source: File on filesystem
Path: your zip file (that will be prompted to you by the IDE)
Click OK, build the project and you’re done.
As a build artifact, under bin/debug folder, you’ll see the final, installable .vsix file that can be distributed inside your organisation or team.
Install it as you would install any extension from the visual studio marketplace and you’ll be able to create new projects based on your template.
This extension will install a new type of project in your Visual Studio, containing all the bits and pieces you added in your template: NuGet packages, folder structure
Use it to create a new project and you’ll see that everything you’ve set up is there: the folder structure, the controllers you added, the files, and the most important part the NuGet packages you need.
We often forget that Visual Studio can be extended and that those extension can prove to be more than useful for many of us. We all have the tendency to use only the extensions that are already there, created by others. How about creating cool extensions that others can use from us, even if we are talking about our own team or colleagues or organization?