我的大部分代码都包含三个相关的类,一个基于一个名为IImplementation的接口,一个Execute方法(帮助强制执行单一责任)以及一个响应和上下文类。我厌倦了这个,它费力,重复和耗时,所以今天我决定写一个C#项目模板。
模板文件和.vstemplate文件如下所示:
Implmentation.vstemplate
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:sdk="http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010">
<TemplateData>
<Name>Implementation</Name>
<Description>Genertates a skeletal concrete IImplementation class complete with Response and Context classes.</Description>
<Icon>Class.ico</Icon>
<TemplateID>74206442-81cb-4328-afc8-08c40fb5d3e7</TemplateID>
<ProjectType>CSharp</ProjectType>
<RequiredFrameworkVersion>2.0</RequiredFrameworkVersion>
<NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp>
<DefaultName>Class.cs</DefaultName>
</TemplateData>
<TemplateContent>
<References>
<Reference>
<Assembly>System</Assembly>
</Reference>
</References>
<ProjectItem SubType="Code" TargetFileName="$fileinputname$.cs" ReplaceParameters="true">Implementation.cs</ProjectItem>
<ProjectItem SubType="Code" TargetFileName="$fileinputname$.cs\$fileinputname$Response.cs" ReplaceParameters="true">Response.cs</ProjectItem>
<ProjectItem SubType="Code" TargetFileName="$fileinputname$.cs\$fileinputname$Context.cs" ReplaceParameters="true">Context.cs</ProjectItem>
</TemplateContent>
<WizardExtension>
<Assembly>Microsoft.VisualStudio.Web.Application, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
<FullClassName>Microsoft.VisualStudio.Web.Application.WATemplateWizard /> </WizardExtension>
</VSTemplate>
Implementation.cs
using System;
using $safeprojectname$.Implementation.Attibutes;
using $safeprojectname$.Implementation.Interfaces;
namespace $rootnamespace$
{
[AutofacRegisterImplementation]
public class $classname$ :
IImplementation<$classname$Response, $classname$Context>
{
#region Execute
public $fileinputname$Response Execute($fileinputname$Context context)
{
throw new NotImplementedException();
}
#endregion
}
}
Response.cs
namespace $rootnamespace$
{
public class $classname$Response
{
#region Properties
#endregion
}
}
Context.cs
namespace $rootnamespace$
{
public class $classname$Context
{
#region Properties
#endregion
}
}
它有点好用,我接近快乐,因为我甚至设法将响应和上下文类嵌套在植入类下面,这意味着在具有大量实现的项目文件中更少混乱(我尝试了一些东西)很难避免!)
除来自Implementation.cs的以下片段:
using $safeprojectname$.Implementation.Attibutes;
using $safeprojectname$.Implementation.Interfaces;
生成此(整个生成的文件):
using System;
using $safeprojectname$.Implementation.Attibutes;
using $safeprojectname$.Implementation.Interfaces;
namespace Balcony.Common.Implementation
{
[AutofacRegisterImplementation]
public class Test1 :
IImplementation<Test1Response, Test1Context>
{
#region Execute
public Test1Response Execute(Test1Context context)
{
throw new NotImplementedException();
}
#endregion
}
}
这让我得出结论$ safeprojectname $仅适用于C#项目模板。这是正确的吗?
有人知道如何在C#项目模板中正确扩展安全项目名称变量吗?
同时在它...如何进入New from Template子菜单?你在哪里可以窃取Class.ico文件? :)
答案 0 :(得分:1)
我说使用$ rootnamespace $代替项目名称,这应该做同样的事情。