Exemplo n.º 1
0
        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 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);
            }
        }
Exemplo n.º 3
0
        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 ShouldGenerateProperty()
        {
            var codeGenerationHelper = new CodeGenerationHelper();
            CodeMemberProperty memberProperty = codeGenerationHelper.CreateProperty(typeof (string), "Name");
            CodeCompileUnit codeCompileUnit = codeGenerationHelper.GetCodeCompileUnit("someNamespace", "someType");
            codeCompileUnit.Namespaces[0].Types[0].Members.Add(memberProperty);
            cSharpCodeProvider.GenerateCodeFromCompileUnit(codeCompileUnit, new StringWriter(stringBuilder),
                                                           new CodeGeneratorOptions());

            var builderCompare = new StringBuilder();
            builderCompare.AppendLine("public virtual string Name {");
            builderCompare.AppendLine("            get {");
            builderCompare.AppendLine("                return this._name;");
            builderCompare.AppendLine("            }");
            builderCompare.AppendLine("            set {");
            builderCompare.AppendLine("                this._name = value;");
            builderCompare.AppendLine("            }");
            builderCompare.Append("        }");
            Assert.IsTrue(stringBuilder.ToString().Contains(builderCompare.ToString()));
        }
Exemplo n.º 5
0
        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;
        }