public TCollection Build(ObjectGraph values, IPropMapValueCache propMapValueCache, ILogger logger) { var objects = SplitObjectGraph(values, logger).Select(BuildSingleObject); return(CollectionBuilder(objects)); T BuildSingleObject(ObjectGraph obj) { var result = SingleObjBuilder.Build(obj, propMapValueCache, logger); obj.Dispose(); return(result); } }
/// <summary> /// Build a concrete object from an object graph /// </summary> public T Build(ObjectGraph values, IPropMapValueCache propMapValueCache, ILogger logger) { var obj = BuildObject(values, propMapValueCache, logger); var enumProps = (values?.GetSimpleProps()) .OrEmpty() .Where(isEnumerableDataCell) .Select(name); AddEmptyEnumerables(obj, enumProps); return(obj); bool isEnumerableDataCell((string, IEnumerable <object>, bool isEnumerableDataCell) c) => c.isEnumerableDataCell; string name((string name, IEnumerable <object>, bool) c) => c.name; }
static T InvokeObjectConstructor <T>( Dictionary <Type[], IValueGetter[]> cArgGetters, ObjectGraph vals, IPropMapValueCache propMapValueCache, ILogger logger) { var constructorArgTypes = vals.ConstructorArgTypes ?? EmptyTypes; if (!cArgGetters.ContainsKey(constructorArgTypes)) { throw new InvalidOperationException($"Unable to find constructor for object {typeof(T)} with constructor args [{constructorArgTypes.JoinString(", ")}]."); } var constructor = cArgGetters[constructorArgTypes]; var cargs = vals.GetSimpleConstructorArgs() .Select(SimpleConstructorArg) .Concat(vals.GetComplexConstructorArgs() .OrEmpty() .Select(BuildAndDisposeofComplexConstructorArg)) .OrderBy(ca => ca.i) .Select((ca, i) => { if (i != ca.i) { throw new InvalidOperationException($"Expecting constructor arg {i}, but got {ca.i}."); } return(ca.v); }); // Create output object return((T)ConstructObject( typeof(T), constructorArgTypes, cargs.ToArray())); (int i, object v) SimpleConstructorArg((int argIndex, IEnumerable <object> value, bool isEnumerableDataCell) arg) => GetSimpleConstructorArg(constructor, arg, logger); (int i, object v) BuildAndDisposeofComplexConstructorArg((int argIndex, IEnumerable <ObjectGraph> value) arg) => GetComplexConstructorArgAndDisposeObjectGraphs(constructorArgTypes, constructor, arg, propMapValueCache, logger); }
/// <summary> /// Parse the results of a sql query which returns a comlex object /// </summary> /// <param name="rows">The query results</param> /// <param name="propertyGraph">The query columns mapped to an object graph</param> static IEnumerable <TResult> ParseComplex <TResult>(this IEnumerable <object[]> rows, RootObjectPropertyGraph propertyGraph, ILogger logger, IPropMapValueCache propMapBuilder) { var objectGraphCache = new ObjectGraphCache(logger); var builder = Builders.GetBuilder <TResult>(); foreach (var obj in CreateObject(propertyGraph, objectGraphCache, rows, logger)) { var result = builder.Build(obj, propMapBuilder, logger); obj.Dispose(); yield return(result); } }
/// <summary> /// Build a concrete object from an object graph /// </summary> object IBuilder.Build(ObjectGraph values, IPropMapValueCache propMapValueCache, ILogger logger) => Build(values, propMapValueCache, logger);