protected override void AddDocumentation(int start, ClassConstructorDefinition definition)
        {
            if (definition.Documentation == null)
            {
                return;
            }

            if (definition.Documentation.HasSummary)
            {
                Lines.Add(new CodeLine("{0}/// <summary>", Indent(start)));
                Lines.Add(new CodeLine("{0}/// {1}", Indent(start), definition.Documentation.Summary));
                Lines.Add(new CodeLine("{0}/// </summary>", Indent(start)));
            }

            foreach (var parameter in definition.Parameters)
            {
                if (parameter.Documentation == null)
                {
                    continue;
                }

                if (!string.IsNullOrEmpty(parameter.Documentation.Summary))
                {
                    Lines.Add(new CodeLine("{0}/// <param name=\"{1}\">{2}</param>", Indent(start), parameter.Name, parameter.Documentation.Summary));
                }
            }

            if (definition.Documentation.HasRemarks)
            {
                Lines.Add(new CodeLine("{0}/// <remarks>{1}</remarks>", Indent(start), definition.Documentation.Remarks));
            }
        }
Beispiel #2
0
        public static EntityClassDefinition GetEntityClassDefinition(this EntityFrameworkCoreProject project, ITable table)
        {
            var definition = new EntityClassDefinition
            {
                Namespaces =
                {
                    "System"
                },
                Namespace    = project.Database.HasDefaultSchema(table) ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(table.Schema),
                Name         = project.GetEntityName(table),
                IsPartial    = true,
                Constructors =
                {
                    new ClassConstructorDefinition()
                }
            };

            if (!string.IsNullOrEmpty(table.Description))
            {
                definition.Documentation.Summary = table.Description;
            }

            var projectSelection = project.GetSelection(table);

            if (projectSelection.Settings.UseDataAnnotations)
            {
                definition.Namespaces.Add("System.ComponentModel.DataAnnotations");
                definition.Namespaces.Add("System.ComponentModel.DataAnnotations.Schema");
            }

            if (projectSelection.Settings.EnableDataBindings)
            {
                definition.Namespaces.Add("System.ComponentModel");

                definition.Implements.Add("INotifyPropertyChanged");

                definition.Events.Add(new EventDefinition("PropertyChangedEventHandler", "PropertyChanged"));
            }

            if (table.PrimaryKey != null)
            {
                var constructor = new ClassConstructorDefinition();

                foreach (var key in table.GetColumnsFromConstraint(table.PrimaryKey))
                {
                    var propertyType = string.Empty;

                    if (project.Database.HasTypeMappedToClr(key))
                    {
                        var clrType = project.Database.GetClrMapForType(key);

                        propertyType = clrType.AllowClrNullable ? string.Format("{0}?", clrType.GetClrType().Name) : clrType.GetClrType().Name;
                    }
                    else
                    {
                        propertyType = "object";
                    }

                    constructor.Parameters.Add(new ParameterDefinition(propertyType, project.GetParameterName(key)));

                    constructor.Lines.Add(new CodeLine("{0} = {1};", key.GetPropertyName(), project.GetParameterName(key)));
                }

                definition.Constructors.Add(constructor);
            }

            var columns = table.Columns;

            foreach (var column in columns)
            {
                var propertyType = string.Empty;

                if (project.Database.HasTypeMappedToClr(column))
                {
                    var clrType = project.Database.GetClrMapForType(column);

                    propertyType = clrType.AllowClrNullable ? string.Format("{0}?", clrType.GetClrType().Name) : clrType.GetClrType().Name;
                }
                else
                {
                    propertyType = "object";
                }

                if (projectSelection.Settings.EnableDataBindings)
                {
                    definition.AddViewModelProperty(propertyType, column.HasSameNameEnclosingType(table) ? column.GetNameForEnclosing() : table.GetPropertyNameHack(column));
                }
                else
                {
                    if (projectSelection.Settings.BackingFields.Contains(table.GetFullColumnName(column)))
                    {
                        definition.AddPropertyWithField(propertyType, column.HasSameNameEnclosingType(table) ? column.GetNameForEnclosing() : table.GetPropertyNameHack(column));
                    }
                    else if (projectSelection.Settings.UseAutomaticPropertiesForEntities)
                    {
                        definition.Properties.Add(new PropertyDefinition(propertyType, column.HasSameNameEnclosingType(table) ? column.GetNameForEnclosing() : table.GetPropertyNameHack(column)));
                    }
                    else
                    {
                        definition.AddPropertyWithField(propertyType, column.HasSameNameEnclosingType(table) ? column.GetNameForEnclosing() : table.GetPropertyNameHack(column));
                    }
                }
            }

            if (projectSelection.Settings.AuditEntity == null)
            {
                definition.Implements.Add(projectSelection.Settings.EntityInterfaceName);
            }
            else
            {
                var count = 0;

                foreach (var column in columns)
                {
                    if (projectSelection.Settings.AuditEntity.Names.Contains(column.Name))
                    {
                        count += 1;
                    }
                }

                if (count == projectSelection.Settings.AuditEntity.Names.Length)
                {
                    definition.Implements.Add("IAuditEntity");
                }
                else
                {
                    definition.Implements.Add("IEntity");
                }
            }

            if (projectSelection.Settings.SimplifyDataTypes)
            {
                definition.SimplifyDataTypes();
            }

            if (projectSelection.Settings.DeclareNavigationProperties)
            {
                foreach (var foreignKey in table.ForeignKeys)
                {
                    var foreignTable = project.Database.FindTable(foreignKey.References);

                    if (foreignTable == null)
                    {
                        continue;
                    }

                    definition.Namespaces
                    .AddUnique(project.Database.HasDefaultSchema(foreignTable) ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(foreignTable.Schema));

                    definition.Namespace = project.Database.HasDefaultSchema(table) ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(table.Schema);

                    var fkProperty = foreignKey.GetParentNavigationProperty(foreignTable, project);

                    if (definition.Properties.FirstOrDefault(item => item.Name == fkProperty.Name) == null)
                    {
                        definition.Properties.Add(fkProperty);
                    }
                }

                foreach (var child in project.Database.Tables)
                {
                    foreach (var foreignKey in child.ForeignKeys)
                    {
                        if (foreignKey.References.EndsWith(table.FullName))
                        {
                            definition.Namespaces
                            .AddUnique(projectSelection.Settings.NavigationPropertyEnumerableNamespace);

                            definition.Namespaces
                            .AddUnique(project.Database.HasDefaultSchema(child) ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(child.Schema));

                            var navigationProperty = project.GetChildNavigationProperty(projectSelection, child, foreignKey);

                            if (definition.Properties.FirstOrDefault(item => item.Name == navigationProperty.Name) == null)
                            {
                                definition.Properties.Add(navigationProperty);
                            }
                        }
                    }
                }
            }

            return(definition);
        }
