private static ClassConstructorDefinition GetConstructor(ProjectFeature <AspNetCoreProjectSettings> projectFeature)
        {
            var parameters = new List <ParameterDefinition>
            {
                new ParameterDefinition(projectFeature.GetInterfaceRepositoryName(), "repository")
            };

            var lines = new List <ILine>
            {
                new CodeLine("Repository = repository;")
            };

            var settings = projectFeature.GetAspNetCoreProject().GlobalSelection().Settings;

            if (settings.UseLogger)
            {
                parameters.Add(new ParameterDefinition(string.Format("ILogger<{0}>", projectFeature.GetControllerName()), "logger"));

                lines.Add(new CodeLine("Logger = logger;"));
            }

            return(new ClassConstructorDefinition(AccessModifier.Public, parameters.ToArray())
            {
                Lines = lines
            });
        }
        public static CSharpClassDefinition GetRepositoryClassDefinition(this ProjectFeature <DapperProjectSettings> projectFeature)
        {
            var classDefinition = new CSharpClassDefinition();

            classDefinition.Namespaces.Add("System");
            classDefinition.Namespaces.Add("System.Collections.Generic");
            classDefinition.Namespaces.Add("System.Data");
            classDefinition.Namespaces.Add("System.Data.SqlClient");
            classDefinition.Namespaces.Add("System.Linq");
            classDefinition.Namespaces.Add("System.Text");
            classDefinition.Namespaces.Add("System.Threading.Tasks");
            classDefinition.Namespaces.Add("Dapper");
            classDefinition.Namespaces.Add("Microsoft.Extensions.Options");

            foreach (var table in projectFeature.Project.Database.Tables)
            {
                if (table.HasDefaultSchema())
                {
                    classDefinition.Namespaces.AddUnique(projectFeature.GetDapperProject().GetEntityLayerNamespace());
                }
                else
                {
                    classDefinition.Namespaces.AddUnique(projectFeature.GetDapperProject().GetEntityLayerNamespace(table.Schema));
                }

                classDefinition.Namespaces.AddUnique(projectFeature.GetDapperProject().GetDataLayerContractsNamespace());
            }

            classDefinition.Namespace = projectFeature.GetDapperProject().GetDataLayerRepositoriesNamespace();

            classDefinition.Name = projectFeature.GetClassRepositoryName();

            classDefinition.BaseClass = "Repository";

            classDefinition.Implements.Add(projectFeature.GetInterfaceRepositoryName());

            classDefinition.Constructors.Add(new ClassConstructorDefinition(new ParameterDefinition("IOptions<AppSettings>", "appSettings"))
            {
                Invocation = "base(appSettings)"
            });

            var dbos   = projectFeature.DbObjects.Select(dbo => dbo.FullName).ToList();
            var tables = projectFeature.Project.Database.Tables.Where(t => dbos.Contains(t.FullName)).ToList();
            var views  = projectFeature.Project.Database.Views.Where(v => dbos.Contains(v.FullName)).ToList();

            foreach (var table in tables)
            {
                classDefinition.Methods.Add(GetGetAllMethod(projectFeature, table));

                if (table.PrimaryKey != null)
                {
                    classDefinition.Methods.Add(GetGetMethod(projectFeature, table));
                }

                classDefinition.Methods.Add(GetAddMethod(projectFeature, table));

                if (table.PrimaryKey != null)
                {
                    classDefinition.Methods.Add(GetUpdateMethod(projectFeature, table));
                    classDefinition.Methods.Add(GetRemoveMethod(projectFeature, table));
                }

                foreach (var unique in table.Uniques)
                {
                    classDefinition.Methods.Add(GetByUniqueMethod(projectFeature, table, unique));
                }
            }

            foreach (var view in views)
            {
                classDefinition.Methods.Add(GetGetAllMethod(projectFeature, view));
            }

            return(classDefinition);
        }
        public static ControllerClassDefinition GetControllerClassDefinition(this ProjectFeature <AspNetCoreProjectSettings> projectFeature)
        {
            var aspNetCoreProject = projectFeature.GetAspNetCoreProject();

            var definition = new ControllerClassDefinition
            {
                Namespaces =
                {
                    "System",
                    "System.Linq",
                    "System.Threading.Tasks",
                    "Microsoft.AspNetCore.Mvc",
                    "Microsoft.EntityFrameworkCore",
                    "Microsoft.Extensions.Logging",
                    aspNetCoreProject.GetDataLayerContractsNamespace(),
                    aspNetCoreProject.GetDataLayerRepositoriesNamespace(),
                    aspNetCoreProject.GetResponsesNamespace(),
                    aspNetCoreProject.GetRequestsNamespace()
                },
                Namespace      = string.Format("{0}.{1}", aspNetCoreProject.Name, "Controllers"),
                AccessModifier = AccessModifier.Public,
                Name           = projectFeature.GetControllerName(),
                Attributes     = new List <MetadataAttribute>
                {
                    new MetadataAttribute("Route", string.IsNullOrEmpty(aspNetCoreProject.Version) ? "\"api/[controller]\"" : string.Format("\"api/{0}/[controller]\"", aspNetCoreProject.Version)),
                    new MetadataAttribute("ApiController")
                },
                BaseClass = "ControllerBase",
                Fields    =
                {
                    new FieldDefinition(AccessModifier.Protected, projectFeature.GetInterfaceRepositoryName(), "Repository")
                    {
                        IsReadOnly = true
                    }
                }
            };

            var settings = aspNetCoreProject.GlobalSelection().Settings;

            if (settings.UseLogger)
            {
                definition.Fields.Add(new FieldDefinition(AccessModifier.Protected, "ILogger", "Logger"));
            }

            definition.Constructors.Add(GetConstructor(projectFeature));

            var dbos   = projectFeature.DbObjects.Select(dbo => dbo.FullName).ToList();
            var tables = projectFeature.Project.Database.Tables.Where(t => dbos.Contains(t.FullName)).ToList();

            foreach (var table in tables)
            {
                if (table.Columns.Count == table.PrimaryKey?.Key.Count)
                {
                    continue;
                }

                definition.Methods.Add(GetGetAllMethod(projectFeature, definition, table));

                if (table.PrimaryKey != null)
                {
                    definition.Methods.Add(GetGetMethod(projectFeature, table));
                }

                definition.Methods.Add(GetPostMethod(projectFeature, table));

                if (table.PrimaryKey != null)
                {
                    definition.Methods.Add(GetPutMethod(projectFeature, table));

                    definition.Methods.Add(GetDeleteMethod(projectFeature, table));
                }
            }

            // todo: Add views in controller

            definition.SimplifyDataTypes();

            return(definition);
        }
