Ejemplo n.º 1
0
        private void Execute()
        {
            foreach (var fieldset in _model)
            {
                var alias         = $"{_dataType}.{fieldset.Alias}";
                var currentType   = Populate.GetRegisteredType(alias, _type);
                var typeHierarchy = currentType.GetHierarchy();

                var archetypeModel = Activator.CreateInstance(currentType) as T;

                foreach (var type in typeHierarchy)
                {
                    var mapping = Populate.GetMappingForType(type);

                    if (mapping == null)
                    {
                        continue;
                    }

                    var rules = mapping.GetRules();

                    foreach (var rule in rules)
                    {
                        rule.Execute(_session, _options, archetypeModel, type, fieldset);
                    }
                }

                _results.Add(archetypeModel);
            }
        }
        void IMappingRule.Execute(IMappingSession session, MappingOptions options, object model, Type type, object source)
        {
            var content = source as IPublishedContent;

            if (content == null)
            {
                throw new Exception("Expected source type IPublishedContent");
            }

            var destProperty = type.GetProperty(_propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? type.GetProperty(_propertyName);

            var componentModel = new T();

            //If component was not mapped inline, then we need to fetch it from the mapping registry
            if (_componentMapping == null)
            {
                _componentMapping = Populate.GetMappingForType(typeof(T));

                if (_componentMapping == null)
                {
                    throw new Exception($"No component mapper is defined for type: {typeof(T).FullName}");
                }
            }

            var rules = _componentMapping.GetRules();

            foreach (var rule in rules)
            {
                rule.Execute(session, options, componentModel, typeof(T), content);
            }

            destProperty.SetValue(model, componentModel);
        }