我可以自动将代码和新类放入解决方案的许多部分吗



我想把我自动生成的代码和新类放在解决方案的不同部分。例如:

我有这些项目:

  • 型号
  • DAO
  • 业务
  • Web

当我在Model项目中手动创建一个新实体时,我想:

  • Model项目中创建IDAO类和IService类
  • DAO项目中创建您的DAO类
  • Business项目中创建您的服务类
  • Webglobal.asax文件中的Application_Start()方法中创建将接口注册到类的代码(Denpendency Injection)

一切都是自动的。那么,这可能吗?

它总是取决于项目和您试图实现的目标。

我使用T4来避免维护冗余代码。在你描述的场景中,经常有一些代码感觉是多余的,很难使其成为非冗余的。

我会做什么:

  • 以某种方式描述我的模型(T4代码、XML或您喜欢的任何东西)
  • 创建一个T4模板,该模板采用模型并构建model类
  • 创建一个T4模板,该模板采用模型并构建DAO类
  • 创建一个T4模板,该模板采用该模型并构建业务类
  • 创建一个T4模板,用于注册类以进行依赖项注入

当我在模式文件中添加一个新实体时,我会在VS中点击"转换所有模板",或者将其添加到构建系统中。这将为Model、DAO、Business和Application_Start生成必要的代码。

这个解决方案的好坏取决于你正在进行的项目以及你想要实现的目标。

使用Visual Studio宏可以非常容易地自动化这类内容。

有两个地方可以开始:

  • 在web上搜索创建文档的VS宏,或应用您对感兴趣的其他操作

  • 查看"工具">"宏"菜单。从这里,您可以录制一个临时宏(开始录制,然后执行诸如创建文档之类的操作,然后停止录制)。然后在Macros IDE中打开录制宏,看看它生成了什么代码(对于某些操作,你不会得到任何东西,但大多数时候VS非常擅长从你的UI操作中生成一些有用的代码)。然后,您只需要将录制的代码片段拼接在一起,构建一个宏来自动化tedius任务通常非常容易。

已解决!我使用visual studio宏。

我改变了策略。我没有创建T4文件(.tt),而是创建了一个带有一些标签的公共类文件(.cs),这些标签将由String.Replace.更改

using System;
// place here your necessary "usings" 
namespace #namespace#
{
    /// <summary>
    /// Class responsible for #entity# data access.
    /// </summary>
    public class #class# : I#class#
    {
    }
}

宏:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports System.IO
Public Module Templates
    ' Get the active class (in this case will be the entity that you have just created)
    Dim entity As String = DTE.ActiveDocument.Name.Replace(".cs", "")
    Dim projects As Projects = DTE.Solution.Projects
    ' Project where are the template classes   
    Dim projectTemplates As Project = DTE.ActiveDocument.ProjectItem.ContainingProject
    Sub CreateCRUD()
        ' Exemple of creating DAO Class
        CreateClass("NamespaceWhereAreYouProject.Repositorio NamespaceWhereAreYouProject.Repositorio.csproj", _
                    "DAL", _
                    " NamespaceWhereAreYouProject.Repository.DAL", _
                    "DAO" + entity, _
                    "DAOTemplate.cs")
    End Sub
    Private Sub CreateClass(ByVal currentProject As String, _
                                 ByVal currentFolder As String, _
                                 ByVal currentNamespace As String, _
                                 ByVal currentClass As String, _
                                 ByVal currentTemplate As String)
        ' Loading attributes
        Dim project As Project = projects.Item(currentProject)
        Dim folder As ProjectItem = project.ProjectItems.Item(currentFolder)
        Dim currentTemplatePath As String = projectTemplates.ProjectItems.Item("Templates") _
            .ProjectItems.Item(currentTemplate).Properties.Item("FullPath").Value
        Dim newClassPath As String = folder.Properties.Item("FullPath").Value + currentClass + ".cs"
        ' Creating class
        folder.ProjectItems.AddFromTemplate(currentTemplatePath, currentClass + ".cs")
        Dim newText As String = File.ReadAllText(newClassPath) _
            .Replace("#class#", currentClass) _
            .Replace("#namespace#", currentNamespace) _
            .Replace("#entity#", entity)
        File.WriteAllText(newClassPath, newText)
    End Sub
End Module

相关内容

最新更新