Ejemplo n.º 1
0
        public void TestScaffoldingOrderClass()
        {
            // Arrange
            var definition = new CSharpClassDefinition
            {
                Namespaces =
                {
                    "System",
                    "System.ComponentModel.DataAnnotations",
                    "System.ComponentModel.DataAnnotations.Schema"
                },
                Namespace      = "DesignPatterns",
                AccessModifier = AccessModifier.Public,
                Name           = "Order"
            };

            definition.Attributes.Add(new MetadataAttribute("Table", "\"Orders\"")
            {
                Sets =
                {
                    new MetadataAttributeSet("Schema", "\"dbo\"")
                }
            });

            definition.Properties.Add(
                CSharpClassDefinition.CreateAutomaticProperty("Int32?", "OrderID", attributes: new[]
            {
                new MetadataAttribute("Key"),
                new MetadataAttribute("DatabaseGenerated", "DatabaseGeneratedOption.Identity")
            })
                );

            definition.Properties.Add(
                CSharpClassDefinition.CreateAutomaticProperty("DateTime?", "OrderDate", attributes: new[] { new MetadataAttribute("Column") })
                );

            definition.Properties.Add(
                CSharpClassDefinition.CreateAutomaticProperty("String", "CustomerID", attributes: new[]
            {
                new MetadataAttribute("Column"),
                new MetadataAttribute("StringLength", "5")
            })
                );

            definition.Properties.Add(
                CSharpClassDefinition.CreateAutomaticProperty("Int32?", "ShipperID", attributes: new[] { new MetadataAttribute("Column") })
                );

            definition.SimplifyDataTypes();

            // Act
            CSharpCodeBuilder.CreateFiles("C:\\Temp\\CatFactory.NetCore\\DesignPatterns", string.Empty, true, definition);
        }
