locked
Can i put code and new classes in a lot of parts of my solution automatically? RRS feed

  • Question

  • I want to put code that i generate automatically and new classes in different parts of my solution. Ex:

    I have these projects:

    • Model
    • DAO
    • Business
    • Web

    When i create manually a new entity in Model project i want to:

    • Create in Model project your IDAO class and IService class
    • Create in DAO project your DAO class.
    • Create in Business project your Service class.
    • Create in Application_Start() method that is in Web\global.asax file the code that register the interfaces to your classes (Denpendency Injection).

    All things automatically. So, is it possible?


    Caso esse post foi útil para você não esqueça de "Votar como Útil". =)

    Thursday, February 16, 2012 3:29 PM

Answers

  • Solved!

    I changed the strategy. Instead of create a T4 file (.tt), i create a common class file (.cs) with some tags that will be changed by String.Replace.

    using System; 
    // place here your necessary "usings"  
     
    namespace #namespace# 

        /// <summary> 
        /// Class responsible for #entity# data access. 
        /// </summary> 
        public class #class# : I#class# 
        { 
        } 

    The Macro:

    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 

    Caso esse post foi útil para você não esqueça de "Votar como Útil". =)


    Monday, February 27, 2012 11:49 AM

All replies

  • Hi

    I'm not clear about your situation, but if you want to do some Visual Studio automation to write code for you automaticly, yes, it is possiable.

    Generally, we can use visual studio automation object (DTE) to access the codemodel of the source code file, and we can create code object, like class, namespace, functions, etc into your source code directly. You can find detail method from Discovering Code by Using the Code Model (Visual C#), http://msdn.microsoft.com/en-us/library/ms228763.aspx.

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, February 17, 2012 6:42 AM
  • Hello,

    Would you mind letting me know the result of the suggestions? If you need further assistance, feel free to let me know. I will be more than happy to be of assistance.

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, February 21, 2012 5:30 AM
  • I'm trying to create a macro to add a new class file based in a T4 file in a specific project in my solution. I tried:

    Dim currentProject As Project = DTE.ActiveDocument.ProjectItem.ContainingProject
    Dim templatePath As String = currentProject.ProjectItems.Item("Templates").ProjectItems.Item("TemplateDAO.tt").Properties.Item("FullPath").Value
    currentProject.ProjectItems.Item("Entidades").ProjectItems.AddFromTemplate(templatePath, "TestDAO.cs")

    But the code generated is the .tt code, not the .cs from the .tt.

    How can i do that?


    Caso esse post foi útil para você não esqueça de "Votar como Útil". =)


    Friday, February 24, 2012 4:24 PM
  • Hi

    You need to set Custom Tool as TextTemplatingFileGenerator and execute the custom build to make the tt file generate its output.

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, February 27, 2012 8:55 AM
  • Solved!

    I changed the strategy. Instead of create a T4 file (.tt), i create a common class file (.cs) with some tags that will be changed by String.Replace.

    using System; 
    // place here your necessary "usings"  
     
    namespace #namespace# 

        /// <summary> 
        /// Class responsible for #entity# data access. 
        /// </summary> 
        public class #class# : I#class# 
        { 
        } 

    The Macro:

    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 

    Caso esse post foi útil para você não esqueça de "Votar como Útil". =)


    Monday, February 27, 2012 11:49 AM