Exemplo n.º 1
0
        public void Add(TypeDefinition key, TypeDefinition targetType, DuplicateKeyBehavior behavior)
        {
            //TODO Better filtering, mostly just to remove <Module>
            if (targetType.FullName.Contains('<') || targetType.FullName.Contains('>'))
            {
                return;
            }


            //Last key in wins, this allows for manual mapping to override things added with behaviors
            bool duplicateKey = false;

            foreach (var kvp in _maps.Where(kvp => kvp.Value.Keys.Contains(key)).ToList())
            {
                duplicateKey = true;
                kvp.Value.Keys.Remove(key);
                if (!kvp.Value.Keys.Any())
                {
                    _maps.Remove(kvp.Key);
                }
            }

            if (duplicateKey && behavior == DuplicateKeyBehavior.RemoveAll)
            {
                return;
            }

            if (!_maps.TryGetValue(targetType.FullName, out TypeMap typeMap))
            {
                _maps[targetType.FullName] = typeMap = new TypeMap(targetType);
            }

            typeMap.Keys.Add(key);
        }
Exemplo n.º 2
0
        public void Add(TypeDefinition key, TypeDefinition targetType, DuplicateKeyBehavior behavior, Lifetime?lifetime)
        {
            //TODO Better filtering, mostly just to remove <Module>
            if (targetType.Name.StartsWith("<") || targetType.Name.EndsWith(">"))
            {
                return;
            }

            //Issue 59 - don't allow compile-time mapping to open generics
            if (targetType.HasGenericParameters || key.HasGenericParameters)
            {
                _logger.Debug($"Ignoring map from '{key.FullName}' => '{targetType.FullName}' because it contains an open generic", DebugLogLevel.Verbose);
                return;
            }

            //Last key in wins, this allows for manual mapping to override things added with behaviors
            bool duplicateKey = false;

            foreach (var _ in _maps.Where(kvp => kvp.Value.RemoveKey(key)))
            {
                duplicateKey = true;
            }

            if (duplicateKey && behavior == DuplicateKeyBehavior.RemoveAll)
            {
                _logger.Debug($"Removing duplicate maps with service key '{key.FullName}'", DebugLogLevel.Verbose);
                return;
            }

            if (!_maps.TryGetValue(targetType.FullName, out TypeMap typeMap))
            {
                _maps[targetType.FullName] = typeMap = new TypeMap(targetType);
            }

            typeMap.AddKey(key, lifetime ?? Settings.DefaultLifetime);
        }
        /// <summary>
        /// Creates a dictionary from a list of KeyValuePairs, using the specified behavior in case of duplicate keys.
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="source"></param>
        /// <param name="duplicateKeyBehavior"></param>
        /// <param name="equalityComparer"></param>
        /// <returns></returns>

        public static Dictionary <TKey, TValue> ToDictionary <TKey, TValue>(this IEnumerable <KeyValuePair <TKey, TValue> > source, DuplicateKeyBehavior duplicateKeyBehavior = DuplicateKeyBehavior.Throw, IEqualityComparer <TKey> equalityComparer = null)
        {
            var dictionary = new Dictionary <TKey, TValue>(equalityComparer);

            foreach (var kvp in source)
            {
                if (kvp.Key == null)
                {
                    continue;
                }

                if (dictionary.ContainsKey(kvp.Key))
                {
                    if (duplicateKeyBehavior == DuplicateKeyBehavior.Throw)
                    {
                        throw new ArgumentException("An item with the same key has already been added");
                    }
                    if (duplicateKeyBehavior == DuplicateKeyBehavior.KeepFirst)
                    {
                        continue;
                    }
                }

                dictionary[kvp.Key] = kvp.Value;
            }

            return(dictionary);
        }