Esempio n. 1
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,
                                                                         $"_{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 CodeCompileUnit GetCompileUnit(string className)
        {
            var codeGenerationHelper = new CodeGenerationHelper();
            var compileUnit          = codeGenerationHelper.GetCodeCompileUnit(nameSpace, className);

            var mapper  = new DataTypeMapper();
            var newType = compileUnit.Namespaces[0].Types[0];

            var nameArgument      = new CodeAttributeArgument("Name", new CodeSnippetExpression("\"" + className + "\" "));
            var nameSpaceArgument = new CodeAttributeArgument("Namespace", new CodeSnippetExpression("\"\""));

            newType.CustomAttributes = new CodeAttributeDeclarationCollection {
                new CodeAttributeDeclaration("DataContract", nameArgument, nameSpaceArgument)
            };

            foreach (var column in table.Columns)
            {
                if (column.IsPrimaryKey)
                {
                    if (appPrefs.IncludeHasMany)
                    {
                        foreach (var foreignKeyTable in table.HasManyRelationships)
                        {
                            var fkEntityName = appPrefs.ClassNamePrefix + foreignKeyTable.Reference.MakeSingular().GetPreferenceFormattedText(appPrefs);
                            newType.Members.Add(codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute("IList<" + fkEntityName + ">", foreignKeyTable.Reference.MakePlural().GetPreferenceFormattedText(appPrefs)));
                        }
                    }

                    var primaryKeyType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale);
                    newType.Members.Add(codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute(primaryKeyType.Name, "Id"));
                    continue;
                }
                if (column.IsForeignKey)
                {
                    var fKey               = column.ForeignKeyTableName;
                    var typeName           = appPrefs.ClassNamePrefix + fKey.MakeSingular().GetPreferenceFormattedText(appPrefs);
                    var codeMemberProperty = codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute(typeName, fKey.MakeSingular().GetPreferenceFormattedText(appPrefs));
                    newType.Members.Add(codeMemberProperty);
                    continue;
                }
                var propertyName  = column.Name.GetPreferenceFormattedText(appPrefs);
                var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale);

                newType.Members.Add(codeGenerationHelper.CreateAutoPropertyWithDataMemberAttribute(mapFromDbType.Name, propertyName));
            }
            return(compileUnit);
        }
Esempio n. 3
0
        private void CreateProperties(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType)
        {
            switch (appPrefs.FieldGenerationConvention)
            {
            case FieldGenerationConvention.Field:
                CreateFields(codeGenerationHelper, mapper, newType);
                break;

            case FieldGenerationConvention.Property:
                CreateFullProperties(codeGenerationHelper, mapper, newType);
                break;

            case FieldGenerationConvention.AutoProperty:
                CreateAutoProperties(codeGenerationHelper, mapper, newType);
                break;
            }
        }
Esempio n. 4
0
        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));
            }
        }
