private void CreateFullProperties(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType) { foreach (var pk in Table.PrimaryKey.Columns) { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, Formatter.FormatText(pk.Name), true)); newType.Members.Add(codeGenerationHelper.CreateProperty(mapFromDbType, Formatter.FormatText(pk.Name), appPrefs.UseLazy)); } // Note that a foreign key referencing a primary within the same table will end up giving you a foreign key property with the same name as the table. foreach (var fk in Table.ForeignKeys.Where(fk => !string.IsNullOrEmpty(fk.References))) { newType.Members.Add(codeGenerationHelper.CreateField(appPrefs.ClassNamePrefix + Formatter.FormatSingular(fk.References), Formatter.FormatSingular(fk.UniquePropertyName))); newType.Members.Add(codeGenerationHelper.CreateProperty(appPrefs.ClassNamePrefix + Formatter.FormatSingular(fk.References), Formatter.FormatSingular(fk.UniquePropertyName), appPrefs.UseLazy)); } foreach (var column in Table.Columns.Where(x => x.IsPrimaryKey != true)) { if (!appPrefs.IncludeForeignKeys && column.IsForeignKey) { continue; } var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, Formatter.FormatText(column.Name), true, column.IsNullable)); newType.Members.Add(codeGenerationHelper.CreateProperty(mapFromDbType, Formatter.FormatText(column.Name), column.IsNullable, appPrefs.UseLazy)); } }
private void CreateFields(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType) { if (Table.PrimaryKey != null) { foreach (var pk in Table.PrimaryKey.Columns) { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, Formatter.FormatText(pk.Name), true)); } } if (appPrefs.IncludeForeignKeys) { var pascalCaseTextFormatter = new PascalCaseTextFormatter(); // Note that a foreign key referencing a primary within the same table will end up giving you a foreign key property with the same name as the table. foreach (var fk in Table.ForeignKeys.Where(fk => !string.IsNullOrEmpty(fk.References))) { var fieldName = Formatter.FormatSingular(fk.UniquePropertyName); var typeName = appPrefs.ClassNamePrefix + pascalCaseTextFormatter.FormatSingular(fk.References); newType.Members.Add(codeGenerationHelper.CreateField(typeName, fieldName)); } } foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); var fieldName = Formatter.FormatText(column.Name); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, fieldName, column.IsNullable)); } }
private void CreateFullProperties(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType) { var camelCaseFormatter = new CamelCaseTextFormatter(); if (Table.PrimaryKey != null) { foreach (var pk in Table.PrimaryKey.Columns) { if (pk.IsForeignKey && appPrefs.IncludeForeignKeys) { newType.Members.Add(codeGenerationHelper.CreateField(Formatter.FormatSingular(pk.ForeignKeyTableName), "_" + camelCaseFormatter.FormatSingular(pk.ForeignKeyTableName))); newType.Members.Add(codeGenerationHelper.CreateProperty(Formatter.FormatSingular(pk.ForeignKeyTableName), Formatter.FormatSingular(pk.ForeignKeyTableName), appPrefs.UseLazy)); } else { var mapFromDbType = mapper.MapFromDBType(appPrefs.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); var pkAlsoFkQty = (from fk in Table.ForeignKeys.Where(fk => fk.UniquePropertyName == pk.Name) select fk).Count(); var fieldName = FixPropertyWithSameClassName(pk.Name, Table.Name); if (pkAlsoFkQty > 0) { fieldName = fieldName + "Id"; } newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, "_" + camelCaseFormatter.FormatText(fieldName), true)); newType.Members.Add(codeGenerationHelper.CreateProperty(mapFromDbType, Formatter.FormatText(fieldName), appPrefs.UseLazy)); } } } if (appPrefs.IncludeForeignKeys) { var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = appPrefs.FieldPrefixRemovalList }; // Note that a foreign key referencing a primary within the same table will end up giving you a foreign key property with the same name as the table. foreach (var fk in Table.Columns.Where(c => c.IsForeignKey && !c.IsPrimaryKey)) { var typeName = appPrefs.ClassNamePrefix + pascalCaseTextFormatter.FormatSingular(fk.ForeignKeyTableName); var propertyName = fk.ForeignKeyTableName; var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); newType.Members.Add(codeGenerationHelper.CreateField(typeName, string.Format("_{0}", camelCaseFormatter.FormatSingular(fieldName)))); newType.Members.Add(codeGenerationHelper.CreateProperty(typeName, Formatter.FormatSingular(fieldName), appPrefs.UseLazy)); } } foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { var mapFromDbType = mapper.MapFromDBType(appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); var fieldName = FixPropertyWithSameClassName(column.Name, Table.Name); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, "_" + camelCaseFormatter.FormatText(fieldName), column.IsNullable)); var property = codeGenerationHelper.CreateProperty(mapFromDbType, Formatter.FormatText(fieldName), column.IsNullable, appPrefs.UseLazy); AttachValidatorAttributes(ref property, column); newType.Members.Add(property); } }
public void ShouldGenerateField() { var codeGenerationHelper = new CodeGenerationHelper(); CodeMemberField codeMemberField = codeGenerationHelper.CreateField(typeof (string), "name"); CodeCompileUnit codeCompileUnit = codeGenerationHelper.GetCodeCompileUnit("someNamespace", "someType"); codeCompileUnit.Namespaces[0].Types[0].Members.Add(codeMemberField); cSharpCodeProvider.GenerateCodeFromCompileUnit(codeCompileUnit, new StringWriter(stringBuilder), new CodeGeneratorOptions()); Assert.IsTrue(stringBuilder.ToString().Contains("private string name;")); }
private void CreateFields(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType) { if (Table.PrimaryKey != null) { foreach (var pk in Table.PrimaryKey.Columns) { var mapFromDbType = mapper.MapFromDBType(appPrefs.ServerType, pk.DataType, pk.DataLength, pk.DataPrecision, pk.DataScale); var propertyName = Formatter.FormatText(pk.Name); var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); var pkAlsoFkQty = (from fk in Table.ForeignKeys.Where(fk => fk.UniquePropertyName == pk.Name) select fk).Count(); if (pkAlsoFkQty > 0) fieldName = fieldName + "Id"; newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, fieldName, true)); } } if (appPrefs.IncludeForeignKeys) { var pascalCaseTextFormatter = new PascalCaseTextFormatter(); // Note that a foreign key referencing a primary within the same table will end up giving you a foreign key property with the same name as the table. foreach (var fk in Table.ForeignKeys.Where(fk => !string.IsNullOrEmpty(fk.References))) { var propertyName = fk.UniquePropertyName; propertyName = Formatter.FormatSingular(propertyName); var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); var typeName = appPrefs.ClassNamePrefix + pascalCaseTextFormatter.FormatSingular(fk.References); newType.Members.Add(codeGenerationHelper.CreateField(typeName, fieldName)); } } foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys))) { var mapFromDbType = mapper.MapFromDBType(appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale); var propertyName = Formatter.FormatText(column.Name); var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, fieldName, column.IsNullable)); } }
public CodeCompileUnit GetCompileUnit(out CodeCompileUnit pkClass) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, tableName.GetFormattedText()); CodeCompileUnit pkCompileUnit = null; var mapper = new DataTypeMapper(); var newType = compileUnit.Namespaces[0].Types[0]; List<ColumnDetail> pkColumns = columnDetails.FindAll(col => col.IsPrimaryKey); List<ColumnDetail> normalColumns = columnDetails.FindAll(col => col.IsPrimaryKey == false); // data contract count int dataContractCount = 1; // create pk columns if(pkColumns.Count > 0) { if(pkColumns.Count == 1) { var columnDetail = pkColumns[0]; string propertyName = columnDetail.ColumnName.GetPreferenceFormattedText(applicationPreferences); Type mapFromDbType = mapper.MapFromDBType(columnDetail.DataType, columnDetail.DataLength, columnDetail.DataPrecision, columnDetail.DataScale); switch (applicationPreferences.FieldGenerationConvention) { case FieldGenerationConvention.Property: newType.Members.Add(codeGenerationHelper.CreateProperty(mapFromDbType, propertyName.MakeFirstCharUpperCase())); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, propertyName.MakeFirstCharLowerCase())); break; case FieldGenerationConvention.AutoProperty: propertyName = columnDetail.ColumnName.GetFormattedText(); var codeMemberProperty = codeGenerationHelper.CreateAutoProperty(mapFromDbType, propertyName); newType.Members.Add(codeMemberProperty); break; default: newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, propertyName)); break; } } else // composite key { string pkClassName = tableName.GetFormattedText() + "PK"; string pkRefProperty = pkClassName; pkCompileUnit = codeGenerationHelper.GetCodeCompileUnit(nameSpace, pkClassName); var newPKType = pkCompileUnit.Namespaces[0].Types[0]; // create composite member switch (applicationPreferences.FieldGenerationConvention) { case FieldGenerationConvention.Property: newType.Members.Add(codeGenerationHelper.CreateProperty(pkClassName, pkRefProperty.MakeFirstCharUpperCase())); newType.Members.Add(codeGenerationHelper.CreateField(pkClassName, pkRefProperty.MakeFirstCharLowerCase())); break; case FieldGenerationConvention.AutoProperty: //pkPropertyName = columnDetail.ColumnName.GetFormattedText(); var codeMemberProperty = codeGenerationHelper.CreateAutoProperty(pkClassName, pkRefProperty); newType.Members.Add(codeMemberProperty); break; default: newType.Members.Add(codeGenerationHelper.CreateField(pkClassName, pkRefProperty)); break; } // create pk columns foreach (var columnDetail in pkColumns) { string pkPropertyName = columnDetail.ColumnName.GetPreferenceFormattedText(applicationPreferences); Type pkMapFromDbType = mapper.MapFromDBType(columnDetail.DataType, columnDetail.DataLength, columnDetail.DataPrecision, columnDetail.DataScale); // get compile unit of compile class switch (applicationPreferences.FieldGenerationConvention) { case FieldGenerationConvention.Property: newPKType.Members.Add(codeGenerationHelper.CreateProperty(pkMapFromDbType, pkPropertyName.MakeFirstCharUpperCase())); newPKType.Members.Add(codeGenerationHelper.CreateField(pkMapFromDbType, pkPropertyName.MakeFirstCharLowerCase())); break; case FieldGenerationConvention.AutoProperty: pkPropertyName = columnDetail.ColumnName.GetFormattedText(); var codeMemberProperty = codeGenerationHelper.CreateAutoProperty(pkMapFromDbType, pkPropertyName); newPKType.Members.Add(codeMemberProperty); break; default: newPKType.Members.Add(codeGenerationHelper.CreateField(pkMapFromDbType, pkPropertyName)); break; } } newPKType.CustomAttributes.Add(new CodeAttributeDeclaration("Serializable")); newPKType.CustomAttributes.Add(new CodeAttributeDeclaration("Validate")); newPKType.CustomAttributes.Add(new CodeAttributeDeclaration("DataContract")); foreach (var member in newPKType.Members) { if (member is CodeMemberProperty) { CodeMemberProperty property = (CodeMemberProperty)member; CodeAttributeArgument[] arguments = new CodeAttributeArgument[] { new CodeAttributeArgument("Name",new CodePrimitiveExpression(dataContractCount.ToString())), new CodeAttributeArgument("Order",new CodePrimitiveExpression(dataContractCount++)) }; property.CustomAttributes.Add(new CodeAttributeDeclaration("DataMember", arguments)); } } } } // create normal columns foreach (var columnDetail in normalColumns) { string propertyName = columnDetail.ColumnName.GetPreferenceFormattedText(applicationPreferences); Type mapFromDbType = mapper.MapFromDBType(columnDetail.DataType, columnDetail.DataLength, columnDetail.DataPrecision, columnDetail.DataScale); if(FiendInTableReference(columnDetail)) continue; switch (applicationPreferences.FieldGenerationConvention) { case FieldGenerationConvention.Property: newType.Members.Add(codeGenerationHelper.CreateProperty(mapFromDbType, propertyName.MakeFirstCharUpperCase())); newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, propertyName.MakeFirstCharLowerCase())); break; case FieldGenerationConvention.AutoProperty: propertyName = columnDetail.ColumnName.GetFormattedText(); var codeMemberProperty = codeGenerationHelper.CreateAutoProperty(mapFromDbType, propertyName); newType.Members.Add(codeMemberProperty); break; default: newType.Members.Add(codeGenerationHelper.CreateField(mapFromDbType, propertyName)); break; } } // create detail member if exist if(tableReferences != null && tableReferences.Count > 0) { foreach (KeyValuePair<string, TableReference> pair in tableReferences) { TableReference reference = pair.Value; string refTable = GlobalCache.Instance.ReplaceShortWords(reference.ReferenceTable); switch(reference.ReferenceType) { case ReferenceType.OneToMany: refTable = refTable.GetFormattedText(); var detailListMemberProperty = codeGenerationHelper.CreateAutoProperty("IList<" + refTable + ">", refTable + "s"); newType.Members.Add(detailListMemberProperty); break; case ReferenceType.ManyToOne: refTable = refTable.GetFormattedText(); var masterMemberProperty = codeGenerationHelper.CreateAutoProperty(refTable, refTable); newType.Members.Add(masterMemberProperty); break; } } } var constructor = new CodeConstructor {Attributes = MemberAttributes.Public}; newType.Members.Add(constructor); newType.CustomAttributes.Add(new CodeAttributeDeclaration("Serializable")); newType.CustomAttributes.Add(new CodeAttributeDeclaration("Validate")); newType.CustomAttributes.Add(new CodeAttributeDeclaration("DataContract")); foreach (var member in newType.Members) { if(member is CodeMemberProperty) { CodeMemberProperty property = (CodeMemberProperty) member; CodeAttributeArgument[] arguments = new CodeAttributeArgument[] { new CodeAttributeArgument("Name",new CodePrimitiveExpression(dataContractCount.ToString())), new CodeAttributeArgument("Order",new CodePrimitiveExpression(dataContractCount++)) }; property.CustomAttributes.Add(new CodeAttributeDeclaration("DataMember",arguments)); } } pkClass = pkCompileUnit; return compileUnit; }