Beispiel #3
0
 protected abstract void AddDocumentation(int start, ClassConstructorDefinition definition);
        public static EntityClassDefinition GetEntityClassDefinition(this EntityFrameworkCoreProject project, ITable table)
        {
            var definition = new EntityClassDefinition
            {
                Namespaces =
                {
                    "System"
                },
                Namespace      = project.Database.HasDefaultSchema(table) ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(table.Schema),
                AccessModifier = AccessModifier.Public,
                Name           = project.GetEntityName(table),
                IsPartial      = true,
                Constructors   =
                {
                    new ClassConstructorDefinition(AccessModifier.Public)
                },
                DbObject = table
            };

            if (!string.IsNullOrEmpty(table.Description))
            {
                definition.Documentation.Summary = table.Description;
            }

            var projectSelection = project.GetSelection(table);

            if (projectSelection.Settings.UseDataAnnotations)
            {
                definition.Namespaces.Add("System.ComponentModel.DataAnnotations");
                definition.Namespaces.Add("System.ComponentModel.DataAnnotations.Schema");
            }

            if (projectSelection.Settings.EnableDataBindings)
            {
                definition.Namespaces.Add("System.ComponentModel");

                definition.Implements.Add("INotifyPropertyChanged");

                definition.Events.Add(new EventDefinition("PropertyChangedEventHandler", "PropertyChanged"));
            }

            if (table.PrimaryKey != null)
            {
                var constructor = new ClassConstructorDefinition
                {
                    AccessModifier = AccessModifier.Public
                };

                foreach (var key in table.GetColumnsFromConstraint(table.PrimaryKey))
                {
                    var col = (Column)key;

                    var propertyType = project.Database.ResolveDatabaseType(col);

                    constructor.Parameters.Add(new ParameterDefinition(propertyType, project.GetParameterName(col)));

                    constructor.Lines.Add(new CodeLine("{0} = {1};", project.GetPropertyName(key.Name), project.GetParameterName(col)));
                }

                definition.Constructors.Add(constructor);
            }

            var columns = table.Columns;

            foreach (var column in columns)
            {
                var propertyType = project.Database.ResolveDatabaseType(column);

                if (projectSelection.Settings.EnableDataBindings)
                {
                    definition.AddViewModelProperty(propertyType, project.GetPropertyName(table, column));
                }
                else
                {
                    if (projectSelection.Settings.BackingFields.Contains(table.GetFullColumnName(column)))
                    {
                        definition.AddPropertyWithField(propertyType, project.GetPropertyName(table, column));
                    }
                    else if (projectSelection.Settings.UseAutomaticPropertiesForEntities)
                    {
                        definition.Properties.Add(new PropertyDefinition
                        {
                            AccessModifier = AccessModifier.Public,
                            Type           = propertyType,
                            Name           = project.GetPropertyName(table, column),
                            IsAutomatic    = true
                        });
                    }
                    else
                    {
                        definition.AddPropertyWithField(propertyType, project.GetPropertyName(table, column));
                    }
                }
            }

            if (projectSelection.Settings.AuditEntity == null)
            {
                definition.Implements.Add(projectSelection.Settings.EntityInterfaceName);
            }
            else
            {
                var count = 0;

                foreach (var column in columns)
                {
                    if (projectSelection.Settings.AuditEntity.Names.Contains(column.Name))
                    {
                        count += 1;
                    }
                }

                if (count == projectSelection.Settings.AuditEntity.Names.Count())
                {
                    definition.Implements.Add("IAuditEntity");
                }
                else
                {
                    definition.Implements.Add("IEntity");
                }
            }

            if (projectSelection.Settings.SimplifyDataTypes)
            {
                definition.SimplifyDataTypes();
            }

            if (projectSelection.Settings.DeclareNavigationProperties)
            {
                foreach (var foreignKey in table.ForeignKeys)
                {
                    var foreignTable = project.Database.FindTable(foreignKey.References);

                    if (foreignTable == null)
                    {
                        continue;
                    }

                    if (definition.Namespace != project.GetEntityLayerNamespace(foreignTable.Schema))
                    {
                        definition.Namespaces
                        .AddUnique(project.Database.HasDefaultSchema(foreignTable) ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(foreignTable.Schema));
                    }

                    var fkProperty = foreignKey.GetParentNavigationProperty(foreignTable, project);

                    if (!definition.Properties.Any(item => item.Name == fkProperty.Name))
                    {
                        definition.Properties.Add(fkProperty);
                    }
                }

                foreach (var child in project.Database.Tables)
                {
                    foreach (var foreignKey in child.ForeignKeys)
                    {
                        if (foreignKey.References.EndsWith(table.FullName))
                        {
                            definition.Namespaces
                            .AddUnique(projectSelection.Settings.NavigationPropertyEnumerableNamespace);

                            if (definition.Namespace != project.GetEntityLayerNamespace(child.Schema))
                            {
                                definition.Namespaces
                                .AddUnique(project.Database.HasDefaultSchema(child) ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(child.Schema));
                            }

                            var navigationProperty = project.GetChildNavigationProperty(projectSelection, child, foreignKey);

                            if (!definition.Properties.Any(item => item.Name == navigationProperty.Name))
                            {
                                definition.Properties.Add(navigationProperty);
                            }
                        }
                    }
                }
            }

            return(definition);
        }