Exemple #1
0
        public void WriteWith(DatabaseTable foreignKeyChild)
        {
            var ffks = CodeWriterUtils.GetWithForeignKeys(table, foreignKeyChild).ToList();

            foreach (var ffk in ffks)
            {
                var ffkTable           = table.DatabaseSchema.FindTableByName(ffk.TableName);
                var ffkReferencedTable = ffk.ReferencedTable(table.DatabaseSchema);
                var ffkColumns         = ffk.Columns.Select(item => ffkTable.FindColumn(item));
                ffkColumns.OrderBy(item => item.Name);
                var ffkReferencedColumns = ffk.ReferencedColumns(table.DatabaseSchema).Select(item => ffkReferencedTable.FindColumn(item));

                var withMethodSignature = CodeWriterUtils.GetWithMethodSignature(
                    ffkReferencedTable,
                    ffkTable,
                    ffk,
                    codeWriterSettings);

                string propertyName = null;
                string repositoryMethodNameForFfkTable = null;
                if (table.IsSharedPrimaryKey(foreignKeyChild))
                {
                    propertyName = foreignKeyChild.Name;
                    repositoryMethodNameForFfkTable = CodeWriterUtils.GetGetMethodName(ffkColumns, codeWriterSettings, true);
                }
                else
                {
                    propertyName = codeWriterSettings.Namer.ForeignKeyCollectionName(ffkReferencedTable.Name, ffkTable, ffk);
                    repositoryMethodNameForFfkTable = CodeWriterUtils.GetGetMethodName(ffkColumns, codeWriterSettings, false);
                }

                classBuilder.BeginNest($"public virtual {withMethodSignature}");
                var repositoryMethodCallParametersForFfkTable = new List <string>();
                foreach (var ffkReferencedColumn in ffkReferencedColumns)
                {
                    var parameter = $"{CodeWriterUtils.GetPropertyNameForDatabaseColumn(ffkReferencedColumn)}";
                    if (ffkReferencedColumn.Nullable && CodeWriterUtils.FindDataType(ffkReferencedColumn).EndsWith("?"))
                    {
                        using (classBuilder.BeginNest($"if (!{parameter}.HasValue)"))
                        {
                            classBuilder.AppendLine($"{propertyName} = new List<{ffkTable.NetName}>();");
                            classBuilder.AppendLine("return this;");
                        }

                        classBuilder.AppendLine("");
                        parameter += ".Value";
                    }

                    repositoryMethodCallParametersForFfkTable.Add(parameter);
                }

                var repositoryMethodCallParametersForFfkTablePrinted = string.Join(", ", repositoryMethodCallParametersForFfkTable);
                var fieldNameForFfkTableRepository = NameFixer.ToCamelCase(CodeWriterUtils.GetRepositoryImplementationName(foreignKeyChild));
                classBuilder.AppendLine($"{propertyName} = _{fieldNameForFfkTableRepository}.{repositoryMethodNameForFfkTable}({repositoryMethodCallParametersForFfkTablePrinted});");
                classBuilder.AppendLine("return this;");
                classBuilder.EndNest();
                classBuilder.AppendLine("");
            }
        }
 public void Execute()
 {
     foreach (var t in schema.Tables)
     {
         table        = t;
         classBuilder = new ClassBuilder();
         var implementationText = Write();
         CodeWriterUtils.WriteClassFile(codeWriterSettings.OutputDirectory, CodeWriterUtils.GetRepositoryImplementationName(table), implementationText);
     }
 }
        private void WriteConstructor(IEnumerable <Parameter> fields)
        {
            var constructorSignature = string.Join(", ", fields.Select(f => $"{f.DataType} {f.Name}"));

            using (classBuilder.BeginNest($"public {CodeWriterUtils.GetRepositoryImplementationName(table)}({constructorSignature})"))
            {
                foreach (var f in fields)
                {
                    classBuilder.AppendLine($"this.{f.Name} = {f.Name};");
                }
            }
        }
