Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a template for adding multiple files to a project?

My new project is my first look at WPF MVVM and WCF and I like it but it seems like I am creating a lot of files, always in the same basic setup and structure.

I am wondering if anyone has a way of defining some kind of folder/project structure as an input and automatically creating the various POCOs, views, models, service classes and interfaces, perhaps with some kind of consistent prefix for the file name.

Then the developer can just go in and cut the code to get their data.

I saw this, which is sort of the right idea:

http://www.codeproject.com/Articles/16515/Creating-a-Custom-Tool-to-Generate-Multiple-Files

A colleague also suggested a batch file might be worth investigating. Open to all ideas, thanks for your help.

UPDATE //

This would take place after the project was created. So the folders and projects are already in place but you want to add the necessary files for an end-to-end service call and presentation.

like image 457
GP24 Avatar asked Jun 13 '13 04:06

GP24


1 Answers

You can use Visual Studio's Item Template not only to add a single file, but also to add multiple items and sort them in your project folder structure:

If you create an item template with VS, via File -> Export Template, you get a zipped folder. You can unzip it and add several files, for example until it looks like this: template folder

You can then modify the file named 'MyTemplate'. The according file for the above example looks like this:

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">
  <TemplateData>
    <DefaultName>Enter Module Name here</DefaultName>
    <Name>INCA Module</Name>
    <Description>Creates all files for a module - Add this item at project root level only!</Description>
    <ProjectType>CSharp</ProjectType>
    <SortOrder>10</SortOrder>
    <Icon>__TemplateIcon.ico</Icon>
  </TemplateData>
  <TemplateContent>
    <References />
    <ProjectItem SubType="Code" TargetFileName="Models/$fileinputname$.cs" ReplaceParameters="true">ElementModel.cs</ProjectItem>
    <ProjectItem SubType="Code" TargetFileName="ViewModels$fileinputname$ViewModel.cs" ReplaceParameters="true">ElementViewModel.cs</ProjectItem>
    <ProjectItem SubType="Designer" TargetFileName="$fileinputname$View.xaml" ReplaceParameters="true">View.xaml</ProjectItem>
    <ProjectItem SubType="Code" TargetFileName="$fileinputname$View.xaml.cs" ReplaceParameters="true">View.xaml.cs</ProjectItem>
    <ProjectItem SubType="Designer" TargetFileName="Metadata/$fileinputname$Metadata.xaml" ReplaceParameters="true">Metadata.xaml</ProjectItem>
    <ProjectItem SubType="Code" TargetFileName="Metadata/$fileinputname$Metadata.xaml.cs" ReplaceParameters="true">Metadata.xaml.cs</ProjectItem>
  </TemplateContent>
</VSTemplate>

I think, what happens here is quite self explanatory.

It is very helpful to use template parameters:

MSDN on Template Parameters

Then just zip the whole thing again and place it in the folder User/Visual Studio xy/Templates. Note, that templates are exported to the folder called MyExportedTemplates.

like image 177
Marc Avatar answered Sep 19 '22 08:09

Marc