Ejemplo n.º 1
0
        public MappingExecutor <T> Map <T>() where T : class
        {
            var type    = typeof(T);
            var mapping = Populate.GetMappingForType(type) as IContentMapping;

            if (mapping == null)
            {
                throw new Exception($"Type {type.FullName} does not implement IContentMapper");
            }

            if (mapping.ContentSource == null)
            {
                throw new Exception($"No mapping content source defined for type: {type}");
            }

            var contents = mapping.ContentSource(new UmbracoHelper(UmbracoContext.Current));

            return(Map <T>(contents));
        }
        private void Execute()
        {
            foreach (var content in _contents)
            {
                var currentType   = Populate.GetRegisteredType(content.DocumentTypeAlias, _type);
                var typeHierarchy = currentType.GetHierarchy();

                var cacheKey = $"{content.Id}|{currentType.FullName}";

                if (_session.Contains(cacheKey))
                {
                    var cachedModel = _session.Get <T>(cacheKey);

                    foreach (var type in typeHierarchy)
                    {
                        if (!_options.IncludedProperties.ContainsKey(type))
                        {
                            continue;
                        }

                        foreach (var propertyName in _options.IncludedProperties[type])
                        {
                            var prop = type.GetProperty(propertyName);

                            if (prop.GetValue(cachedModel) != null)
                            {
                                continue;
                            }

                            var mapping = Populate.GetMappingForType(type);

                            if (mapping == null)
                            {
                                continue;
                            }

                            var rule = mapping.GetRuleByProperty(propertyName);

                            rule.Execute(_session, _options, cachedModel, type, content);
                        }
                    }

                    _results.Add(cachedModel);

                    continue;
                }

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

                _session.Add(cacheKey, model);

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

                    if (mapping == null)
                    {
                        continue;
                    }

                    foreach (var rule in mapping.GetRules())
                    {
                        rule.Execute(_session, _options, model, type, content);
                    }
                }

                _results.Add(model);
            }
        }