Exemple #4
0
        public void WriteWith(DatabaseConstraint foreignKey)
        {
            // TODO: refactor this method to be consistent with approach taken for other overload
            var refTable = foreignKey.ReferencedTable(table.DatabaseSchema);
            var dataType = refTable.NetName;

            if (foreignKey.Columns.Count != foreignKey.ReferencedColumns(table.DatabaseSchema).Count())
            {
                throw new InvalidOperationException("Number of foreign key columns does not match number of columns referenced!");
            }

            classBuilder.BeginNest($"public virtual {CodeWriterUtils.GetWithMethodSignature(table, foreignKey, codeWriterSettings)}");

            var methodCallParameters = new List <string>();

            var propertyName = codeWriterSettings.Namer.ForeignKeyName(table, foreignKey);

            foreach (var fkc in foreignKey.Columns)
            {
                var tc        = table.Columns.Single(_tc => _tc.Name == fkc);
                var parameter = $"{CodeWriterUtils.GetPropertyNameForDatabaseColumn(tc)}";
                if (tc.Nullable && CodeWriterUtils.FindDataType(tc).EndsWith("?")) // KE: need the check for the "?" so that we correctly handle reference types like string
                {
                    using (classBuilder.BeginNest($"if (!{parameter}.HasValue)"))
                    {
                        classBuilder.AppendLine($"{propertyName} = null;");
                        classBuilder.AppendLine("return this;");
                    }

                    classBuilder.AppendLine("");
                    parameter += ".Value";
                }

                methodCallParameters.Add(parameter);
            }

            var s = string.Join(", ", methodCallParameters);
            var referencedColumnNames = foreignKey.ReferencedColumns(table.DatabaseSchema).ToList();

            referencedColumnNames.Sort();
            var referencedColumns             = referencedColumnNames.Select(c => foreignKey.ReferencedTable(table.DatabaseSchema).FindColumn(c));
            var methodParameters              = CodeWriterUtils.GetMethodParametersForColumns(referencedColumns, codeWriterSettings);
            var methodName                    = CodeWriterUtils.GetMethodName(methodParameters, codeWriterSettings, true, CodeWriterUtils.BaseMethodNameGet);
            var fieldNameForFkTableRepository = NameFixer.ToCamelCase(CodeWriterUtils.GetRepositoryImplementationName(refTable));

            classBuilder.AppendLine($"{propertyName} = _{fieldNameForFkTableRepository}.{methodName}({s});");
            classBuilder.AppendLine("return this;");
            classBuilder.EndNest();
            classBuilder.AppendLine("");
        }
Exemple #5
0
        private void WriteRegisterRepositories()
        {
            using (classBuilder.BeginNest($"public static IServiceCollection AddEnterpriseDataRepositories(this IServiceCollection services)"))
            {
                foreach (var t in schema.Tables)
                {
                    var interfaceName      = CodeWriterUtils.GetRepositoryInterfaceName(t);
                    var implementationName = CodeWriterUtils.GetRepositoryImplementationName(t);
                    classBuilder.AppendLine($"services.AddTransient<{interfaceName}, {implementationName}>();");
                }

                classBuilder.AppendLine("return services;");
            }

            classBuilder.AppendLine("");
        }
Exemple #6
0
        public static IEnumerable <Parameter> GetTablesAsParameters(IEnumerable <DatabaseTable> tables)
        {
            var fields = new List <Parameter>();

            foreach (var t in tables.Distinct().OrderBy(t => t.Name))
            {
                var field = new Parameter
                {
                    ColumnNameToQueryBy = null,
                    DataType            = CodeWriterUtils.GetRepositoryInterfaceName(t),
                    Name = NameFixer.ToCamelCase(CodeWriterUtils.GetRepositoryImplementationName(t))
                };

                fields.Add(field);
            }

            return(fields);
        }
        private string Write()
        {
            if (string.IsNullOrEmpty(table.NetName) && table.DatabaseSchema != null)
            {
                PrepareSchemaNames.Prepare(table.DatabaseSchema, codeWriterSettings.Namer);
            }

            CodeWriterUtils.WriteFileHeader(classBuilder);
            WriteUsings();
            CodeWriterUtils.BeginNestNamespace(classBuilder, codeWriterSettings);
            var tableOrView     = table is DatabaseView ? "view" : "table";
            var comment         = $"Repository class for the {table.Name} {tableOrView}";
            var classDefinition = $"public partial class {CodeWriterUtils.GetRepositoryImplementationName(table)} : {CodeWriterUtils.GetRepositoryInterfaceName(table)}";

            classBuilder.AppendXmlSummary(comment);
            classBuilder.BeginNest(classDefinition);
            WriteAllMembers();
            classBuilder.EndNest(); // class
            classBuilder.EndNest(); // namespace
            return(classBuilder.ToString());
        }
        private void WriteConstructorsAndFields()
        {
            var fields             = new List <Parameter>();
            var dbContextParameter = CodeWriterUtils.GetDbContextMethodParameter();

            dbContextParameter.Name = _dbContextFieldName;
            fields.Add(dbContextParameter);
            fields.Add(new Parameter
            {
                DataType = $"ILogger<{CodeWriterUtils.GetRepositoryImplementationName(table)}>",
                Name     = _loggerFieldName
            });
            fields.Add(new Parameter
            {
                DataType = "IServiceProvider",
                Name     = _serviceProviderFieldName
            });
            WriteFields(fields);
            classBuilder.AppendLine("");
            WriteConstructor(fields);
            classBuilder.AppendLine("");
        }