/// <summary> /// Executed when there is progress reported from BackgroundWorker Logic. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> static void Generate_ProgressChanged(object sender, ProgressChangedEventArgs e) { var worker = (BackgroundWorker)sender; //if (worker.CancellationPending) { } else if (e.UserState is Exception) { var ex = (Exception)e.UserState; // Delete Template Class File TemplateClass.Delete(); // Log Error LogManager.LogError(ex); // Raise OnException event GeneratorManager.RaiseEvent <GeneratorOnExceptionEventArgs>(new GeneratorOnExceptionEventArgs(ex)); } else if (e.UserState is GeneratorOnProgressEventArgs) { var eventArgs = (GeneratorOnProgressEventArgs)e.UserState; // Raise OnProgress event GeneratorManager.RaiseEvent <GeneratorOnProgressEventArgs>(eventArgs); } else if (e.UserState is GeneratorOnCompleteEventArgs) { var eventArgs = (GeneratorOnCompleteEventArgs)e.UserState; // Raise OnComplete event GeneratorManager.RaiseEvent <GeneratorOnCompleteEventArgs>(eventArgs); } }
/// <summary> /// Checks if there is a worker cancellation pending. /// </summary> /// <returns></returns> public static bool CheckCancellationPending() { if (_worker != null && _worker.CancellationPending) { // Wait for worker signal _resetEvent.Set(); // Delete Template Class File TemplateClass.Delete(); return(true); } return(false); }
/// <summary> /// Generates the DTOs of the EDMX Document provided using the parameters received. /// </summary> /// <param name="parameters">Parameters for the generation of DTOs.</param> /// <param name="worker">BackgroundWorker reference.</param> public static List <DTOEntity> GenerateDTOs(GenerateDTOsParams parameters, BackgroundWorker worker) { LogManager.LogMethodStart(); if (parameters.DTOsServiceReady) { VisualStudioHelper.AddReferenceToProject(parameters.TargetProject, Resources.AssemblySystemRuntimeSerialization, Resources.AssemblySystemRuntimeSerialization); } if (GeneratorManager.CheckCancellationPending()) { return(null); } EditPoint objEditPoint; // EditPoint to reuse CodeParameter objCodeParameter; // CodeParameter to reuse CodeNamespace objNamespace = null; // Namespace item to add Classes ProjectItem sourceFileItem = null; // Source File Item to save int dtosGenerated = 0; List <EnumType> enums = DTOGenerator.GetEnumTypes(parameters); PropertyHelper.SetEnumTypes(enums); List <DTOEntity> entitiesDTOs = DTOGenerator.GetEntityDTOs(parameters); if (GeneratorManager.CheckCancellationPending()) { return(null); } worker.ReportProgress(0, new GeneratorOnProgressEventArgs(0, string.Format(Resources.Text_DTOsGenerated, dtosGenerated, entitiesDTOs.Count))); TemplateClass.CreateFile(); // Imports to add to the Source File var importList = new List <SourceCodeImport>(); // EnumTypes defined in the EDMX ? if (enums.Exists(e => e.IsExternal == false)) { importList.Add(new SourceCodeImport(parameters.EntitiesNamespace)); VisualStudioHelper.AddReferenceToProject(parameters.TargetProject, parameters.EDMXProject); } // Include imports of external enums. foreach (string externalEnumNamespace in enums.Where(e => e.IsExternal).Select(e => e.Namespace).Distinct()) { importList.Add(new SourceCodeImport(externalEnumNamespace)); } // Generate Source File if type is One Source File if (parameters.SourceFileGenerationType == SourceFileGenerationType.OneSourceFile) { sourceFileItem = null; // Generate Source and Get the Namespace item objNamespace = VisualStudioHelper.GenerateSourceAndGetNamespace( parameters.TargetProject, parameters.TargetProjectFolder, parameters.SourceFileName, parameters.SourceFileHeaderComment, parameters.SourceNamespace, parameters.DTOsServiceReady, out sourceFileItem); // Add Imports to Source File VisualStudioHelper.AddImportsToSourceCode(ref sourceFileItem, importList); } // Check Cancellation Pending if (GeneratorManager.CheckCancellationPending()) { return(null); } // Loop through Entities DTOs foreach (DTOEntity entityDTO in entitiesDTOs) { // Generate Source File if type is Source File per Class if (parameters.SourceFileGenerationType == SourceFileGenerationType.SourceFilePerClass) { sourceFileItem = null; // Generate Source and Get the Namespace item objNamespace = VisualStudioHelper.GenerateSourceAndGetNamespace( parameters.TargetProject, parameters.TargetProjectFolder, entityDTO.NameDTO, parameters.SourceFileHeaderComment, parameters.SourceNamespace, parameters.DTOsServiceReady, out sourceFileItem); // Add Imports to Source File VisualStudioHelper.AddImportsToSourceCode(ref sourceFileItem, importList); } // Add Class CodeClass objCodeClass = objNamespace.AddClassWithPartialSupport(entityDTO.NameDTO, entityDTO.NameBaseDTO, entityDTO.DTOClassAccess, entityDTO.DTOClassKind); // Set IsAbstract objCodeClass.IsAbstract = entityDTO.IsAbstract; // Set Class Attributes foreach (DTOAttribute classAttr in entityDTO.Attributes) { objCodeClass.AddAttribute(classAttr.Name, classAttr.Parameters, AppConstants.PLACE_AT_THE_END); } // Set Class Properties foreach (DTOClassProperty entityProperty in entityDTO.Properties) { // Add Property CodeProperty objCodeProperty = objCodeClass.AddProperty(entityProperty.PropertyName, entityProperty.PropertyName, entityProperty.PropertyType, AppConstants.PLACE_AT_THE_END, entityProperty.PropertyAccess, null); // Get end of accessors auto-generated code objEditPoint = objCodeProperty.Setter.EndPoint.CreateEditPoint(); objEditPoint.LineDown(); objEditPoint.EndOfLine(); var getSetEndPoint = objEditPoint.CreateEditPoint(); // Move to the start of accessors auto-generated code objEditPoint = objCodeProperty.Getter.StartPoint.CreateEditPoint(); objEditPoint.LineUp(); objEditPoint.LineUp(); objEditPoint.EndOfLine(); // Replace accessors auto-generated code with a more cleaner one objEditPoint.ReplaceText(getSetEndPoint, Resources.CSharpCodeGetSetWithBrackets, Convert.ToInt32(vsEPReplaceTextOptions.vsEPReplaceTextAutoformat)); // Set Property Attributes foreach (DTOAttribute propAttr in entityProperty.PropertyAttributes) { objCodeProperty.AddAttribute(propAttr.Name, propAttr.Parameters, AppConstants.PLACE_AT_THE_END); } objEditPoint = objCodeProperty.StartPoint.CreateEditPoint(); objEditPoint.SmartFormat(objEditPoint); } if (parameters.GenerateDTOConstructors) { // Add empty Constructor CodeFunction emptyConstructor = objCodeClass.AddFunction(objCodeClass.Name, vsCMFunction.vsCMFunctionConstructor, null, AppConstants.PLACE_AT_THE_END, vsCMAccess.vsCMAccessPublic, null); // Does this DTO have a Base Class ? if (entityDTO.BaseDTO != null) { // Add call to empty Base Constructor objEditPoint = emptyConstructor.StartPoint.CreateEditPoint(); objEditPoint.EndOfLine(); objEditPoint.Insert(Resources.Space + Resources.CSharpCodeBaseConstructor); } // Does this DTO have properties ? if (entityDTO.Properties.Count > 0) { // Add Constructor with all properties as parameters CodeFunction constructorWithParams = objCodeClass.AddFunction(objCodeClass.Name, vsCMFunction.vsCMFunctionConstructor, null, AppConstants.PLACE_AT_THE_END, vsCMAccess.vsCMAccessPublic, null); foreach (DTOClassProperty entityProperty in entityDTO.Properties) { // Add Constructor parameter objCodeParameter = constructorWithParams.AddParameter( Utils.SetFirstLetterLowercase(entityProperty.PropertyName), entityProperty.PropertyType, AppConstants.PLACE_AT_THE_END); // Add assignment objEditPoint = constructorWithParams.EndPoint.CreateEditPoint(); objEditPoint.LineUp(); objEditPoint.EndOfLine(); objEditPoint.Insert(Environment.NewLine + AppConstants.TAB + AppConstants.TAB + AppConstants.TAB); objEditPoint.Insert(string.Format(Resources.CSharpCodeAssignmentThis, entityProperty.PropertyName, objCodeParameter.Name)); } // Does this DTO have a Base Class ? if (entityDTO.BaseDTO != null) { // Get the Base Class properties (includes the properties of the base recursively) List <DTOClassProperty> baseProperties = DTOGenerator.GetPropertiesForConstructor(entityDTO.BaseDTO); // Base Constructor parameters var sbBaseParameters = new StringBuilder(); foreach (DTOClassProperty entityProperty in baseProperties) { // Add Constructor parameter objCodeParameter = constructorWithParams.AddParameter( Utils.SetFirstLetterLowercase(entityProperty.PropertyName), entityProperty.PropertyType, AppConstants.PLACE_AT_THE_END); // Add parameter separation if other parameters exists if (sbBaseParameters.Length > 0) { sbBaseParameters.Append(Resources.CommaSpace); } // Add to Base Constructor parameters sbBaseParameters.Append(objCodeParameter.Name); } // Add call to Base Constructor with parameters objEditPoint = constructorWithParams.StartPoint.CreateEditPoint(); objEditPoint.EndOfLine(); objEditPoint.Insert( Environment.NewLine + AppConstants.TAB + AppConstants.TAB + AppConstants.TAB); objEditPoint.Insert( string.Format(Resources.CSharpCodeBaseConstructorWithParams, sbBaseParameters.ToString())); } // END if DTO has a Base Class } // END if DTO has properties } // END if Generate DTO Constructor methods // Save changes to Source File Item sourceFileItem.Save(); // Count DTO generated dtosGenerated++; // Report Progress int progress = ((dtosGenerated * 100) / entitiesDTOs.Count); if (progress < 100) { worker.ReportProgress(progress, new GeneratorOnProgressEventArgs(progress, string.Format(Resources.Text_DTOsGenerated, dtosGenerated, entitiesDTOs.Count))); } // Check Cancellation Pending if (GeneratorManager.CheckCancellationPending()) { return(null); } } // END Loop through Entities DTOs // Save Target Project parameters.TargetProject.Save(); // Delete Template Class File TemplateClass.Delete(); // Report Progress worker.ReportProgress(100, new GeneratorOnProgressEventArgs(100, string.Format(Resources.Text_DTOsGenerated, dtosGenerated, entitiesDTOs.Count))); LogManager.LogMethodEnd(); // Return the DTOs generated return(entitiesDTOs); }
/// <summary> /// Generates Assemblers for DTOs generated using the parameters received. /// </summary> /// <param name="parameters">Parameters for the generation of Assemblers.</param> /// <param name="worker">BackgroundWorker reference.</param> public static void GenerateAssemblers(GenerateAssemblersParams parameters, BackgroundWorker worker) { LogManager.LogMethodStart(); // Variables EditPoint objEditPoint; // EditPoint to reuse CodeNamespace objNamespace = null; // Namespace item to add Classes ProjectItem sourceFileItem = null; // Source File Item to save int assemblersGenerated = 0; // Report Progress worker.ReportProgress(0, new GeneratorOnProgressEventArgs(0, string.Format(Resources.Text_AssemblersGenerated, assemblersGenerated, parameters.EntitiesDTOs.Count))); // Add Reference to System.Core VisualStudioHelper.AddReferenceToProject(parameters.TargetProject, Resources.AssemblySystemCore, Resources.AssemblySystemCore); // Add Reference to System.Data.Entity VisualStudioHelper.AddReferenceToProject(parameters.TargetProject, Resources.AssemblySystemDataEntity, Resources.AssemblySystemDataEntity); if (parameters.IsServiceReady) { // Add Reference to System.Runtime.Serialization VisualStudioHelper.AddReferenceToProject(parameters.TargetProject, Resources.AssemblySystemRuntimeSerialization, Resources.AssemblySystemRuntimeSerialization); } if (parameters.TargetProject.UniqueName != parameters.EDMXProject.UniqueName) { // Add Reference to EDMX Project VisualStudioHelper.AddReferenceToProject(parameters.TargetProject, parameters.EDMXProject); } if (parameters.TargetProject.UniqueName != parameters.DTOsTargetProject.UniqueName) { // Add Reference to DTOs Project VisualStudioHelper.AddReferenceToProject(parameters.TargetProject, parameters.DTOsTargetProject); } // Check Cancellation Pending if (GeneratorManager.CheckCancellationPending()) { return; } // Create Template Class File TemplateClass.CreateFile(); // Imports to add to the Source File var importList = new List <SourceCodeImport>(); importList.Add(new SourceCodeImport(Resources.AssemblySystemLinq)); if (parameters.SourceNamespace != parameters.DTOsNamespace) { // Add import of DTOs namespace importList.Add(new SourceCodeImport(parameters.DTOsNamespace)); } if (parameters.SourceNamespace != parameters.EntitiesNamespace) { // Add import of Entities namespace importList.Add(new SourceCodeImport(parameters.EntitiesNamespace)); } // Generate Source File if type is One Source File if (parameters.SourceFileGenerationType == SourceFileGenerationType.OneSourceFile) { sourceFileItem = null; // Generate Source and Get the Namespace item objNamespace = VisualStudioHelper.GenerateSourceAndGetNamespace( parameters.TargetProject, parameters.TargetProjectFolder, parameters.SourceFileName, parameters.SourceFileHeaderComment, parameters.SourceNamespace, parameters.IsServiceReady, out sourceFileItem); // Add import of System.Data.Objects.DataClasses (necessary for AssemblerBase) importList.Add(new SourceCodeImport(Resources.AssemblySystemDataObjectsDataClasses)); // Add Imports to Source File VisualStudioHelper.AddImportsToSourceCode(ref sourceFileItem, importList); } // Check Cancellation Pending if (GeneratorManager.CheckCancellationPending()) { return; } // Set Assembler for all DTOs foreach (DTOEntity dto in parameters.EntitiesDTOs) { dto.SetAssembler(parameters); } // Check Cancellation Pending if (GeneratorManager.CheckCancellationPending()) { return; } // Loop through Entities DTOs foreach (DTOEntity dto in parameters.EntitiesDTOs) { if (dto.IsAbstract == true && (dto.DTOChilds == null || dto.DTOChilds.Count == 0)) { // DTO is abstract and does not have childs // Get the source file name if it is one source file generation type string sourceFileName = null; if (parameters.SourceFileGenerationType == SourceFileGenerationType.OneSourceFile) { sourceFileName = sourceFileItem.Name; } // Add warning VisualStudioHelper.AddToErrorList(TaskErrorCategory.Warning, string.Format(Resources.Warning_AbstractWithoutChildsAssembler, dto.Name), parameters.TargetProject, sourceFileName, null, null); } else { // Generate Source File if type is Source File per Class if (parameters.SourceFileGenerationType == SourceFileGenerationType.SourceFilePerClass) { sourceFileItem = null; // Generate Source and Get the Namespace item objNamespace = VisualStudioHelper.GenerateSourceAndGetNamespace( parameters.TargetProject, parameters.TargetProjectFolder, dto.Assembler.Name, parameters.SourceFileHeaderComment, parameters.SourceNamespace, parameters.IsServiceReady, out sourceFileItem); // Add Imports to Source File VisualStudioHelper.AddImportsToSourceCode(ref sourceFileItem, importList); } // Instance creation code variables var toDTOInstanceCode = string.Empty; var toEntityInstanceCode = string.Empty; #region Generate instance creation code if (dto.DTOChilds != null && dto.DTOChilds.Count > 0) { bool firstIfStatement = true; // Add null assignment for DTO instance code toDTOInstanceCode += string.Format(Resources.AssemblerToDTOInstanceNull, dto.NameDTO); toDTOInstanceCode += Environment.NewLine + Environment.NewLine; // Add null assignment for Entity instance code toEntityInstanceCode += string.Format(Resources.AssemblerToEntityInstanceNull, dto.Name); toEntityInstanceCode += Environment.NewLine + Environment.NewLine; // Loop DTO Child classes foreach (DTOEntity dtoChild in dto.DTOChilds) { // Set if statement checking if the entity is of this type, if it is then invoke DTO Assembler ToDTO method if (firstIfStatement) { firstIfStatement = false; toDTOInstanceCode += string.Format(Resources.AssemblerToDTOInstanceIfChild, dtoChild.Name); toEntityInstanceCode += string.Format(Resources.AssemblerToEntityInstanceIfChild, dtoChild.NameDTO); } else { toDTOInstanceCode += Environment.NewLine; toDTOInstanceCode += string.Format(Resources.AssemblerToDTOInstanceElseIfChild, dtoChild.Name); toEntityInstanceCode += Environment.NewLine; toEntityInstanceCode += string.Format(Resources.AssemblerToEntityInstanceElseIfChild, dtoChild.NameDTO); } toDTOInstanceCode += Environment.NewLine; toDTOInstanceCode += string.Format(Resources.AssemblerToDTOInstanceChild, dtoChild.Name); toEntityInstanceCode += Environment.NewLine; toEntityInstanceCode += string.Format(Resources.AssemblerToEntityInstanceChild, dtoChild.NameDTO); } if (dto.IsAbstract == false) { toDTOInstanceCode += Environment.NewLine + Resources.AssemblerToDTOInstanceElse + Environment.NewLine; toDTOInstanceCode += string.Format(Resources.AssemblerToDTOInstanceNew, dto.NameDTO); toEntityInstanceCode += Environment.NewLine + Resources.AssemblerToEntityInstanceElse + Environment.NewLine; toEntityInstanceCode += string.Format(Resources.AssemblerToEntityInstanceNew, dto.Name); } } else if (dto.IsAbstract == false) { // No childs, simple DTO instance toDTOInstanceCode = Resources.CSharpCodeVar + Resources.Space; toDTOInstanceCode += string.Format(Resources.AssemblerToDTOInstanceNew, dto.NameDTO); toEntityInstanceCode = Resources.CSharpCodeVar + Resources.Space; toEntityInstanceCode += string.Format(Resources.AssemblerToEntityInstanceNew, dto.Name); } #endregion Generate instance creation code // Property assignments code variables var toDTOAssignmentsCode = string.Empty; var toEntityAssignmentsCode = string.Empty; #region Generate property assignments code if ((dto.DTOChilds != null && dto.DTOChilds.Count > 0) || dto.IsAbstract == false) { foreach (var property in dto.Properties) { bool includeAssignments = true; string toDTOAssignment = null; string toEntityAssignment = null; if (property.IsNavigation == true) { // Do not map navigation properties. // The developer has to map navigations manually on partial methods if needed. includeAssignments = false; } else if (property.IsComplex == true) { toDTOAssignment = string.Format(Resources.AssemblerToDTOAssignmentEntityComplexProp, property.PropertyNameEDMX); toEntityAssignment = string.Format(Resources.AssemblerToEntityAssignmentDTOComplexProp, property.PropertyName); } else { toDTOAssignment = string.Format(Resources.AssemblerToDTOAssignmentEntityProp, property.PropertyNameEDMX); toEntityAssignment = string.Format(Resources.AssemblerToEntityAssignmentDTOProp, property.PropertyName); } if (includeAssignments) { // ToDTO assignment => dto.prop = ... toDTOAssignmentsCode += Environment.NewLine + string.Format(Resources.AssemblerToDTOAssignment, property.PropertyName, toDTOAssignment); // ToEntity assignment => entity.prop = ... toEntityAssignmentsCode += Environment.NewLine + string.Format(Resources.AssemblerToEntityAssignment, property.PropertyNameEDMX, toEntityAssignment); } } } #endregion Generate property assignments code // Get the start point where to insert the Assembler code objEditPoint = objNamespace.EndPoint.CreateEditPoint(); objEditPoint.LineUp(); objEditPoint.EndOfLine(); objEditPoint.Insert(Environment.NewLine); // Get the Assembler's code string assemblerCode = dto.Assembler.GetAssemblerCode(toDTOInstanceCode, toEntityInstanceCode, toDTOAssignmentsCode, toEntityAssignmentsCode); // Insert Assembler's code objEditPoint.Insert(assemblerCode); // Format code objEditPoint.StartOfDocument(); EditPoint endOfDocument = objNamespace.EndPoint.CreateEditPoint(); endOfDocument.EndOfDocument(); objEditPoint.SmartFormat(endOfDocument); // Save changes to Source File Item sourceFileItem.Save(); } // END (else) if dto is abstract and does not have childs // Count Assemblers generated assemblersGenerated++; // Report Progress int progress = ((assemblersGenerated * 100) / parameters.EntitiesDTOs.Count); if (progress < 100) { worker.ReportProgress(progress, new GeneratorOnProgressEventArgs(progress, string.Format(Resources.Text_AssemblersGenerated, assemblersGenerated, parameters.EntitiesDTOs.Count))); } // Check Cancellation Pending if (GeneratorManager.CheckCancellationPending()) { return; } } // END Loop through Entities DTOs // Save Target Project parameters.TargetProject.Save(); // Delete Template Class File TemplateClass.Delete(); // Report Progress worker.ReportProgress(100, new GeneratorOnProgressEventArgs(100, string.Format(Resources.Text_AssemblersGenerated, assemblersGenerated, parameters.EntitiesDTOs.Count))); LogManager.LogMethodEnd(); }