private void AssignChildren(IDom source, T obj, TargetChildMapping mapping, CodeFirstMetadata parent) { foreach (var childMap in mapping.Children) { var newItems = ReflectionHelpers.InvokeGenericMethod(thisType, "CreateChildren", childMap.UnderlyingTypInfo, this, source, childMap, parent); ReflectionHelpers.AssignPropertyValue(obj, childMap.TargetName, newItems); } }
public virtual T Map(TargetChildMapping mapping, IDom source, CodeFirstMetadata parent) { // var codeFirstType = mapping.GetType().GetTypeInfo().GenericTypeArguments.First(); var newItem = ReflectionHelpers.CreateInstanceOfType <T>(); newItem.Parent = parent; AssignNamedProperties(source, newItem, mapping); var sourceWithAttributes = source as IHasAttributes; if (sourceWithAttributes != null) { AssignAttributesToProperties(sourceWithAttributes, newItem, mapping); } AssignChildren(source, newItem, mapping, newItem); return(newItem); }
public virtual IEnumerable <T> MapFromConstructor(TargetChildMapping mapping, IDom source, CodeFirstMetadata parent) { var newItems = new List <T>(); var sourceWithConstructor = source as IClassOrStructure; if (sourceWithConstructor != null) { foreach (var constructor in sourceWithConstructor.Constructors) // no clue what multiple constructors might mean { foreach (var statement in constructor.Statements) { newItems.AddRange(MapInvocation(statement as IInvocationStatement, source as IClass, parent)); } } } return(newItems); }
public virtual IEnumerable <T> MapList(TargetChildMapping mapping, IEnumerable <IDom> sourceList, CodeFirstMetadata parent) { var ret = new List <T>(); foreach (var source in sourceList) { ret.Add(Map(mapping, source, parent)); } return(ret); }
private IEnumerable <T> MapInvocation(IInvocationStatement invocation, IClass source, CodeFirstMetadata parent) { var newItems = new List <T>(); if (invocation == null) { return(newItems); } if (source == null) { return(newItems); } var name = invocation.MethodName; // English language convention used here if (name.StartsWith("Add")) { name = name.SubstringAfter("Add"); var plural = Pluralizer.Pluralize(name); var current = source; if (current != null) { var property = GetPropertyUseBase(plural, ref current); if (property != null) { var newItem = Activator.CreateInstance <T>(); newItem.Parent = parent; SetNewItemProperties(newItem, invocation); newItems.Add(newItem); } } } return(newItems); }
private IEnumerable <TLocal> CreateChildren <TLocal>(IDom source, TargetChildMapping mapping, CodeFirstMetadata parent) where TLocal : CodeFirstMetadata { var mapper = serviceProvider.GetMapper2 <TLocal>(); var sourceChildren = GetSourceChildren(source, mapping); var items = new List <TLocal>(); if (sourceChildren != null) { items.AddRange(mapper.MapList(mapping, sourceChildren, parent)); } var newItems2 = mapper.MapFromConstructor(mapping, source, parent); items.AddRange(newItems2); return(items); }