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 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);
        }
        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);
        }
        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);
        }
        private void AssignAttributesToProperties(IHasAttributes source, T obj, TargetChildMapping mapping)
        {
            var usage = MakeValuePairList(source);

            foreach (var map in mapping.Attributes)
            {
                var valueUsage = usage.Where(x => x.Value.Key == map.TargetName).FirstOrDefault();
                if (valueUsage != null)
                {
                    if (ReflectionUtilities.CanSetProperty(obj, map.TargetName))
                    {
                        valueUsage.IncrementUse();
                        ReflectionHelpers.AssignPropertyValue(obj, map.TargetName, valueUsage.Value.Value);
                    }
                }
            }
        }
        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);
        }
        private void AssignNamedProperties(IDom source, T obj, TargetChildMapping mapping)
        {
            var sourceHasStructuredDocs = source as IHasStructuredDocumentation;

            if (sourceHasStructuredDocs != null)
            {
                var xmlDocs = sourceHasStructuredDocs.StructuredDocumentation;
                obj.XmlCommentString = xmlDocs.Document;
            }
            var namedProperties = mapping.GetNamedProperties().ToList();

            foreach (var namedProperty in namedProperties)
            {
                var value = source.RequestValue(namedProperty, true);
                //while (value == null) { value = source.Parent.RequestValue(namedProperty); }
                if (ReflectionUtilities.CanSetProperty(obj, namedProperty))
                {
                    ReflectionHelpers.AssignPropertyValue(obj, namedProperty, value);
                }
            }
        }
 private IEnumerable <IDom> GetSourceChildren(IDom source, TargetChildMapping mapping)
 {
     // TODO: Add class, properties, etc. maybe namespace, depending on abstract root
     // yes, it's ugly, but the reflection here is worse, should have used DI
     if ((mapping as TargetClassMapping) != null)
     {
         return(((INestedContainer)source).Classes);
     }
     if ((mapping as TargetMethodMapping) != null)
     {
         return(((ITypeMemberContainer)source).Methods);
     }
     if ((mapping as TargetPropertyMapping) != null)
     {
         return(((ITypeMemberContainer)source).Properties);
     }
     if ((mapping as TargetParameterMapping) != null)
     {
         return(((IPropertyOrMethod)source).Parameters);
     }
     throw new NotImplementedException();
 }