When it comes to building a microservice architecture for .NET projects, it may initially seem complex. However, there are tools available, including Microsoft's Visual Studio, that can greatly simplify our work. Visual Studio provides the option to create custom templates, which ensures consistency within our team and projects.
Importance of Visual Studio Template?
Visual Studio templates play a crucial role in a microservices architecture. As projects progress, repetitive tasks become inevitable. This is especially true in a microservice architecture, where multiple small and independent projects need to be created to serve as microservices.
Microservice architecture is centered around code organization, unlike traditional SOA services. Breaking down code into separate services and libraries is the first step toward adopting a microservice architecture. Some may argue that this approach brings overhead or has limited benefits compared to drawbacks. However, it requires a slight shift in mindset to embrace the benefits of microservices and move away from the comfort of monolithic applications.
When transitioning to a microservices architecture with a large team, different coding approaches, naming conventions, and project infrastructure setups may arise. This diversity is not necessarily negative, but it can be beneficial to establish a uniform approach during the project creation phase. This ensures a consistent look and feel across future microservices and saves time on certain aspects.
For each project built within this infrastructure, there are often common requirements such as specific packages, configurations, and folder structures. This is where Visual Studio templates come into play. By creating a custom template that aligns with your project needs, you and your team can maintain the same structure across all projects.
Steps to Create a Visual Studio Template for Your Microservices Architecture
To create a Visual Studio template, follow these steps:
STEP 1: Install the Extensibility Tool extension:
Begin by installing the Extensibility Tool extension in Visual Studio. This extension enables you to create VSIX projects, Item templates, and Project templates.
STEP 2: Create a new project for your template base:
Start a new project in Visual Studio that will serve as the foundation for your template. Add the necessary NuGet packages, folders, and files that represent the desired infrastructure for your microservices.
STEP 3: Set up the required components:
As an example, if you're using Dapper as a Micro ORM, you might want to include logging capabilities for File and ElasticSearch. Additionally, you may need to configure the infrastructure and include documentation for your API.
STEP 4: Configure additional settings:
Depending on your requirements, you might need to include a hosting.json file to map your API to a specific port, granting you more control in a hosted environment. Ensure that your template base reflects the desired settings and configurations.
STEP 5: Evaluate the template infrastructure:
Once you have set up the template base, review it to determine if it meets your specific needs and requirements. Ensure that all necessary components and configurations are in place.
Once you’re done, decide if this infrastructure suits your needs. If it does, you’re ready to export this as a template.
Steps to Export Your Template Base
To export your template base as a Visual Studio template, follow these steps:
STEP 1: Access the Export Template option:
In Visual Studio, navigate to the Project menu and click on "Export Template."
STEP 2: Choose the template type:
When the Export Template wizard appears, select "Project template" instead of "Item template." This ensures that your entire project will be exported as a template.
STEP 3: Provide template details:
The wizard will prompt you to enter a Template Name (note that this name will appear in Visual Studio), a description, and an optional representative icon for the template.
STEP 4: Complete the export process:
Click "Finish" to complete the template export. The exported template will be saved as a zip file in the following location: C:UsersusernameDocumentsVisual Studio 2017My Exported Templates.
STEP 5: Overcome a known issue:
Due to a bug in Visual Studio, you need to perform an additional step. Close Visual Studio and extract the contents of the exported archive. Then, edit the MyTemplate.vstemplate manifest file and add the following node: <CreateInPlace>true</CreateInPlace>.
STEP 6: Package the template:
After making the necessary changes, zip the entire project folder, including the manifest file modification.
STEP 7: Finalize the process:
You have now created a Visual Studio template. Proceed with the desired steps to distribute or use the template in Visual Studio for future projects.
Steps to create the VSIX Extension
STEP 1: Create a new VSIX project:
In Visual Studio, navigate to the Extensibility node and select the VSIX project template. Create a new project using this template.
STEP 2: Import the fixed zip file:
Import the zip file that contains your modified template base into the newly created VSIX project solution. This can be done by adding the zip file to the project or dragging and dropping it into the solution explorer.
STEP 3: Configure the zip file properties:
Right-click on the imported zip file in the solution explorer and go to Properties. In the properties window, set the Build Action to "Copy to Output Directory" and select "Copy always" to ensure that the zip file is included in the output directory of the VSIX project.
STEP 4: Edit the source.extension.vsixmanifest file:
Double-click on the source.extension.vsixmanifest file in the solution explorer to open it for editing. This file contains the metadata and assets information for the final VSIX file.
STEP 5: Customize the metadata and assets:
Within the source.extension.vsixmanifest editor, update the metadata and assets according to your requirements. This includes providing a meaningful display name, description, version number, and other relevant information about your VSIX extension.
STEP 6: Finalize the project:
Once you have customized the metadata and assets, save the changes to the source.extension.vsixmanifest file.
Steps to Edit Metadata of the VSIX File
Edit the metadata:
Click on the Metadata tab in the source.extension.vsixmanifest editor.
Add the desired details in the available input sections, such as the display name, description, version number, and other relevant metadata for your VSIX extension.
Edit the assets:
Switch to the Assets tab in the source.extension.vsixmanifest editor.
Click on the "New" button to add a new asset.
Select "Microsoft.VisualStudio.ProjectTemplate" as the type of asset.
Choose "File on filesystem" as the source and provide the path to your modified zip file containing the template.
Click "OK" to add the asset.
Build the project:
Build the VSIX project by selecting the "Build" option from the Visual Studio menu or using the keyboard shortcut (e.g., Ctrl + Shift + B).
Locate the final .vsix file:
After a successful build, navigate to the "bin/debug" folder within your project directory.
You will find the final, installable .vsix file in this folder. This file can be distributed within your organization or team.
Install the extension:
Install the .vsix file as you would install any other extension from the Visual Studio marketplace.
Once installed, you will be able to create new projects based on your custom template.
By using this extension, you can create new projects in Visual Studio that include the predefined folder structure, controllers, files, and the necessary NuGet packages you have set up in your template.
Remember, Visual Studio can be extended with custom extensions, and creating your own extensions can be valuable for your team, colleagues, or organization. Don't limit yourself to using existing extensions—explore the possibilities of creating and sharing your own useful extensions.
Image source: Fortech