static void WriteValueProperty(ColumnModel column, IndentedTextWriter writer) { writer.WriteLine(@"///<summary>" + column.Description.EscapeXml() + @"</summary>"); writer.WriteGeneratedCodeAttribute(); writer.WriteLine(column.PropertyVisibility.ToString().ToLowerInvariant() + " " + column.ActualType + " " + column.PropertyName + " {"); writer.Indent++; writer.WriteLine("get { return base.Field<" + column.ActualType + ">(" + column.ColumnPropertyName + "); }"); if (string.IsNullOrEmpty(column.Expression)) writer.WriteLine("set { base[" + column.ColumnPropertyName + "] = value; }"); writer.Indent--; writer.WriteLine("}"); }
static void WriteSchema(SchemaModel schema, IndentedTextWriter writer) { writer.WriteLine(@"///<summary>" + schema.RowClassDescription.EscapeXml() + @"</summary>"); writer.WriteLine(schema.RowClassVisibility.ToString().ToLowerInvariant() + " partial class " + schema.RowClassName + " : Row {"); writer.Indent++; writer.WriteLine(@"///<summary>Creates a new " + schema.RowClassName.EscapeXml() + @" instance.</summary>"); writer.WriteLine("public " + schema.RowClassName + " () : base(Schema) { Initialize(); }"); writer.WriteLine("partial void Initialize();"); writer.WriteLine(); writer.WriteLine(@"///<summary>Creates a strongly-typed " + schema.Name.EscapeXml() + @" table.</summary>"); writer.WriteLine("public static TypedTable<" + schema.RowClassName + "> CreateTable() " + "{ return new TypedTable<" + schema.RowClassName + ">(Schema, () => new " + schema.RowClassName + "()); }"); writer.WriteLine(); foreach (var column in schema.Columns) { writer.WriteLine(@"///<summary>Gets the schema's " + column.Name.EscapeXml() + @" column.</summary>"); writer.WriteLine("public static " + column.ColumnType.Name + " " + column.ColumnPropertyName + " { get; private set; }"); } writer.WriteLine(); writer.WriteLine(@"///<summary>Gets the " + schema.Name.EscapeXml() + @" schema instance.</summary>"); writer.WriteLine("public static new TypedSchema<" + schema.RowClassName + "> Schema { get; private set; }"); writer.WriteLine(@"///<summary>Gets the SchemaMapping that maps this schema to the SQL Server " + schema.SqlName.EscapeXml() + @" table.</summary>"); writer.WriteLine("public static SchemaMapping SchemaMapping { get; private set; }"); writer.WriteLine(); writer.WriteGeneratedCodeAttribute(); writer.WriteLine("static " + schema.RowClassName + "() {"); writer.Indent++; writer.WriteLine("#region Create Schema"); writer.WriteLine("Schema = new TypedSchema<" + schema.RowClassName + ">(" + schema.Name.Quote() + ");"); foreach (var column in schema.Columns) { writer.WriteLine(); WriteColumnCreation(column, writer); } writer.WriteLine("#endregion"); writer.WriteLine(); writer.WriteLine("#region Create SchemaMapping"); writer.WriteLine("SchemaMapping = new SchemaMapping(Schema, false);"); writer.WriteLine("SchemaMapping.SqlName = " + schema.SqlName.Quote() + ";"); writer.WriteLine("SchemaMapping.SqlSchemaName = " + schema.SqlSchemaName.Quote() + ";"); writer.WriteLine(); foreach (var column in schema.Columns.Where(c => c.GenerateSqlMapping)) writer.WriteLine("SchemaMapping.Columns.AddMapping(" + column.PropertyName + "Column, " + column.SqlName.Quote() + ");"); writer.WriteLine("#endregion"); writer.WriteLine("SchemaMapping.SetPrimaryMapping(SchemaMapping);"); writer.Indent--; writer.WriteLine("}"); writer.WriteLine(); writer.WriteLine(@"///<summary>Gets the typed table that contains this row, if any.</summary>"); writer.WriteGeneratedCodeAttribute(); writer.WriteLine("public new TypedTable<" + schema.RowClassName + "> Table { get { return (TypedTable<" + schema.RowClassName + ">)base.Table; } }"); writer.WriteLine("#region Value Properties"); foreach (var column in schema.Columns) WriteValueProperty(column, writer); writer.WriteLine("#endregion"); if (schema.ChildSchemas.Any()) { writer.WriteLine(); writer.WriteLine("#region ChildRows Properties"); //Support schemas with two foreign keys //that reference the same parent schema foreach (var foreignColumn in schema.ChildSchemas.Distinct().SelectMany(fs => fs.Columns.Where(c => c.ForeignSchema == schema))) { //Example: //public IChildRowCollection<Pledge> Pledges { get { return TypedChildRows<Pledge>(Pledge.PersonIdColumn); } } writer.WriteLine(@"///<summary>" + foreignColumn.ForeignRelationPropertyDescription.EscapeXml() + @"</summary>"); writer.WriteLine(foreignColumn.Owner.RowClassVisibility.ToString().ToLowerInvariant() + " IChildRowCollection<" + foreignColumn.Owner.RowClassName + "> " + foreignColumn.ForeignRelationPropertyName + " { get { return TypedChildRows<" + foreignColumn.Owner.RowClassName + ">(" + foreignColumn.Owner.RowClassName + "." + foreignColumn.PropertyName + "Column); } }"); } writer.WriteLine("#endregion"); } writer.WriteLine(); writer.WriteLine("#region Partial Methods"); writer.WriteLine("partial void OnColumnChanged(Column column, object oldValue, object newValue);"); foreach (var column in schema.Columns.NonCalculated()) { writer.WriteLine(); WriteColumnPartials(column, writer); } writer.WriteLine("#endregion"); writer.WriteLine(); writer.WriteLine("#region Column Callbacks"); #region ValidateValue writer.WriteLine(@"///<summary>Checks whether a value would be valid for a given column in an attached row.</summary>"); writer.WriteLine(@"///<param name=""column"">The column containing the value.</param>"); writer.WriteLine(@"///<param name=""newValue"">The value to validate.</param>"); writer.WriteLine(@"///<returns>An error message, or null if the value is valid.</returns>"); writer.WriteLine(@"///<remarks>This method is overridden by typed rows to perform custom validation logic.</remarks>"); writer.WriteGeneratedCodeAttribute(); writer.WriteLine("public override string ValidateValue(Column column, object newValue) {"); writer.Indent++; writer.WriteLine("string error = base.ValidateValue(column, newValue);"); writer.WriteLine("if (!String.IsNullOrEmpty(error)) return error;"); writer.WriteLine("Action<string> reporter = s => error = s;"); writer.WriteLine(); bool first = true; foreach (var column in schema.Columns.NonCalculated()) { if (first) first = false; else writer.Write("} else "); writer.WriteLine("if (column == " + column.PropertyName + "Column) {"); writer.Indent++; writer.WriteLine("Validate" + column.PropertyName + "((" + column.ActualType + ")newValue, reporter);"); writer.WriteLine("if (!String.IsNullOrEmpty(error)) return error;"); writer.Indent--; } writer.WriteLine("}"); writer.WriteLine("return null;"); writer.Indent--; writer.WriteLine("}"); #endregion #region OnValueChanged writer.WriteLine(@"///<summary>Processes an explicit change of a column value.</summary>"); writer.WriteGeneratedCodeAttribute(); writer.WriteLine("protected override void OnValueChanged(Column column, object oldValue, object newValue) {"); writer.Indent++; writer.WriteLine("base.OnValueChanged(column, oldValue, newValue);"); writer.WriteLine("OnColumnChanged(column, oldValue, newValue);"); first = true; foreach (var column in schema.Columns.NonCalculated()) { if (first) first = false; else writer.Write("else "); writer.WriteLine("if (column == " + column.PropertyName + "Column)"); writer.WriteLine("\tOn" + column.PropertyName + "Changed((" + column.NullableType + ")oldValue, (" + column.NullableType + ")newValue);"); } writer.Indent--; writer.WriteLine("}"); #endregion writer.WriteLine("#endregion"); writer.Indent--; writer.WriteLine("}"); }