/// <summary> /// Carries out the generation of code for a class /// </summary> /// <param name="sb">The string builder to append code to.</param> /// <param name="classGenerationParameters">The class generation parameters</param> protected override void DoGenerationOfClass(StringBuilder sb, IClassGenerationParameters classGenerationParameters) { if (sb == null) { throw new ArgumentNullException("sb"); } if (classGenerationParameters == null) { throw new ArgumentNullException("classGenerationParameters"); } var tabCount = 2; var classSuffix = this.GetClassSuffix(); // generate the main interface sb.AppendLine("{0}/// <summary>", Helpers.GetTabs(tabCount)); sb.AppendLine("{0}/// {1}", Helpers.GetTabs(tabCount), classGenerationParameters.ClassRemarks); sb.AppendLine("{0}/// </summary>", Helpers.GetTabs(tabCount)); var nucleotideAssembly = Assembly.GetExecutingAssembly().GetName(); sb.AppendLine($" [System.CodeDom.Compiler.GeneratedCode(\"DHGMS Nucleotide\", \"{nucleotideAssembly.Version}\")]"); sb.AppendLine("{0}public interface I{1}{2} : IUnkeyed{1}{2}", Helpers.GetTabs(tabCount), classGenerationParameters.ClassName, classSuffix); sb.AppendLine("{0}{{", Helpers.GetTabs(tabCount)); tabCount++; this.DoProperties(sb, tabCount, classGenerationParameters.Properties.Where(x => x.IsKey).ToArray(), false); tabCount--; sb.AppendLine("{0}}}", Helpers.GetTabs(tabCount)); sb.AppendLine(string.Empty); // generate the unkeyed interface sb.AppendLine("{0}/// <summary>", Helpers.GetTabs(tabCount)); sb.AppendLine("{0}/// Un-keyed interface for {1}{2}", Helpers.GetTabs(tabCount), classGenerationParameters.ClassRemarks, classSuffix); sb.AppendLine("{0}/// </summary>", Helpers.GetTabs(tabCount)); sb.AppendLine("{0}/// <remarks>", Helpers.GetTabs(tabCount)); sb.AppendLine("{0}/// Un-keyed interfaces are used in services that allow creation of new objects.", Helpers.GetTabs(tabCount)); sb.AppendLine("{0}/// </remarks>", Helpers.GetTabs(tabCount)); sb.AppendLine("{0}public interface IUnkeyed{1}{2}", Helpers.GetTabs(tabCount), classGenerationParameters.ClassName, classSuffix); sb.AppendLine("{0}{{", Helpers.GetTabs(tabCount)); tabCount++; this.DoProperties(sb, tabCount, classGenerationParameters.Properties.Where(x => !x.IsKey).ToArray(), true); tabCount--; sb.AppendLine("{0}}}", Helpers.GetTabs(tabCount)); }
/// <summary> /// The do get data columns method. /// </summary> /// <param name="generatedCode"> /// The String Builder to add the code to /// </param> /// <param name="classInfo"> /// The class Info. /// </param> protected static void DoGetDataColumnsMethod(StringBuilder generatedCode, IClassGenerationParameters classInfo) { if (generatedCode == null) { throw new ArgumentNullException(nameof(generatedCode)); } if (classInfo == null) { throw new ArgumentNullException(nameof(generatedCode)); } generatedCode.AppendLine(" /// <summary>"); generatedCode.AppendLine(" /// Gets a collection of data columns representing the type"); generatedCode.AppendLine(" /// </summary>"); generatedCode.AppendLine(" /// <returns>a collection of strings representing the data record</returns>"); generatedCode.AppendLine(" public override System.Data.DataColumn[] GetDataColumns()"); generatedCode.AppendLine(" {"); generatedCode.AppendLine(" System.Collections.Generic.List<System.Data.DataColumn> result ="); generatedCode.Append(" new System.Collections.Generic.List<System.Data.DataColumn>"); var baseClassProperties = classInfo.BaseClassProperties; if (baseClassProperties != null && baseClassProperties.Length > 0) { generatedCode.Append(" (base.GetDataColumns())"); } generatedCode.AppendLine(string.Empty); generatedCode.AppendLine(" {"); var properties = classInfo.Properties; for (int i = 0; i < properties.Length; i++) { generatedCode.AppendLine( " new System.Data.DataColumn(\"" + properties[i].Name + "\", typeof(" + properties[i].NetDataType + "))" + (i < properties.Length - 1 ? "," : string.Empty)); } generatedCode.AppendLine(" };"); generatedCode.AppendLine(string.Empty); generatedCode.AppendLine(" return result.ToArray();"); generatedCode.AppendLine(" }"); }
/// <summary> /// Carries out the generation of code for a class /// </summary> /// <param name="sb">The string builder to append code to.</param> /// <param name="classGenerationParameters">The class generation parameters</param> protected override void DoGenerationOfClass(StringBuilder sb, IClassGenerationParameters classGenerationParameters) { if (classGenerationParameters == null) { throw new ArgumentNullException("classGenerationParameters"); } var mainNamespaceName = classGenerationParameters.MainNamespaceName; if (string.IsNullOrWhiteSpace(mainNamespaceName)) { throw new ArgumentException("MainNamespaceName", "classGenerationParameters"); } var className = classGenerationParameters.ClassName; if (string.IsNullOrWhiteSpace(className)) { throw new ArgumentException("ClassName", "classGenerationParameters"); } var companyName = classGenerationParameters.CompanyName; if (string.IsNullOrWhiteSpace(companyName)) { throw new ArgumentException("CompanyName", "classGenerationParameters"); } var copyrightBanner = classGenerationParameters.CopyrightBanner; if (copyrightBanner == null || copyrightBanner.Length < 1) { throw new ArgumentException("copyrightBanner", "classGenerationParameters"); } var copyrightStartYear = classGenerationParameters.CopyrightStartYear; if (copyrightStartYear < 1900) { throw new ArgumentException("CopyrightStartYear", "classGenerationParameters"); } var classRemarks = classGenerationParameters.ClassRemarks; if (string.IsNullOrWhiteSpace(classRemarks)) { throw new ArgumentException("ClassRemarks", "classGenerationParameters"); } var properties = classGenerationParameters.Properties; if (properties == null) { throw new ArgumentException("Properties", "classGenerationParameters"); } if (properties.Count(p => p.IsKey) > 1) { throw new ArgumentException("Too many primary keys defined"); } sb.AppendLine(string.Empty); sb.AppendLine(Helpers.GetAutoGeneratedWarning()); sb.AppendLine(" /// <summary>"); sb.AppendLine(" /// " + classRemarks); sb.AppendLine(" /// </summary>"); sb.AppendLine(" [System.Runtime.Serialization.DataContract]"); var nucleotideAssembly = Assembly.GetExecutingAssembly().GetName(); sb.AppendLine($" [System.CodeDom.Compiler.GeneratedCode(\"DHGMS Nucleotide\", \"{nucleotideAssembly.Version}\")]"); sb.AppendLine(" public class " + className + this.GetClassSuffix()); sb.AppendLine("// ReSharper disable RedundantNameQualifier"); var baseClassName = classGenerationParameters.BaseClassName; var baseClassProperties = classGenerationParameters.BaseClassProperties; if (string.IsNullOrWhiteSpace(baseClassName) == false && baseClassProperties != null && baseClassProperties.Length > 0) { sb.AppendLine(" : " + baseClassName + this.GetClassSuffix()); } else { sb.AppendLine( " : " + this.DefaultBaseNamespace + "<" + className + this.GetClassSuffix() + ">, I" + className + this.GetClassSuffix()); } sb.AppendLine("// ReSharper restore RedundantNameQualifier"); sb.AppendLine(" {"); this.DoFieldsRegion(sb, properties); this.DoConstructorMethod(sb, className, properties, baseClassName, baseClassProperties); this.DoPropertiesRegion(sb, properties, baseClassProperties); this.DoIComparableRegion(sb, className, properties, baseClassName, baseClassProperties); sb.Append(Helpers.GetIEquatableRegion(className + this.GetClassSuffix(), baseClassName, baseClassProperties)); this.DoOurMethodsRegion( sb, mainNamespaceName, classGenerationParameters.SubNamespace, className, properties, baseClassName, baseClassProperties); this.DoDisposeMethod(sb, properties, baseClassName); sb.AppendLine(" }"); }
/// <summary> /// The do get strongly typed object from data reader row method. /// </summary> /// <param name="generatedCode"> /// The String Builder to add the code to /// </param> /// <param name="classInfo"> /// The class Info. /// </param> /// <param name="doGetRowDataMethod"> /// The do Get Row Data Method. /// </param> /// <param name="classSuffix"> /// class suffix (used for difference class, etc.) /// </param> protected virtual void DoGetStronglyTypedObjectFromDataReaderRowMethod( StringBuilder generatedCode, IClassGenerationParameters classInfo, Action<StringBuilder, PropertyInfoBase[]> doGetRowDataMethod, string classSuffix) { if (generatedCode == null) { throw new ArgumentNullException("generatedCode"); } if (classInfo == null) { throw new ArgumentNullException("classInfo"); } if (doGetRowDataMethod == null) { throw new ArgumentNullException("doGetRowDataMethod"); } var fullyQualifiedClassName = classInfo.MainNamespaceName + ".Model." + (!string.IsNullOrWhiteSpace(classInfo.SubNamespace) ? classInfo.SubNamespace + "." : null) + classInfo.ClassName; var properties = classInfo.Properties; if (properties == null || properties.Length < 1) { throw new ArgumentException("classInfo.properties"); } var baseClassProperties = classInfo.BaseClassProperties; generatedCode.AppendLine(" /// <summary>"); generatedCode.AppendLine(" /// Get Strongly Typed Object from a data reader"); generatedCode.AppendLine(" /// </summary>"); generatedCode.AppendLine(" /// <param name=\"dataReader\">"); generatedCode.AppendLine(" /// The data Reader."); generatedCode.AppendLine(" /// </param>"); generatedCode.AppendLine(" /// <returns>"); generatedCode.AppendLine(" /// strongly typed object."); generatedCode.AppendLine(" /// </returns>"); generatedCode.AppendLine( " public override " + fullyQualifiedClassName + classSuffix + " GetStronglyTypedObjectFromDataReaderRow(System.Data.IDataReader dataReader)"); generatedCode.AppendLine(" {"); if (baseClassProperties != null && baseClassProperties.Length > 0 && baseClassProperties.Any(pi => pi.SqlDataReaderType == null || pi.Collection != CollectionType.None)) { generatedCode.AppendLine(" throw new NotImplementedException();"); generatedCode.AppendLine(" }"); return; } if (properties.Any(pi => pi.SqlDataReaderType == null || pi.Collection != CollectionType.None)) { generatedCode.AppendLine(" throw new NotImplementedException();"); generatedCode.AppendLine(" }"); return; } generatedCode.AppendLine(" if (dataReader == null)"); generatedCode.AppendLine(" {"); generatedCode.AppendLine(" throw new ArgumentNullException(\"dataReader\");"); generatedCode.AppendLine(" }"); generatedCode.AppendLine(string.Empty); generatedCode.AppendLine(" // get ordinals"); if (baseClassProperties != null && baseClassProperties.Length > 0) { DoGetOrdinals(generatedCode, baseClassProperties); } DoGetOrdinals(generatedCode, properties); generatedCode.AppendLine(string.Empty); generatedCode.AppendLine(" // get row data"); if (baseClassProperties != null && baseClassProperties.Length > 0) { doGetRowDataMethod(generatedCode, baseClassProperties); } doGetRowDataMethod(generatedCode, properties); generatedCode.AppendLine(" return new " + fullyQualifiedClassName + classSuffix + "("); if (baseClassProperties != null && baseClassProperties.Length > 0) { generatedCode.AppendLine( " " + Helpers.GetVariableName("row" + baseClassProperties[0].Name) + (baseClassProperties.Length > 1 ? "," : string.Empty)); for (int i = 1; i < baseClassProperties.Length; i++) { generatedCode.AppendLine( " " + Helpers.GetVariableName("row" + baseClassProperties[i].Name) + ","); } } for (int i = 0; i < properties.Length; i++) { generatedCode.AppendLine( " " + Helpers.GetVariableName("row" + properties[i].Name) + (i < properties.Length - 1 ? "," : ");")); } generatedCode.AppendLine(" }"); }
/// <summary> /// Carries out the generation of code for a class /// </summary> /// <param name="sb">The string builder to append code to.</param> /// <param name="classGenerationParameters">The class generation parameters</param> protected override void DoGenerationOfClass(StringBuilder sb, IClassGenerationParameters classGenerationParameters) { if (sb == null) { throw new ArgumentNullException(nameof(sb)); } if (classGenerationParameters == null) { throw new ArgumentNullException(nameof(classGenerationParameters)); } var fullyQualifiedClassName = classGenerationParameters.MainNamespaceName + ".Model." + (!string.IsNullOrWhiteSpace(classGenerationParameters.SubNamespace) ? classGenerationParameters.SubNamespace + "." : null) + classGenerationParameters.ClassName; sb.AppendLine(" using System;"); sb.AppendLine(string.Empty); sb.AppendLine(" /// <summary>"); sb.AppendLine(" /// Helper methods for using POCO and low level ADO.NET"); sb.AppendLine(" /// </summary>"); sb.AppendLine(" public class " + classGenerationParameters.ClassName + "AdoNetHelper"); sb.AppendLine(" : Dhgms.DataManager.Model.Helper.AdoNet.AdoNetBase<" + fullyQualifiedClassName + ">"); sb.AppendLine(" {"); sb.AppendLine(" #region our methods"); sb.AppendLine(string.Empty); DoGetDataColumnsMethod(sb, classGenerationParameters); this.DoGetStronglyTypedObjectFromDataReaderRowMethod(sb, classGenerationParameters, this.DoInformationClassGetRowData, null); sb.AppendLine(" #endregion"); sb.AppendLine(" }"); sb.AppendLine(string.Empty); }
/// <summary> /// Generates the code that maps the class to a database table t /// </summary> /// <param name="sb"> /// The string builder to add the code to /// </param> /// <param name="classInfo"> /// The information relating to the generation of the class /// </param> /// <param name="inheritingFullyQualifiedClass"> /// The fully qualified class name for the EF inheriting class /// </param> private static void DoEntityFrameworkModelMethod(StringBuilder sb, IClassGenerationParameters classInfo, string inheritingFullyQualifiedClass) { if (classInfo == null) { throw new ArgumentNullException("classInfo"); } if (string.IsNullOrWhiteSpace(classInfo.ClassName)) { throw new ArgumentException("ClassName"); } if (classInfo.Properties == null) { throw new ArgumentException("Properties"); } if (classInfo.Properties.Count(p => p.IsKey) > 1) { throw new ArgumentException("Too many primary keys defined"); } // currently not going to support list types in our EF mapping if ((classInfo.BaseClassProperties != null && classInfo.BaseClassProperties.Any(baseProperty => baseProperty.Collection != CollectionType.None)) || classInfo.Properties.Any(property => property.Collection != CollectionType.None)) { return; } string fullyQualifiedClassName = classInfo.MainNamespaceName + ".Model." + (!string.IsNullOrWhiteSpace(classInfo.SubNamespace) ? classInfo.SubNamespace + "." : null) + classInfo.ClassName; sb.AppendLine("/// <summary>"); sb.AppendLine("/// Maps the information class to the entity framework model"); sb.AppendLine("/// </summary>"); sb.AppendLine("/// <param name=\"modelBuilder\">"); sb.AppendLine("/// model builder object"); sb.AppendLine("/// </param>"); sb.AppendLine("/// <param name=\"schemaName\">"); sb.AppendLine("/// The schema Name in the database"); sb.AppendLine("/// </param>"); sb.AppendLine("/// <param name=\"tableName\">"); sb.AppendLine("/// The table Name in the database"); sb.AppendLine("/// </param>"); sb.AppendLine( "public void DoEntityFrameworkModel(DbModelBuilder modelBuilder, string schemaName, string tableName)"); sb.AppendLine("{"); sb.AppendLine(" if (modelBuilder == null)"); sb.AppendLine(" {"); sb.AppendLine(" throw new ArgumentNullException(\"modelBuilder\");"); sb.AppendLine(" }"); sb.AppendLine(string.Empty); sb.AppendLine(" if (tableName == null)"); sb.AppendLine(" {"); sb.AppendLine(" throw new ArgumentNullException(\"tableName\");"); sb.AppendLine(" }"); sb.AppendLine(string.Empty); foreach (PropertyInfoBase p in classInfo.Properties.Where(p => p.IsKey)) { sb.AppendLine( " modelBuilder.Entity<" + fullyQualifiedClassName + ">().HasKey(x => x." + p.Name + ");"); } foreach (PropertyInfoBase p in classInfo.Properties) { sb.Append( " modelBuilder.Entity<" + fullyQualifiedClassName + ">().Property(x => x." + p.Name + ").Is"); sb.AppendLine(p.Optional ? "Optional();" : "Required();"); } foreach ( PropertyInfoBase p in classInfo.Properties.Where(p => !string.IsNullOrWhiteSpace(p.AlternativeDatabaseColumnName))) { sb.AppendLine( " modelBuilder.Entity<" + fullyQualifiedClassName + ">().Property(x => x." + p.Name + ").HasColumnName(\"" + p.AlternativeDatabaseColumnName + "\");"); } if (string.IsNullOrWhiteSpace(inheritingFullyQualifiedClass)) { sb.AppendLine(" modelBuilder.Entity<" + fullyQualifiedClassName + ">().ToTable(tableName, schemaName);"); } else { sb.AppendLine(" modelBuilder.Entity<" + inheritingFullyQualifiedClass + ">().Map(m =>"); sb.AppendLine(" {"); sb.AppendLine(" m.MapInheritedProperties();"); sb.AppendLine(" m.ToTable(tableName, schemaName);"); sb.AppendLine(" });"); } sb.AppendLine("}"); sb.AppendLine(string.Empty); }
/// <summary> /// Carries out the generation of code for a class /// </summary> /// <param name="sb">The string builder to append code to.</param> /// <param name="classGenerationParameters">The class generation parameters</param> protected abstract void DoGenerationOfClass(StringBuilder sb, IClassGenerationParameters classGenerationParameters);