Ejemplo n.º 2
0
        public static CSharpClassDefinition CreateView(this DapperProject project, IView view)
        {
            var definition = new CSharpClassDefinition();

            definition.Namespaces.Add("System");

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

            definition.Name = view.GetEntityName();

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

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

            var selection = project.GetSelection(view);

            foreach (var column in view.Columns)
            {
                if (selection.Settings.UseAutomaticPropertiesForEntities)
                {
                    definition.Properties.Add(new PropertyDefinition(project.Database.ResolveType(column), column.GetPropertyName()));
                }
                else
                {
                    definition.AddPropertyWithField(project.Database.ResolveType(column), column.GetPropertyName());
                }
            }

            definition.Implements.Add("IEntity");

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

            return(definition);
        }
        public void ScaffoldingEntityExtensionsClass()
        {
            // Arrange
            var definition = new CSharpClassDefinition
            {
                Namespaces =
                {
                    "System"
                },
                Namespace      = "DesignPatterns",
                AccessModifier = AccessModifier.Public,
                IsStatic       = true,
                Name           = "EntityExtensions"
            };

            definition.Methods.Add(new MethodDefinition
            {
                AccessModifier = AccessModifier.Public,
                IsStatic       = true,
                IsExtension    = true,
                Type           = "string",
                Name           = "ToJson",
                Parameters     =
                {
                    new ParameterDefinition("Order", "entity")
                },
                Lines =
                {
                    new CodeLine("return string.Empty;")
                }
            });

            definition.SimplifyDataTypes();

            // Act
            CSharpCodeBuilder.CreateFiles(@"C:\Temp\CatFactory.NetCore\DesignPatterns", string.Empty, true, definition);
        }
        public void TestPersonClassGeneration()
        {
            // Arrange
            var definition = new CSharpClassDefinition
            {
                Namespaces = new List <string>
                {
                    "System",
                    "System.ComponentModel"
                },
                Namespace = "ContactManager",
                Name      = "Person",
            };

            definition.Attributes.Add(new MetadataAttribute("Table", "\"Persons\"")
            {
                Sets = new List <MetadataAttributeSet>
                {
                    new MetadataAttributeSet("Schema", "\"PersonalInfo\"")
                }
            });

            definition.Properties.Add(new PropertyDefinition("Int32?", "ID", new MetadataAttribute("Key"))
            {
                IsVirtual = true
            });
            definition.Properties.Add(new PropertyDefinition("String", "FirstName", new MetadataAttribute("Required"), new MetadataAttribute("StringLength", "25")));
            definition.Properties.Add(new PropertyDefinition("String", "MiddleName", new MetadataAttribute("StringLength", "25")));
            definition.Properties.Add(new PropertyDefinition("String", "LastName", new MetadataAttribute("Required"), new MetadataAttribute("StringLength", "25")));
            definition.Properties.Add(new PropertyDefinition("String", "Gender", new MetadataAttribute("Required"), new MetadataAttribute("StringLength", "1")));
            definition.Properties.Add(new PropertyDefinition("DateTime?", "BirthDate", new MetadataAttribute("Required")));

            definition.SimplifyDataTypes();

            // Act
            CSharpCodeBuilder.CreateFiles("C:\\Temp\\CatFactory.DotNetCore", string.Empty, true, definition);
        }
        public static CSharpClassDefinition GetEntityClassDefinition(this EntityFrameworkCoreProject project, ITable table)
        {
            var classDefinition = new CSharpClassDefinition();

            classDefinition.Namespaces.Add("System");

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

            if (project.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.GetSingularName();
            classDefinition.IsPartial = true;

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

            var columns = table.Columns;

            if (table.PrimaryKey?.Key.Count == 1)
            {
                var column = table.PrimaryKey.GetColumns(table).First();

                classDefinition.Constructors.Add(new ClassConstructorDefinition(new ParameterDefinition(column.GetClrType(), 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 columns)
            {
                if (project.Settings.EnableDataBindings)
                {
                    classDefinition.AddViewModelProperty(column.GetClrType(), column.HasSameNameEnclosingType(table) ? column.GetNameForEnclosing() : column.GetPropertyName());
                }
                else
                {
                    if (project.Settings.BackingFields.Contains(table.GetFullColumnName(column)))
                    {
                        classDefinition.AddPropertyWithField(column.GetClrType(), column.HasSameNameEnclosingType(table) ? column.GetNameForEnclosing() : column.GetPropertyName());
                    }
                    else if (project.Settings.UseAutomaticPropertiesForEntities)
                    {
                        classDefinition.Properties.Add(new PropertyDefinition(column.GetClrType(), column.HasSameNameEnclosingType(table) ? column.GetNameForEnclosing() : column.GetPropertyName()));
                    }
                    else
                    {
                        classDefinition.AddPropertyWithField(column.GetClrType(), column.HasSameNameEnclosingType(table) ? column.GetNameForEnclosing() : column.GetPropertyName());
                    }
                }
            }

            if (project.Settings.AuditEntity == null)
            {
                classDefinition.Implements.Add("IEntity");
            }
            else
            {
                var count = 0;

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

                if (count == project.Settings.AuditEntity.Names.Length)
                {
                    classDefinition.Implements.Add(project.Settings.EntityInterfaceName);
                }
                else
                {
                    classDefinition.Implements.Add("IEntity");
                }
            }

            foreach (var foreignKey in table.ForeignKeys)
            {
                var foreignTable = project.Database.FindTableByFullName(foreignKey.References);

                if (foreignTable == null)
                {
                    continue;
                }

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

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

                classDefinition.Properties.Add(foreignKey.GetParentNavigationProperty(project, foreignTable));
            }

            foreach (var child in project.Database.Tables)
            {
                if (table.FullName == child.FullName)
                {
                    continue;
                }

                foreach (var foreignKey in child.ForeignKeys)
                {
                    if (foreignKey.References.EndsWith(table.FullName))
                    {
                        classDefinition.Namespaces.AddUnique(project.Settings.NavigationPropertyEnumerableNamespace);
                        classDefinition.Namespaces.AddUnique(child.HasDefaultSchema() ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(child.Schema));

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

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

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

            return(classDefinition);
        }
Ejemplo n.º 6
0
        public static CSharpClassDefinition CreateEntity(this DapperProject project, ITable table)
        {
            var classDefinition = new CSharpClassDefinition();

            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), column.GetPropertyName());
                }
                else
                {
                    if (selection.Settings.UseAutomaticPropertiesForEntities)
                    {
                        classDefinition.Properties.Add(new PropertyDefinition(project.Database.ResolveType(column), column.GetPropertyName()));
                    }
                    else
                    {
                        classDefinition.AddPropertyWithField(project.Database.ResolveType(column), column.GetPropertyName());
                    }
                }
            }

            classDefinition.Implements.Add("IEntity");

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

            return(classDefinition);
        }