public void TestCSharpClassWithIndexerGeneration()
        {
            // Arrange
            var definition = new CSharpClassDefinition
            {
                Namespaces = new List <string>
                {
                    "System",
                    "System.Collections.Generic"
                },
                Namespace = "Mapping",
                Name      = "DataSet"
            };

            definition.Indexers.Add(new IndexerDefinition
            {
                AccessModifier = AccessModifier.Public,
                Type           = "string",
                Parameters     = new List <ParameterDefinition>
                {
                    new ParameterDefinition
                    {
                        Type = "int",
                        Name = "index"
                    }
                },
                GetBody = new List <ILine>
                {
                    new CodeLine("return m_columns[index];")
                },
                SetBody = new List <ILine>
                {
                    new CodeLine("m_columns = value;")
                }
            });

            definition.AddPropertyWithField("List<string>", "Columns");

            // Act
            CSharpCodeBuilder.CreateFiles("C:\\Temp\\CatFactory.DotNetCore", string.Empty, true, definition);
        }
Example #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 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);
        }
Example #4
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);
        }