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("");
            }
        }