Ejemplo n.º 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("");
            }
        }
Ejemplo n.º 2
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("");
        }
Ejemplo n.º 3
0
        private void WriteColumn(DatabaseColumn column, bool notNetName)
        {
            var propertyName = CodeWriterUtils.GetPropertyNameForDatabaseColumn(column);
            var dataType     = CodeWriterUtils.FindDataType(column);

            if (notNetName)
            {
                //in EF, you want a fk Id property
                //must not conflict with entity fk name
                propertyName += "Id";
            }

            codeWriterSettings.CodeInserter.WriteColumnAnnotations(table, column, classBuilder);
            dataAnnotationWriter.Write(classBuilder, column, propertyName);
            var useVirtual = true;

            classBuilder.AppendAutomaticProperty(dataType, propertyName, useVirtual);
        }
        private void WriteParseEntityFromReader(string entityVariableName)
        {
            var ordinal = 0;

            foreach (var c in table.Columns)
            {
                var leftHandSideComponent = $"{entityVariableName}.{CodeWriterUtils.GetPropertyNameForDatabaseColumn(c)} = ";
                var dataType = CodeWriterUtils.FindDataType(c);
                var getValueFromReaderComponent = $"reader.GetFieldValue<{dataType}>({ordinal})";

                string line;
                if (c.Nullable)
                {
                    line = $"{leftHandSideComponent}reader.IsDBNull({ordinal}) ? null : {getValueFromReaderComponent};";
                }
                else
                {
                    line = $"{leftHandSideComponent}{getValueFromReaderComponent};";
                }

                classBuilder.AppendLine(line);
                ordinal++;
            }
        }