Esempio n. 5
0
        public CodeCompileUnit GetCompileUnit(string className)
        {
            var codeGenerationHelper = new CodeGenerationHelper();
            var compileUnit          = codeGenerationHelper.GetCodeCompileUnitWithInheritanceAndInterface(nameSpace, className, appPrefs.InheritenceAndInterfaces);

            var mapper  = new DataTypeMapper();
            var newType = compileUnit.Namespaces[0].Types[0];

            newType.IsPartial = appPrefs.GeneratePartialClasses;

            CreateProperties(codeGenerationHelper, mapper, newType);

            // Generate GetHashCode() and Equals() methods.
            if (Table.PrimaryKey != null && Table.PrimaryKey.Columns.Count != 0 && Table.PrimaryKey.Type == PrimaryKeyType.CompositeKey)
            {
                var pkColsList = new List <string>();
                foreach (var pkCol in Table.PrimaryKey.Columns)
                {
                    if (pkCol.IsForeignKey && appPrefs.IncludeForeignKeys)
                    {
                        pkColsList.Add(Formatter.FormatSingular(pkCol.ForeignKeyTableName) + "." + Formatter.FormatText(pkCol.ForeignKeyColumnName));
                    }
                    else
                    {
                        pkColsList.Add(Formatter.FormatText(pkCol.Name));
                    }
                }

                var equalsCode     = CreateCompositeKeyEqualsMethod(pkColsList);
                var getHashKeyCode = CreateCompositeKeyGetHashKeyMethod(pkColsList);

                equalsCode.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "NHibernate Composite Key Requirements"));
                newType.Members.Add(equalsCode);
                newType.Members.Add(getHashKeyCode);
                getHashKeyCode.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));
            }

            // Don't create a constructor if there are no relationships.
            if (Table.HasManyRelationships.Count == 0)
            {
                return(compileUnit);
            }

            var pascalCaseTextFormatter = new PascalCaseTextFormatter {
                PrefixRemovalList = appPrefs.FieldPrefixRemovalList
            };
            var constructorStatements = new CodeStatementCollection();

            if (appPrefs.IncludeHasMany)
            {
                foreach (var hasMany in Table.HasManyRelationships)
                {
                    if (appPrefs.Language == Language.CSharp)
                    {
                        newType.Members.Add(codeGenerationHelper.CreateAutoProperty(
                                                $"{appPrefs.ForeignEntityCollectionType}<{appPrefs.ClassNamePrefix}{pascalCaseTextFormatter.FormatSingular(hasMany.Reference)}>", Formatter.FormatPlural(hasMany.Reference), appPrefs.UseLazy));
                        constructorStatements.Add(new CodeSnippetStatement(string.Format(TABS + "{0} = new {1}<{2}{3}>();", Formatter.FormatPlural(hasMany.Reference), codeGenerationHelper.InstatiationObject(appPrefs.ForeignEntityCollectionType), appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference))));
                    }
                    else if (appPrefs.Language == Language.VB)
                    {
                        newType.Members.Add(codeGenerationHelper.CreateAutoProperty(
                                                $"{appPrefs.ForeignEntityCollectionType}(Of {appPrefs.ClassNamePrefix}{pascalCaseTextFormatter.FormatSingular(hasMany.Reference)})", Formatter.FormatPlural(hasMany.Reference), appPrefs.UseLazy));
                        constructorStatements.Add(new CodeSnippetStatement(string.Format(TABS + "{0} = New {1}(Of {2}{3})()", Formatter.FormatPlural(hasMany.Reference), codeGenerationHelper.InstatiationObject(appPrefs.ForeignEntityCollectionType), appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference))));
                    }
                }
            }

            var constructor = new CodeConstructor {
                Attributes = MemberAttributes.Public
            };

            constructor.Statements.AddRange(constructorStatements);
            newType.Members.Add(constructor);
            return(compileUnit);
        }
Esempio n. 6
0
        private void CreateAutoProperties(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType)
        {
            if (Table.PrimaryKey != null)
            {
                foreach (var pk in Table.PrimaryKey.Columns)
                {
                    if (pk.IsForeignKey && appPrefs.IncludeForeignKeys)
                    {
                        newType.Members.Add(codeGenerationHelper.CreateAutoProperty(Formatter.FormatSingular(pk.ForeignKeyTableName),
                                                                                    Formatter.FormatSingular(pk.ForeignKeyTableName),
                                                                                    appPrefs.UseLazy));
                    }
                    else
                    {
                        var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, pk.DataType, pk.DataLength,
                                                                 pk.DataPrecision, pk.DataScale);
                        var fieldName   = FixPropertyWithSameClassName(pk.Name, 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.CreateAutoProperty(mapFromDbType.ToString(),
                                                                                    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.
                string lastOne = null;
                foreach (var fk in Table.Columns.Where(c => c.IsForeignKey && !c.IsPrimaryKey))
                {
                    var typeName = appPrefs.ClassNamePrefix + pascalCaseTextFormatter.FormatSingular(fk.ForeignKeyTableName);
                    if (String.IsNullOrEmpty(typeName))
                    {
                        System.Diagnostics.Trace.WriteLine($"Skipping null ForeignKeyTableName for field {fk.Name}");
                        continue;
                    }
                    var propertyName = Formatter.FormatSingular(fk.ForeignKeyTableName);
                    var fieldName    = FixPropertyWithSameClassName(propertyName, Table.Name);
                    if (lastOne != fieldName)
                    {
                        newType.Members.Add(codeGenerationHelper.CreateAutoProperty(typeName, fieldName, appPrefs.UseLazy));
                    }
                    lastOne = 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 = FixPropertyWithSameClassName(column.Name, Table.Name);
                var property  = codeGenerationHelper.CreateAutoProperty(mapFromDbType, Formatter.FormatText(fieldName), column.IsNullable, appPrefs.UseLazy);
                AttachValidatorAttributes(ref property, column);
                newType.Members.Add(property);
            }
        }