Exemple #1
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.GetRepositoryClassName(),
                BaseClass      = "Repository",
                Implements     =
                {
                    projectFeature.GetRepositoryInterfaceName()
                },
                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))
                    {
                        continue;
                    }

                    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 project.Database.GetTableFunctions())
            {
                var projectSelection = projectFeature.GetEntityFrameworkCoreProject().GetSelection(tableFunction);

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

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

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

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

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

            return(definition);
        }