private bool IsRegistered(Type type, string instanceName = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var key = new ServiceMappingKey(type, instanceName);

            return(_registeredServices.ContainsKey(key));
        }
        public object Resolve(Type type, string instanceName = null)
        {
            if (!IsRegistered(type, instanceName))
            {
                return(null);
            }
            ServiceMappingKey key = new ServiceMappingKey(type, instanceName);
            object            instance;

            if (_instancesCache.TryGetValue(key, out instance))
            {
                return(instance);
            }
            instance = Activator.CreateInstance(_registeredServices[key]);
            _instancesCache.Add(key, instance);
            return(instance);
        }
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            ServiceMappingKey compareTo = obj as ServiceMappingKey;

            if (ReferenceEquals(this, compareTo))
            {
                return(true);
            }

            if (compareTo == null)
            {
                return(false);
            }

            return(Type.Equals(compareTo.Type) &&
                   string.Equals(InstanceName, compareTo.InstanceName, StringComparison.CurrentCultureIgnoreCase));
        }
        public void ReplaceMapping(Type from, Type to)
        {
            if (to == null)
            {
                throw new ArgumentNullException("to");
            }

            if (!from.GetTypeInfo().IsAssignableFrom(to.GetTypeInfo()))
            {
                string errorMessage = string.Format("Error trying to register the instance: '{0}' is not assignable from '{1}'",
                                                    from.FullName, to.FullName);

                throw new InvalidOperationException(errorMessage);
            }

            ServiceMappingKey key = new ServiceMappingKey(from, null);

            if (_registeredServices.ContainsKey(key))
            {
                _registeredServices[key] = to;
                return;
            }
            _registeredServices.Add(key, to);
        }
        public void Register(Type from, Type to, string instanceName = null)
        {
            if (to == null)
            {
                throw new ArgumentNullException("to");
            }

            if (!from.GetTypeInfo().IsAssignableFrom(to.GetTypeInfo()))
            {
                string errorMessage = string.Format("Error trying to register the instance: '{0}' is not assignable from '{1}'",
                                                    from.FullName, to.FullName);

                throw new InvalidOperationException(errorMessage);
            }

            ServiceMappingKey key = new ServiceMappingKey(from, instanceName);

            if (_registeredServices.ContainsKey(key))
            {
                const string errorMessageFormat = "The requested mapping already exists - {0}";
                throw new InvalidOperationException(string.Format(errorMessageFormat, key.ToString()));
            }
            _registeredServices.Add(key, to);
        }