/// <summary> /// Recursively processes <see cref="ITable"/> instances to create <see cref="RowInstance"/> data for each table in the hierarchy. /// </summary> /// <param name="table"></param> void CreateInstances(ITable table, RowModelType type) { // Get the properties for the specified table columns var properties = table.Columns.Select(c => type.Properties[c.Name] as ModelValueProperty).ToArray(); // Verify all of the columns correlate to value properties if (properties.Any(p => p == null)) { throw new ArgumentException("The specified table columns do not match properties on the specified model type."); } // Create instances for each row in the table foreach (var row in table.Rows) { var instance = new RowInstance(type); var c = 0; foreach (var value in row) { instance.SetValue(properties[c++], value); } type.Instances.Add(instance.Id, instance); } // Recursively process child tables foreach (var child in table.Children) { CreateInstances(child, (RowModelType)((RowModelReferenceProperty)type.Properties[child.Name]).PropertyType); } }
/// <summary> /// Initialize provider with default values. /// </summary> public RowModelTypeProvider(string @namespace, ITable table, string identifier) { this.@namespace = @namespace; // Create model types based on the specified root table this.rootType = CreateType(table, identifier, null); // Create model instances based on the specified root table CreateInstances(table, this.rootType); }
/// <summary> /// Recursively processes <see cref="ITable"/> instances to build a corresponding <see cref="RowModelType"/> hierarchy. /// </summary> /// <param name="table"></param> /// <param name="identifier"></param> /// <param name="parentType"></param> RowModelType CreateType(ITable table, string identifier, RowModelType parentType) { // Ensure the table has at least two columns if (table.Columns.Count() < 2) { throw new ArgumentException("The import table '" + table.Name + "' only has " + table.Columns.Count() + " column, and at least 2 are required."); } // Determine the type name var typeName = String.IsNullOrEmpty(@namespace) ? table.Name : @namespace + "." + table.Name; var type = new RowModelType(typeName, parentType); this.types[typeName] = type; // Create properties for each column in the table var properties = table.Columns.Select(column => (ModelProperty) new RowModelValueProperty(type, nameRegex.Replace(column.Name, ""), column.Name, null, null, false, typeof(string), false, false, false)).ToList(); // Establish the relationship between the parent and child if a parent type was specified if (parentType != null) { // Create the reference from the child to the parent var parentColumn = properties[1]; properties[1] = new RowModelReferenceProperty(type, parentColumn.Name, parentColumn.Label, null, null, false, parentType, false, false, true); // Create the reference from the parent to the child parentType.AddProperties(new[] { new RowModelReferenceProperty(parentType, nameRegex.Replace(type.Name, ""), type.Name, null, null, false, type, true, false, true) }); } // Add the properties to the model type type.AddProperties(properties); // Replace property labels with property names in mapping expressions foreach (var property in properties) { identifier = identifier .Replace("[" + property.Label + "]", property.Name) .Replace(property.Label, property.Name); } // Compile the identifier expression type.IdentifierExpression = type.GetExpression(identifier); // Recursively create child types foreach (var child in table.Children) { CreateType(child, child.Columns.First().Name, type); } // Return the new model type return(type); }
internal RowInstance(RowModelType type) : base(type, "") { this.values = new object[type.Properties.Count]; // Initialize list properties foreach (var property in type.Properties) { if (property is ModelReferenceProperty && ((ModelReferenceProperty)property).IsList) { this.values[property.Index] = new RowInstanceList(this); } } }
public void AddReferenceProperty(string name, RowModelType type, bool isList) { AddProperty(new RowModelReferenceProperty(this, name, name, null, null, false, type, isList, false, true)); }
public RowModelType(string name, RowModelType parentType) : base(name, name, null, null, null, null) { this.ParentType = parentType; this.Instances = new Dictionary <string, RowInstance>(); }