Exemple #4
0
        public static RepositoryClassDefinition GetRepositoryClassDefinition(this ProjectFeature <EntityFrameworkCoreProjectSettings> projectFeature)
        {
            var efCoreProject = projectFeature.GetEntityFrameworkCoreProject();

            var definition = new RepositoryClassDefinition
            {
                Namespaces =
                {
                    "System",
                    "System.Linq",
                    "System.Threading.Tasks",
                    "Microsoft.EntityFrameworkCore"
                },
                Namespace  = efCoreProject.GetDataLayerRepositoriesNamespace(),
                Name       = projectFeature.GetClassRepositoryName(),
                BaseClass  = "Repository",
                Implements =
                {
                    projectFeature.GetInterfaceRepositoryName()
                },
                Constructors =
                {
                    new ClassConstructorDefinition(new ParameterDefinition(efCoreProject.GetDbContextName(efCoreProject.Database), "dbContext"))
                    {
                        Invocation = "base(dbContext)"
                    }
                }
            };

            foreach (var table in efCoreProject.Database.Tables)
            {
                definition.Namespaces
                .AddUnique(projectFeature.Project.Database.HasDefaultSchema(table) ? efCoreProject.GetEntityLayerNamespace() : efCoreProject.GetEntityLayerNamespace(table.Schema));

                definition.Namespaces.AddUnique(efCoreProject.GetDataLayerContractsNamespace());
            }

            var tables = projectFeature
                         .Project
                         .Database
                         .Tables
                         .Where(item => projectFeature.DbObjects.Select(dbo => dbo.FullName).Contains(item.FullName))
                         .ToList();

            foreach (var table in tables)
            {
                var projectSelection = projectFeature.GetEntityFrameworkCoreProject().GetSelection(table);

                if (projectSelection.Settings.EntitiesWithDataContracts)
                {
                    definition.Namespaces.AddUnique(efCoreProject.GetDataLayerDataContractsNamespace());
                }

                foreach (var foreignKey in table.ForeignKeys)
                {
                    if (string.IsNullOrEmpty(foreignKey.Child))
                    {
                        var child = projectFeature.Project.Database.FindTable(foreignKey.Child);

                        if (child != null)
                        {
                            definition.Namespaces.AddUnique(efCoreProject.GetDataLayerDataContractsNamespace());
                        }
                    }
                }

                if (table.ForeignKeys.Count == 0)
                {
                    definition.GetGetAllMethodWithoutForeigns(projectFeature, projectSelection, table);
                }
                else
                {
                    definition.GetGetAllMethod(projectFeature, projectSelection, table);
                }

                if (table.PrimaryKey != null)
                {
                    definition.Methods.Add(GetGetMethod(projectFeature, projectSelection, table));
                }

                foreach (var unique in table.Uniques)
                {
                    definition.Methods.Add(GetGetByUniqueMethods(projectFeature, table, unique));
                }

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

            var views = projectFeature
                        .Project
                        .Database
                        .Views
                        .Where(item => projectFeature.DbObjects.Select(dbo => dbo.FullName).Contains(item.FullName))
                        .ToList();

            foreach (var view in views)
            {
                var projectSelection = projectFeature.GetEntityFrameworkCoreProject().GetSelection(view);

                if (projectSelection.Settings.EntitiesWithDataContracts)
                {
                    definition.Namespaces.AddUnique(efCoreProject.GetDataLayerDataContractsNamespace());
                }

                definition.GetGetAllMethod(projectFeature, view);

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

            return(definition);
        }
Exemple #5
0
        public static RepositoryClassDefinition GetRepositoryClassDefinition(this ProjectFeature <EntityFrameworkCoreProjectSettings> projectFeature)
        {
            var project = projectFeature.GetEntityFrameworkCoreProject();

            var definition = new RepositoryClassDefinition
            {
                Namespaces =
                {
                    "System",
                    "System.Collections.Generic",
                    "System.Data.SqlClient",
                    "System.Linq",
                    "System.Threading.Tasks",
                    "Microsoft.EntityFrameworkCore"
                },
                Namespace      = project.GetDataLayerRepositoriesNamespace(),
                AccessModifier = AccessModifier.Public,
                Name           = projectFeature.GetClassRepositoryName(),
                BaseClass      = "Repository",
                Implements     =
                {
                    projectFeature.GetInterfaceRepositoryName()
                },
                Constructors =
                {
                    new ClassConstructorDefinition(AccessModifier.Public, new ParameterDefinition(project.GetDbContextName(project.Database), "dbContext"))
                    {
                        Invocation = "base(dbContext)"
                    }
                }
            };

            foreach (var table in project.Database.Tables)
            {
                definition.Namespaces
                .AddUnique(projectFeature.Project.Database.HasDefaultSchema(table) ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(table.Schema));

                definition.Namespaces.AddUnique(project.GetDataLayerContractsNamespace());
            }

            foreach (var table in projectFeature.GetTables())
            {
                var projectSelection = projectFeature.GetEntityFrameworkCoreProject().GetSelection(table);

                if (projectSelection.Settings.EntitiesWithDataContracts)
                {
                    definition.Namespaces.AddUnique(project.GetDataLayerDataContractsNamespace());
                }

                foreach (var foreignKey in table.ForeignKeys)
                {
                    if (string.IsNullOrEmpty(foreignKey.Child))
                    {
                        var child = projectFeature.Project.Database.FindTable(foreignKey.Child);

                        if (child != null)
                        {
                            definition.Namespaces.AddUnique(project.GetDataLayerDataContractsNamespace());
                        }
                    }
                }

                if (table.ForeignKeys.Count == 0)
                {
                    definition.Methods.Add(GetGetAllMethodWithoutForeigns(projectFeature, projectSelection, table));
                }
                else
                {
                    definition.GetGetAllMethod(projectFeature, projectSelection, table);
                }

                if (table.PrimaryKey != null)
                {
                    definition.Methods.Add(GetGetMethod(projectFeature, projectSelection, table));
                }

                foreach (var unique in table.Uniques)
                {
                    definition.Methods.Add(GetGetByUniqueMethods(projectFeature, table, unique));
                }

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

            foreach (var view in projectFeature.GetViews())
            {
                var projectSelection = projectFeature.GetEntityFrameworkCoreProject().GetSelection(view);

                if (projectSelection.Settings.EntitiesWithDataContracts)
                {
                    definition.Namespaces.AddUnique(project.GetDataLayerDataContractsNamespace());
                }

                definition.Methods.Add(GetGetAllMethod(projectFeature, view));

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

            foreach (var tableFunction in projectFeature.GetTableFunctions())
            {
                var projectSelection = projectFeature.GetEntityFrameworkCoreProject().GetSelection(tableFunction);

                definition.Methods.Add(GetGetAllMethod(projectFeature, tableFunction));

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

            foreach (var storedProcedure in projectFeature.GetStoredProcedures())
            {
                var projectSelection = projectFeature.GetEntityFrameworkCoreProject().GetSelection(storedProcedure);

                definition.Methods.Add(GetGetAllMethod(projectFeature, storedProcedure));

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

            return(definition);
        }
Exemple #6
0
        public static CSharpClassDefinition GetRepositoryClassDefinition(this ProjectFeature projectFeature)
        {
            var entityFrameworkCoreProject = projectFeature.GetEntityFrameworkCoreProject();

            var classDefinition = new CSharpClassDefinition();

            classDefinition.Namespaces.Add("System");
            classDefinition.Namespaces.Add("System.Linq");
            classDefinition.Namespaces.Add("System.Threading.Tasks");
            classDefinition.Namespaces.Add("Microsoft.EntityFrameworkCore");

            foreach (var table in entityFrameworkCoreProject.Database.Tables)
            {
                classDefinition.Namespaces.AddUnique(table.HasDefaultSchema() ? entityFrameworkCoreProject.GetEntityLayerNamespace() : entityFrameworkCoreProject.GetEntityLayerNamespace(table.Schema));

                classDefinition.Namespaces.AddUnique(entityFrameworkCoreProject.GetDataLayerContractsNamespace());
            }

            classDefinition.Namespace = entityFrameworkCoreProject.GetDataLayerRepositoriesNamespace();

            classDefinition.Name = projectFeature.GetClassRepositoryName();

            classDefinition.BaseClass = "Repository";

            classDefinition.Implements.Add(projectFeature.GetInterfaceRepositoryName());

            classDefinition.Constructors.Add(new ClassConstructorDefinition(new ParameterDefinition(projectFeature.Project.Database.GetDbContextName(), "dbContext"))
            {
                Invocation = "base(dbContext)"
            });

            var tables = projectFeature
                         .Project
                         .Database
                         .Tables
                         .Where(item => projectFeature.DbObjects.Select(dbo => dbo.FullName).Contains(item.FullName))
                         .ToList();

            foreach (var table in tables)
            {
                if (entityFrameworkCoreProject.Settings.EntitiesWithDataContracts.Contains(table.FullName))
                {
                    classDefinition.Namespaces.AddUnique(entityFrameworkCoreProject.GetDataLayerDataContractsNamespace());
                }

                foreach (var foreignKey in table.ForeignKeys)
                {
                    if (string.IsNullOrEmpty(foreignKey.Child))
                    {
                        var child = projectFeature.Project.Database.FindTableBySchemaAndName(foreignKey.Child);

                        if (child != null)
                        {
                            classDefinition.Namespaces.AddUnique(entityFrameworkCoreProject.GetDataLayerDataContractsNamespace());
                        }
                    }
                }

                classDefinition.GetGetAllMethod(projectFeature, table);

                if (table.PrimaryKey != null)
                {
                    classDefinition.Methods.Add(GetGetMethod(projectFeature, table));
                }

                foreach (var unique in table.Uniques)
                {
                    classDefinition.Methods.Add(GetGetByUniqueMethods(projectFeature, table, unique));
                }

                classDefinition.Methods.Add(GetAddMethod(projectFeature, table));

                if (table.PrimaryKey != null)
                {
                    classDefinition.Methods.Add(GetUpdateMethod(projectFeature, table));
                    classDefinition.Methods.Add(GetRemoveMethod(projectFeature, table));
                }
            }



            var views = projectFeature
                        .Project
                        .Database
                        .Views
                        .Where(item => projectFeature.DbObjects.Select(dbo => dbo.FullName).Contains(item.FullName))
                        .ToList();

            foreach (var view in views)
            {
                if (entityFrameworkCoreProject.Settings.EntitiesWithDataContracts.Contains(view.FullName))
                {
                    classDefinition.Namespaces.AddUnique(entityFrameworkCoreProject.GetDataLayerDataContractsNamespace());
                }

                classDefinition.GetGetAllMethod(projectFeature, view);
            }

            return(classDefinition);
        }
Exemple #7
0
        public static CSharpClassDefinition GetControllerClassDefinition(this ProjectFeature <AspNetCoreProjectSettings> projectFeature)
        {
            var definition = new CSharpClassDefinition();

            definition.Namespaces.Add("System");
            definition.Namespaces.Add("System.Linq");
            definition.Namespaces.Add("System.Threading.Tasks");
            definition.Namespaces.Add("Microsoft.AspNetCore.Mvc");
            definition.Namespaces.Add("Microsoft.EntityFrameworkCore");
            definition.Namespaces.Add("Microsoft.Extensions.Logging");

            var project = projectFeature.GetAspNetCoreProject();

            definition.Namespaces.Add(project.GetDataLayerContractsNamespace());
            definition.Namespaces.Add(project.GetDataLayerRepositoriesNamespace());

            var settings = project.GlobalSelection().Settings;

            definition.Namespaces.Add(project.GetResponsesNamespace());
            definition.Namespaces.Add(project.GetRequestModelsNamespace());

            definition.Namespace = string.Format("{0}.{1}", project.Name, "Controllers");

            definition.Attributes = new List <MetadataAttribute>
            {
                new MetadataAttribute("Route", "\"api/[controller]\"")
            };

            definition.Name = projectFeature.GetControllerName();

            definition.BaseClass = "Controller";

            definition.Fields.Add(new FieldDefinition(AccessModifier.Protected, projectFeature.GetInterfaceRepositoryName(), "Repository")
            {
                IsReadOnly = true
            });

            if (settings.UseLogger)
            {
                definition.Fields.Add(new FieldDefinition(AccessModifier.Protected, "ILogger", "Logger"));
            }

            definition.Constructors.Add(GetConstructor(projectFeature));

            definition.Methods.Add(new MethodDefinition(AccessModifier.Protected, "void", "Dispose", new ParameterDefinition("Boolean", "disposing"))
            {
                IsOverride = true,
                Lines      = new List <ILine>
                {
                    new CodeLine("Repository?.Dispose();"),
                    new CodeLine(),
                    new CodeLine("base.Dispose(disposing);")
                }
            });

            var dbos   = projectFeature.DbObjects.Select(dbo => dbo.FullName).ToList();
            var tables = projectFeature.Project.Database.Tables.Where(t => dbos.Contains(t.FullName)).ToList();

            foreach (var table in tables)
            {
                definition.Methods.Add(GetGetAllMethod(projectFeature, definition, table));

                if (table.PrimaryKey != null)
                {
                    definition.Methods.Add(GetGetMethod(projectFeature, table));
                }

                definition.Methods.Add(GetPostMethod(projectFeature, table));

                if (table.PrimaryKey != null)
                {
                    definition.Methods.Add(GetPutMethod(projectFeature, table));

                    definition.Methods.Add(GetDeleteMethod(projectFeature, table));
                }
            }

            return(definition);
        }