Esempio n. 1
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. 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())
            {
                if (Config.OnDemandMappingEnabled == false &&
                    loaders
                    .Select(loader => loader.GetType())
                    .Any(type => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(OnDemandLoader <>))
                    )
                {
                    throw new MapperException(Constants.Errors.OnDemandDisabled);
                }

                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);
                    ProcessProperties(typeConfig.PrivateProperties);

                    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 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. 4
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. 5
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);
                }
            }
        }