private void AddDataFieldAttribute(CodeProperty prop, DBObject obj, Column v) { string propType = v.IsPrimaryKey ? "DataAccess.Core.Attributes.Key" : "DataAccess.Core.Attributes.DataField"; string propValue = string.Format("FieldName=\"{0}\"{1}{2}", v.Name, GetDefaultValueString(v), GetFieldTypeString(v)); prop.AddAttribute(propType, propValue); }
/// <summary> /// Adds the attribute. /// </summary> /// <param name="element">The element.</param> /// <param name="attributeName">Name of the attribute.</param> /// <param name="attributeValue">The attribute value.</param> public static void AddAttribute(CodeProperty element, string attributeName, string attributeValue) { if (!HasAttribute(element, attributeName)) { element.AddAttribute(attributeName, attributeValue, 0); } }
protected override object NewAttribute(NewCodeElementItemParams p, string path, object newItemValue) { var value = null == newItemValue ? null : newItemValue.ToString(); return(_property.AddAttribute(path, value, p.Position.ToDTEParameter())); }
public ShellCodeAttribute AddAttribute(string Name, string Value, object Position) { return(new ShellCodeAttribute(_property.AddAttribute(Name, Value, Position) as CodeAttribute2)); }
/// <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> /// Selects the introduction page html to be at the top of the windows /// </summary> public override void Execute() { DTE vs = GetService <DTE>(true); ProjectItem actionClassFile = (ProjectItem)DteHelper.GetTarget(vs); if ((actionClassFile == null) || (actionClassFile.FileCodeModel == null) || (actionClassFile.FileCodeModel.CodeElements == null) || (actionClassFile.FileCodeModel.CodeElements.Count == 0)) { return; } string fieldName = ""; if ((ParameterName.Length > 1) && (ParameterName[0] >= 'A') && (ParameterName[0] <= 'Z')) { fieldName = char.ToLower(parameterName[0], CultureInfo.CurrentCulture) + parameterName.Substring(1); } else { fieldName = "_" + parameterName; } string attribute; if (ParameterIsOutput) { attribute = "Output"; } else { attribute = "Input"; } bool addedProperty = false; foreach (CodeElement element in actionClassFile.FileCodeModel.CodeElements) { if (element.Kind == vsCMElement.vsCMElementNamespace) { foreach (CodeElement elementNamespace in ((CodeNamespace)element).Members) { if (elementNamespace.Kind == vsCMElement.vsCMElementClass) { #region Look where to insert the property object whereToInsert = null; CodeClass classAction = (CodeClass)elementNamespace; CodeProperty foundProperty = null; foreach (CodeElement classElement in classAction.Members) { if (foundProperty != null) { if (classElement.Kind == vsCMElement.vsCMElementVariable) { // Then insert after this declaration of variable that was after the property whereToInsert = classElement; } } if (classElement.Kind == vsCMElement.vsCMElementProperty) { foundProperty = (CodeProperty)classElement; bool hasAttribute = false; for (int i = 1; i <= foundProperty.Attributes.Count; i++) { if (foundProperty.Attributes.Item(i).Name == attribute) { hasAttribute = true; break; } } if (!hasAttribute) { foundProperty = null; } } } if (foundProperty != null) { if (whereToInsert == null) { whereToInsert = foundProperty; } } else if (whereToInsert == null) { whereToInsert = 0; } #endregion CodeProperty prop = classAction.AddProperty(ParameterName, ParameterName, ParameterType, whereToInsert, vsCMAccess.vsCMAccessPublic, actionClassFile.Name); TextPoint getTextTP = prop.Getter.GetStartPoint(vsCMPart.vsCMPartBody); EditPoint getText = getTextTP.CreateEditPoint(); getText.ReplaceText(prop.Getter.GetEndPoint(vsCMPart.vsCMPartBody), string.Format(CultureInfo.InvariantCulture, "return {0};", fieldName), (int)vsEPReplaceTextOptions.vsEPReplaceTextNormalizeNewlines); getText.SmartFormat(getTextTP); TextPoint setTextTP = prop.Setter.GetStartPoint(vsCMPart.vsCMPartBody); EditPoint setText = setTextTP.CreateEditPoint(); setText.ReplaceText(0, string.Format(CultureInfo.InvariantCulture, "{0}=value;", fieldName), 0); setText.SmartFormat(setTextTP); if (ParameterIsOutput) { prop.AddAttribute(attribute, "", 0); } else { prop.AddAttribute(attribute, "true", 0); } classAction.AddVariable(fieldName, ParameterType, prop, vsCMAccess.vsCMAccessPrivate, actionClassFile.Name); // Stop adding property, just the first class found addedProperty = true; break; } } if (addedProperty) { break; } } } }
/// <summary> /// Adds the attribute internal. /// </summary> /// <param name="name">The name.</param> /// <returns></returns> protected override CodeAttribute2 AddAttributeInternal(string name) { return(_codeElement.AddAttribute(name, String.Empty, null) as CodeAttribute2); }