2012-02-16 24 views
3

我想把我自動生成的代碼和新的類放在我的解決方案的不同部分。例如:我可以自動在我的解決方案的很多部分放置代碼和新類嗎?

我有這些項目:

  • 型號
  • DAO
  • 業務
  • 網絡

當我手動創建Model項目一個新的實體,我想:

  • Model項目中創建您的IDAO類和IService類
  • DAO中創建項目您的DAO類。
  • Business中創建項目您的服務類。
  • Application_Start()中創建的方法在Web\global.asax文件中註冊接口到您的類(Denpendency Injection)的代碼。

所有的東西自動。那麼,有可能嗎?

+1

不認爲這是可能的,真的。你可以自然地使用T4模板來實現其中的一部分,但不一定是你要求的那種類型的表現。也許你可以爲此編寫自己的插件。 – Tigran 2012-02-16 15:29:31

+0

爲此目的有一種「種類」的插件,這個名稱可以幫助我的搜索? – 2012-02-16 15:36:26

回答

0

解決!我使用visual studio宏。

Obs:我改變了策略。除了創建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 
1

你可以很容易地使用Visual Studio宏自動化這類東西。

有上手兩個地方:

  • 搜索以創建文檔,或者應用您有興趣

  • 瞭解這些工具的其他行動VS宏網>宏菜單。從這裏你可以錄製一個臨時宏(開始錄製,然後做一個如創建一個文檔,然後停止錄製)。然後在Macros IDE中打開錄製宏,看看它生成了什麼代碼(對於某些操作你不會得到任何東西,但大部分時間VS非常擅長從UI操作中生成有用的東西)。然後,您只需將所記錄的代碼片段拼湊在一起,構建宏以自動執行tedius任務通常非常容易。

2

它總是依賴於項目和你試圖實現的目標。

我用T4來避免不得不維護冗餘代碼。在這種情況下,你經常描述的代碼會感到冗餘,很難做出非冗餘的代碼。

我會怎麼做:

  • 形容我的模型以某種方式(T4代碼,XML或任何你喜歡)
  • 創建T4模板取模型,並建立了模型類
  • 創建T4模板構建模型並構建DAO類
  • 創建一個採用該模型並構建業務類的T4模板
  • 創建一個T4模板,該模板註冊依賴於jection

當我添加一個新的實體到我的模式文件時,我會點擊VS中的「Transform All Templates」或將它添加到構建系統中。這將爲Model,DAO,Business和Application_Start生成必要的代碼。

如果此解決方案的好壞取決於您正在進行的項目以及您想實現的目標。

+0

當我向我的「模型」中添加一個新實體時,如何才能打開「轉換所有模板」? Obs:我創建了所有t4模板。 – 2012-02-23 12:20:15

相關問題