Exemple #1
0
        public static EntityClassDefinition CreateEntity(this DapperProject project, ITable table)
        {
            var classDefinition = new EntityClassDefinition();

            classDefinition.Namespaces.Add("System");

            var selection = project.GetSelection(table);

            if (selection.Settings.EnableDataBindings)
            {
                classDefinition.Namespaces.Add("System.ComponentModel");

                classDefinition.Implements.Add("INotifyPropertyChanged");

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

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

            classDefinition.Name = table.GetEntityName();

            classDefinition.Constructors.Add(new ClassConstructorDefinition());

            if (table.PrimaryKey != null && table.PrimaryKey.Key.Count == 1)
            {
                var column = table.GetColumnsFromConstraint(table.PrimaryKey).First();

                classDefinition.Constructors.Add(new ClassConstructorDefinition(new ParameterDefinition(project.Database.ResolveType(column), column.GetParameterName()))
                {
                    Lines = new List <ILine>
                    {
                        new CodeLine("{0} = {1};", column.GetPropertyName(), column.GetParameterName())
                    }
                });
            }

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

            foreach (var column in table.Columns)
            {
                if (selection.Settings.EnableDataBindings)
                {
                    classDefinition.AddViewModelProperty(project.Database.ResolveType(column), table.GetPropertyNameHack(column));
                }
                else
                {
                    if (selection.Settings.UseAutomaticPropertiesForEntities)
                    {
                        classDefinition.Properties.Add(new PropertyDefinition(project.Database.ResolveType(column), table.GetPropertyNameHack(column)));
                    }
                    else
                    {
                        classDefinition.AddPropertyWithField(project.Database.ResolveType(column), table.GetPropertyNameHack(column));
                    }
                }
            }

            classDefinition.Implements.Add("IEntity");

            if (selection.Settings.SimplifyDataTypes)
            {
                classDefinition.SimplifyDataTypes();
            }

            return(classDefinition);
        }
        public static EntityClassDefinition GetEntityClassDefinition(this DapperProject 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),
                Constructors   =
                {
                    new ClassConstructorDefinition
                    {
                        AccessModifier = AccessModifier.Public
                    }
                }
            };

            var selection = project.GetSelection(table);

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

                definition.Implements.Add("INotifyPropertyChanged");

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

            if (table.PrimaryKey != null && table.PrimaryKey.Key.Count == 1)
            {
                var column = (Column)table.GetColumnsFromConstraint(table.PrimaryKey).First();

                definition.Constructors.Add(new ClassConstructorDefinition
                {
                    AccessModifier = AccessModifier.Public,
                    Parameters     =
                    {
                        new ParameterDefinition(project.Database.ResolveDatabaseType(column), project.GetParameterName(column))
                    },
                    Lines =
                    {
                        new CodeLine("{0} = {1};", project.GetPropertyName(table, column), project.GetParameterName(column))
                    }
                });
            }

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

            foreach (var column in table.Columns)
            {
                var propertyType = project.Database.ResolveDatabaseType(column);
                var propertyName = project.GetPropertyName(table, column);

                if (selection.Settings.EnableDataBindings)
                {
                    definition.AddViewModelProperty(propertyType, propertyName);
                }
                else
                {
                    if (selection.Settings.UseAutomaticPropertiesForEntities)
                    {
                        definition.Properties.Add(new PropertyDefinition
                        {
                            AccessModifier = AccessModifier.Public,
                            Type           = propertyType,
                            Name           = propertyName,
                            IsAutomatic    = true
                        });
                    }
                    else
                    {
                        definition.AddPropertyWithField(propertyType, propertyName);
                    }
                }
            }

            definition.Implements.Add("IEntity");

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

            return(definition);
        }
Exemple #3
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);
        }
        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);
        }