public static CodeBuilder WriteDocument(CodeBuilder cb = null) { cb = cb ?? new CodeBuilder(); foreach (var et in ObjectModelReflection.GetEntityTypes()) { WriteEntityClass(et, cb); } cb.AppendLine("public partial class DocumentModel"); cb.AppendLine("{"); cb.AppendLine("Document Document;"); foreach (var et in ObjectModelReflection.GetEntityTypes()) { WriteDocumentEntityData(et, cb); } // Write field declarations cb.AppendLine("// Entity collections"); foreach (var t in ObjectModelReflection.GetEntityTypes()) { cb.AppendLine($"public readonly IArray<{t.Name}> {t.Name}List;"); } cb.AppendLine(); // Write property getters for the entity properties cb.AppendLine("// Entity properties"); foreach (var t in ObjectModelReflection.GetEntityTypes()) { cb.AppendLine($"public DictionaryOfLists<int, Property> {t.Name}PropertyLists => {t.Name}EntityTable.PropertyLists;"); } cb.AppendLine(); // Write the constructor cb.AppendLine("public DocumentModel(Document d)"); cb.AppendLine("{"); cb.AppendLine("Document = d;"); cb.AppendLine($"// Initialize entity collections"); foreach (var t in ObjectModelReflection.GetEntityTypes()) { cb.AppendLine($"{t.Name}List = Num{t.Name}.Select(i => Get{t.Name}(i));"); } cb.AppendLine(); cb.AppendLine("}"); cb.AppendLine("} // Document class"); return(cb); }
public static void WriteDocumentBuilder(CodeBuilder cb) { cb.AppendLine("public static class DocumentBuilderExtensions"); cb.AppendLine("{"); cb.AppendLine($"public static TableBuilder ToTableBuilder(this IEnumerable<Entity> entities)"); cb.AppendLine("{"); cb.AppendLine("var first = entities.FirstOrDefault();"); cb.AppendLine("if (first == null) return null;"); foreach (var et in ObjectModelReflection.GetEntityTypes()) { cb.AppendLine($"if (first is {et.Name}) return entities.Cast<{et.Name}>().ToTableBuilder();"); } cb.AppendLine("throw new Exception($\"Could not generate a TableBuilder for {first.GetType()}\");"); cb.AppendLine("}"); foreach (var et in ObjectModelReflection.GetEntityTypes()) { var entityType = et.Name; cb.AppendLine($"public static TableBuilder ToTableBuilder(this IEnumerable<{entityType}> typedEntities)"); cb.AppendLine("{"); var tableName = ObjectModelReflection.GetEntityTableName(et); cb.AppendLine($"var tb = new TableBuilder(\"{tableName}\");"); var entityFields = et.GetEntityFields().ToArray(); foreach (var f in entityFields) { var name = f.Name; cb.AppendLine($"tb.AddColumn(\"{name}\", typedEntities.Select(x => x.{name}));"); } var relationFields = et.GetRelationFields().ToArray(); foreach (var f in relationFields) { var name = f.Name.Substring(1); var relType = ObjectModelReflection.RelationTypeParameter(f.FieldType); var relatedTableName = ObjectModelReflection.GetEntityTableName(relType); if (string.IsNullOrEmpty(relatedTableName)) { throw new Exception($"Could not find related table for type {relType}"); } cb.AppendLine($"tb.AddIndexColumn(\"{relatedTableName}\", \"{name}\", typedEntities.Select(x => x._{name}.Index));"); } cb.AppendLine("return tb;"); cb.AppendLine("}"); } cb.AppendLine("} // DocumentBuilderExtensions"); }