Esempio n. 1
0
        /// <summary>
        /// Loads the specified loaders.
        /// </summary>
        /// <param name="loaders">The list of configuration loaders to load into the context.</param>
        public void Load(params IConfigurationLoader[] loaders)
        {
            if (loaders.Any())
            {
                var typeConfigurations = loaders
                                         .Select(loader => loader.Load()).Aggregate((x, y) => x.Union(y));

                //first we have to add each type config to the collection
                foreach (var typeConfig in typeConfigurations)
                {
                    if (TypeConfigurations.ContainsKey(typeConfig.Type))
                    {
                        Log.Warn("Tried to add type {0} to TypeConfigurationDictioary twice".Formatted(typeConfig.Type));
                        continue;
                    }

                    typeConfig.PerformAutoMap();

                    if (!TypeConfigurations.TryAdd(typeConfig.Type, typeConfig))
                    {
                        Log.Warn("Failed to add type {0} to TypeConfigurationDictionary".Formatted(typeConfig.Type));
                    }
                }
                //then process the properties.
                //this stops the problem of types not existing for certain data handlers
                foreach (var typeConfig in typeConfigurations)
                {
                    ProcessProperties(typeConfig.Properties);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Loads the specified loaders.
        /// </summary>
        /// <param name="loaders">The list of configuration loaders to load into the context.</param>
        public void Load(params IConfigurationLoader[] loaders)
        {
            if (loaders.Any())
            {
                var typeConfigurations = loaders
                                         .Select(loader => loader.Load()).Aggregate((x, y) => x.Union(y));

                //first we have to add each type config to the collection
                foreach (var typeConfig in typeConfigurations)
                {
                    //don't load generic types or specifically the object type
                    //see https://github.com/mikeedwards83/Glass.Mapper/issues/85
                    if (typeConfig.Type.IsGenericTypeDefinition || typeConfig.Type == typeof(System.Object))
                    {
                        continue;
                    }


                    if (TypeConfigurations.ContainsKey(typeConfig.Type))
                    {
                        Log.Warn("Tried to add type {0} to TypeConfigurationDictioary twice".Formatted(typeConfig.Type));
                        continue;
                    }

                    typeConfig.PerformAutoMap();

                    ProcessProperties(typeConfig.Properties);

                    if (!TypeConfigurations.TryAdd(typeConfig.Type, typeConfig))
                    {
                        Log.Warn("Failed to add type {0} to TypeConfigurationDictionary".Formatted(typeConfig.Type));
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the type configuration.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns>AbstractTypeConfiguration.</returns>
        public AbstractTypeConfiguration GetTypeConfiguration(object obj)
        {
            var type   = obj.GetType();
            var config = TypeConfigurations.ContainsKey(type) ? TypeConfigurations[type] : null;

            if (config != null)
            {
                return(config);
            }

            //check base type encase of proxy
            config = TypeConfigurations.ContainsKey(type.BaseType) ? TypeConfigurations[type.BaseType] : null;

            if (config != null)
            {
                return(config);
            }

            //check interfaces encase this is an interface proxy
            string name = type.Name;
            //ME - I added the OrderByDescending in response to issue 53
            // raised on the Glass.Sitecore.Mapper project. Longest name should be compared first
            // to get the most specific interface
            var interfaceType = type.GetInterfaces().OrderByDescending(x => x.Name.Length).FirstOrDefault(x => name.Contains(x.Name));

            if (interfaceType != null)
            {
                config = TypeConfigurations.ContainsKey(interfaceType) ? TypeConfigurations[interfaceType] : null;
            }

            return(config);
        }
Esempio n. 4
0
        /// <summary>
        /// Gets the type configuration.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns>AbstractTypeConfiguration.</returns>
        public AbstractTypeConfiguration GetTypeConfiguration(object obj)
        {
            var type   = obj.GetType();
            var config = TypeConfigurations.ContainsKey(type) ? TypeConfigurations[type] : null;

            if (config != null)
            {
                return(config);
            }

            //check base type encase of proxy
            config = TypeConfigurations.ContainsKey(type.BaseType) ? TypeConfigurations[type.BaseType] : null;

            if (config != null)
            {
                return(config);
            }

            //check interfaces encase this is an interface proxy
            string name          = type.Name;
            var    interfaceType = type.GetInterfaces().FirstOrDefault(x => name.Contains(x.Name));

            if (interfaceType != null)
            {
                config = TypeConfigurations.ContainsKey(interfaceType) ? TypeConfigurations[interfaceType] : null;
            }

            return(config);
        }
        /// <summary>
        /// Bases the template checks.
        /// </summary>
        /// <param name="template">The template.</param>
        /// <param name="config">The config.</param>
        /// <param name="db">The database.</param>
        private void BaseTemplateChecks(
            ItemDefinition template,
            SitecoreTypeConfiguration config,
            Database db)
        {
            //check base templates
            var templateItem       = db.GetItem(template.ID);
            var baseTemplatesField = templateItem[FieldIDs.BaseTemplate];
            var sb = new StringBuilder(baseTemplatesField);

            Sitecore.Diagnostics.Log.Info("Type {0}".Formatted(config.Type.FullName), this);

            Action <Type> idCheck = type =>
            {
                Sitecore.Diagnostics.Log.Info("ID Check {0}".Formatted(type.FullName), this);

                if (!TypeConfigurations.ContainsKey(type))
                {
                    return;
                }

                var baseConfig = TypeConfigurations[type];
                if (baseConfig != null && baseConfig.TemplateId.Guid != Guid.Empty)
                {
                    if (!baseTemplatesField.Contains(baseConfig.TemplateId.ToString()))
                    {
                        sb.Append("|{0}".Formatted(baseConfig.TemplateId));
                    }
                }
            };

            Type baseType = config.Type.BaseType;

            while (baseType != null)
            {
                idCheck(baseType);
                baseType = baseType.BaseType;
            }

            config.Type.GetInterfaces().ForEach(idCheck);

            //dirty fix for circular template inheritance
            var baseTemplates = sb.ToString().Split('|').ToList();

            baseTemplates.Remove(config.TemplateId.ToString());
            sb.Clear();
            sb.Append(string.Join("|", baseTemplates));

            if (baseTemplatesField != sb.ToString())
            {
                templateItem.Editing.BeginEdit();
                templateItem[FieldIDs.BaseTemplate] = sb.ToString();
                templateItem.Editing.EndEdit();
            }
        }
        /// <summary>
        /// Check a folder and all sub folders in Sitecore for templates
        /// </summary>
        /// <param name="folder">The folder.</param>
        /// <param name="sqlDataProvider">The SQL data provider.</param>
        /// <param name="context">The context.</param>
        /// <returns>
        /// True of the folder is deleted itself.
        /// </returns>
        private bool RemoveDeletedClasses(ItemDefinition folder, DataProvider sqlDataProvider, CallContext context)
        {
            if (folder == null)
            {
                throw new ArgumentNullException("folder");
            }
            if (sqlDataProvider == null)
            {
                throw new ArgumentNullException("sqlDataProvider");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var childIds = sqlDataProvider.GetChildIDs(folder, context);

            //check child items
            foreach (ID childId in childIds.ToArray())
            {
                var childDefinition = sqlDataProvider.GetItemDefinition(childId, context);

                //if child is template check the template still exists in the code base
                if (childDefinition.TemplateID == TemplateTemplateId)
                {
                    if (TypeConfigurations.Any(x => x.Value.TemplateId == childDefinition.ID && x.Value.CodeFirst))
                    {
                        continue;
                    }

                    sqlDataProvider.DeleteItem(childDefinition, context);
                    childIds.Remove(childDefinition.ID);
                }
                // if the child is a folder check the children of the folder
                else if (childDefinition.TemplateID == FolderTemplateId)
                {
                    //if the folder itself is deleted then remove from the parent
                    if (RemoveDeletedClasses(childDefinition, sqlDataProvider, context))
                    {
                        childIds.Remove(childDefinition.ID);
                    }
                }
            }

            //if there are no children left delete the folder
            if (childIds.Count == 0 && folder.ID != GlassFolderId)
            {
                sqlDataProvider.DeleteItem(folder, context);
                return(true);
            }

            return(false);
        }
Esempio n. 7
0
 /// <summary>
 /// Gets a type configuration based on type
 /// </summary>
 /// <param name="type">The type.</param>
 /// <returns>AbstractTypeConfiguration.</returns>
 public AbstractTypeConfiguration this[Type type]
 {
     get
     {
         if (TypeConfigurations.ContainsKey(type))
         {
             return(TypeConfigurations[type]);
         }
         else
         {
             return(null);
         }
     }
 }
Esempio n. 8
0
        TypeMap IProfileConfiguration.ConfigureConventionTypeMap(TypeMapRegistry typeMapRegistry, TypePair types)
        {
            if (!TypeConfigurations.Any(c => c.IsMatch(types)))
            {
                return(null);
            }

            var typeMap = _typeMapFactory.CreateTypeMap(types.SourceType, types.DestinationType, this, MemberList.Destination);

            var config = new MappingExpression(typeMap.Types, typeMap.ConfiguredMemberList);

            config.Configure(this, typeMap);

            Configure(typeMapRegistry, typeMap);

            return(typeMap);
        }
Esempio n. 9
0
        /// <summary>
        /// Gets the type configuration.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns>AbstractTypeConfiguration.</returns>
        public T GetTypeConfigurationFromType <T>(Type type, bool doNotLoad = false, bool checkBase = true)
            where T : AbstractTypeConfiguration, new()
        {
            var config = TypeConfigurations.ContainsKey(type) ? TypeConfigurations[type] : null;

            if (config != null)
            {
                return(config as T);
            }

            if (checkBase && type.BaseType != null)
            {
                //check base type encase of proxy
                config = TypeConfigurations.ContainsKey(type.BaseType) ? TypeConfigurations[type.BaseType] : null;
            }

            if (config != null)
            {
                return(config as T);
            }

            //check interfaces encase this is an interface proxy
            string name = type.Name;
            //ME - I added the OrderByDescending in response to issue 53
            // raised on the Glass.Sitecore.Mapper project. Longest name should be compared first
            // to get the most specific interface
            var interfaceType =
                type.GetInterfaces()
                .OrderByDescending(x => x.Name.Length)
                .FirstOrDefault(x => name.Contains(x.Name));

            if (interfaceType != null)
            {
                config = TypeConfigurations.ContainsKey(interfaceType) ? TypeConfigurations[interfaceType] : null;
            }

            if (config == null && !doNotLoad)
            {
                Load(new OnDemandLoader <T>(type));
                return(GetTypeConfigurationFromType <T>(type, true));
            }

            return(config as T);
        }
Esempio n. 10
0
        /// <summary>
        /// Merges other config into this.
        /// </summary>
        internal void MergeTypes(BinaryConfiguration localConfig)
        {
            if (TypeConfigurations == null)
            {
                TypeConfigurations = localConfig.TypeConfigurations;
            }
            else if (localConfig.TypeConfigurations != null)
            {
                // Both configs are present.
                // Local configuration is more complete and takes preference when it exists for a given type.
                var localTypeNames = new HashSet <string>(localConfig.TypeConfigurations.Select(x => x.TypeName),
                                                          StringComparer.OrdinalIgnoreCase);

                var configs = new List <BinaryTypeConfiguration>(localConfig.TypeConfigurations);

                configs.AddRange(TypeConfigurations.Where(x => !localTypeNames.Contains(x.TypeName)));

                TypeConfigurations = configs;
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Loads the specified loaders.
        /// </summary>
        /// <param name="loaders">The list of configuration loaders to load into the context.</param>
        public void Load(params IConfigurationLoader[] loaders)
        {
            if (loaders.Any())
            {
                var typeConfigurations = loaders
                                         .Select(loader => loader.Load()).Aggregate((x, y) => x.Union(y));

                //first we have to add each type config to the collection
                foreach (var typeConfig in typeConfigurations)
                {
                    //don't load generic types
                    //see https://github.com/mikeedwards83/Glass.Mapper/issues/85
                    if (typeConfig.Type.IsGenericTypeDefinition)
                    {
                        continue;
                    }


                    if (TypeConfigurations.ContainsKey(typeConfig.Type))
                    {
                        Log.Warn("Tried to add type {0} to TypeConfigurationDictioary twice".Formatted(typeConfig.Type));
                        continue;
                    }

                    typeConfig.PerformAutoMap();

                    if (!TypeConfigurations.TryAdd(typeConfig.Type, typeConfig))
                    {
                        Log.Warn("Failed to add type {0} to TypeConfigurationDictionary".Formatted(typeConfig.Type));
                    }
                }
                //then process the properties.
                //this stops the problem of types not existing for certain data handlers
                foreach (var typeConfig in typeConfigurations)
                {
                    ProcessProperties(typeConfig.Properties);
                }
            }
        }
Esempio n. 12
0
        public void Configure <T>(Action <TypeConfiguration <T> > configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            if (Built)
            {
                throw new InvalidOperationException("The configuration has already been built.");
            }

            var type = typeof(T);
            var typeConfiguration = TypeConfigurations.FirstOrDefault(c => c.Type == type) as TypeConfiguration <T>;

            if (typeConfiguration == null)
            {
                typeConfiguration = new TypeConfiguration <T>();
                TypeConfigurations.Add(typeConfiguration);
            }

            configure(typeConfiguration);
        }
Esempio n. 13
0
        private void CollectAssemblyDefinedTypeConfigurations()
        {
            if (!Assemblies.Any())
            {
                return;
            }

            foreach (var assembly in Assemblies)
            {
                var typeConfigurationTypeInfo = typeof(TypeConfiguration).GetTypeInfo();
                var types = assembly.ExportedTypes
                            .Select(t => t.GetTypeInfo())
                            .Where(t => !t.IsAbstract && typeConfigurationTypeInfo.IsAssignableFrom(t))
                            .ToList();

                foreach (var type in types)
                {
                    var baseTypeConfigurationType = FindGenericBaseTypeConfigurationType(type);
                    if (baseTypeConfigurationType == null)
                    {
                        throw new InvalidOperationException("You should extend from the generic version of TypeConfiguration.");
                    }

                    var instance    = (TypeConfiguration)Activator.CreateInstance(type.AsType());
                    var elementType = GetElementTypeFromConcreteTypeConfiguration(baseTypeConfigurationType);
                    var existing    = TypeConfigurations.FirstOrDefault(tc => tc.Type == elementType);
                    if (existing != null)
                    {
                        existing.Combine(instance);
                    }
                    else
                    {
                        TypeConfigurations.Add(instance);
                    }
                }
            }
        }
Esempio n. 14
0
        /// <summary>
        /// Loads the specified loaders.
        /// </summary>
        /// <param name="loaders">The list of configuration loaders to load into the context.</param>
        public void Load(params IConfigurationLoader[] loaders)
        {
            if (loaders.Any())
            {
                var typeConfigurations = loaders
                                         .Select(loader => loader.Load()).Aggregate((x, y) => x.Union(y));



                //first we have to add each type config to the collection
                foreach (var typeConfig in typeConfigurations)
                {
                    typeConfig.PerformAutoMap();

                    TypeConfigurations.Add(typeConfig.Type, typeConfig);
                }
                //then process the properties.
                //this stops the problem of types not existing for certain data handlers
                foreach (var typeConfig in typeConfigurations)
                {
                    ProcessProperties(typeConfig.Properties);
                }
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Loads the specified loaders.
        /// </summary>
        /// <param name="loaders">The list of configuration loaders to load into the context.</param>
        public void Load(params IConfigurationLoader[] loaders)
        {
            if (!loaders.Any())
            {
                return;
            }

            var typeConfigurations = loaders
                                     .Select(loader => loader.Load()).Aggregate((x, y) => x.Union(y));

            //first we have to add each type config to the collection
            foreach (var typeConfig in typeConfigurations)
            {
                //don't load generic types or specifically the object type
                //see https://github.com/mikeedwards83/Glass.Mapper/issues/85
                if (typeConfig.Type.IsGenericTypeDefinition || typeConfig.Type == typeof(System.Object))
                {
                    continue;
                }


                if (TypeConfigurations.ContainsKey(typeConfig.Type))
                {
                    Log.Warn("Tried to add type {0} to TypeConfigurationDictioary twice".Formatted(typeConfig.Type));
                    continue;
                }

                typeConfig.PerformAutoMap();

                if (!Config.AutoImportBaseClasses)                 //if we have to import baseclasses... wait for it
                {
                    ProcessProperties(typeConfig.Properties);
                }

                if (!TypeConfigurations.TryAdd(typeConfig.Type, typeConfig))
                {
                    Log.Warn("Failed to add type {0} to TypeConfigurationDictionary".Formatted(typeConfig.Type));
                }
            }

            if (Config.AutoImportBaseClasses)
            {
                foreach (var typeconfig in TypeConfigurations)        //go through all typeconfigs, one can be changed now
                {
                    var type      = typeconfig.Key;
                    var baseTypes = type.GetBaseClassesAndInterfaces();
                    foreach (var baseType in baseTypes)
                    {
                        AbstractTypeConfiguration config;
                        if (TypeConfigurations.TryGetValue(baseType, out config))
                        {
                            typeconfig.Value.Import(config);
                        }
                    }
                }
                foreach (var typeConfig in TypeConfigurations.Values)
                {
                    ProcessProperties(typeConfig.Properties);
                }
            }
        }
Esempio n. 16
0
 public bool IsConventionMap(TypePair types) => TypeConfigurations.Any(c => c.IsMatch(types));