예제 #1
0
        /// <summary>
        /// Determines whether the specified service type is registered by missing type handler.
        /// </summary>
        /// <param name="serviceType">The type of the service.</param>
        /// <returns><c>true</c> if the specified service type is registered; otherwise, <c>false</c>.</returns>
        private bool IsTypeRegisteredByMissingTypeHandler(Type serviceType)
        {
            var missingTypeHandler = MissingType;

            if (missingTypeHandler != null)
            {
                var eventArgs = new MissingTypeEventArgs(serviceType);
                missingTypeHandler(this, eventArgs);

                if (eventArgs.ImplementingInstance != null)
                {
                    Log.Debug("Late registering type '{0}' to instance of type '{1}' via MissingTypeEventArgs.ImplementingInstance", serviceType.FullName, eventArgs.ImplementingInstance.GetType().FullName);

                    RegisterInstance(serviceType, eventArgs.ImplementingInstance, eventArgs.Tag, this);
                    return(true);
                }

                if (eventArgs.ImplementingType != null)
                {
                    Log.Debug("Late registering type '{0}' to type '{1}' via MissingTypeEventArgs.ImplementingType", serviceType.FullName, eventArgs.ImplementingType.FullName);

                    RegisterType(serviceType, eventArgs.ImplementingType, eventArgs.Tag, eventArgs.RegistrationType, true, this, null);
                    return(true);
                }
            }

            return(false);
        }
예제 #2
0
        /// <summary>
        /// Determines whether the specified service type is registered.
        /// </summary>
        /// <param name="serviceType">The type of the service.</param>
        /// <param name="tag">The tag to register the service with. The default value is <c>null</c>.</param>
        /// <returns><c>true</c> if the specified service type is registered; otherwise, <c>false</c>.</returns>
        /// <remarks>Note that the actual implementation lays in the hands of the IoC technique being used.</remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="serviceType"/> is <c>null</c>.</exception>
        public bool IsTypeRegistered(Type serviceType, object tag = null)
        {
            Argument.IsNotNull("serviceType", serviceType);

            var serviceInfo = new ServiceInfo(serviceType, tag);

            lock (_syncObject)
            {
                if (_registeredInstances.ContainsKey(serviceInfo))
                {
                    return(true);
                }

                if (_registeredTypes.ContainsKey(serviceInfo))
                {
                    return(true);
                }

                foreach (var externalContainerKeyValuePair in _externalContainers)
                {
                    var registrationInfo = externalContainerKeyValuePair.Value.GetRegistrationInfo(externalContainerKeyValuePair.Key, serviceType);
                    if (registrationInfo != null)
                    {
                        // Now we know the container, register it as typeof(object), we will re-register as soon as the actual type is known
                        _registeredTypes[serviceInfo] = new RegisteredTypeInfo(serviceType, typeof(object), tag, registrationInfo.RegistrationType, externalContainerKeyValuePair.Value);
                        return(true);
                    }
                }

                // Last resort
                var eventArgs = new MissingTypeEventArgs(serviceType);
                MissingType.SafeInvoke(this, eventArgs);

                if (eventArgs.ImplementingInstance != null)
                {
                    Log.Debug("Late registering type '{0}' to instance of type '{1}' via MissingTypeEventArgs.ImplementingInstance", serviceType.FullName, eventArgs.ImplementingInstance.GetType().FullName);

                    RegisterInstance(serviceType, eventArgs.ImplementingInstance, eventArgs.Tag, this);
                    return(true);
                }

                if (eventArgs.ImplementingType != null)
                {
                    Log.Debug("Late registering type '{0}' to type '{1}' via MissingTypeEventArgs.ImplementingType", serviceType.FullName, eventArgs.ImplementingType.FullName);

                    RegisterType(serviceType, eventArgs.ImplementingType, eventArgs.Tag, eventArgs.RegistrationType, true, this);
                    return(true);
                }
            }

            return(false);
        }
예제 #3
0
        /// <summary>
        /// Determines whether the specified service type is registered.
        /// </summary>
        /// <param name="serviceType">The type of the service.</param>
        /// <param name="tag">The tag to register the service with. The default value is <c>null</c>.</param>
        /// <returns><c>true</c> if the specified service type is registered; otherwise, <c>false</c>.</returns>
        /// <remarks>Note that the actual implementation lays in the hands of the IoC technique being used.</remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="serviceType"/> is <c>null</c>.</exception>
        public bool IsTypeRegistered(Type serviceType, object tag = null)
        {
            Argument.IsNotNull("serviceType", serviceType);

            var serviceInfo = new ServiceInfo(serviceType, tag);

            lock (this)
            {
                if (_registeredInstances.ContainsKey(serviceInfo))
                {
                    return(true);
                }

                if (_registeredTypes.ContainsKey(serviceInfo))
                {
                    return(true);
                }

                // CTL-161, support generic types
                if (serviceType.IsGenericTypeEx())
                {
                    var genericArguments        = serviceType.GetGenericArgumentsEx().ToList();
                    var hasRealGenericArguments = (from genericArgument in genericArguments
                                                   where !string.IsNullOrEmpty(genericArgument.FullName)
                                                   select genericArgument).Any();
                    if (hasRealGenericArguments)
                    {
                        var genericType = serviceType.GetGenericTypeDefinitionEx();
                        var isOpenGenericTypeRegistered = IsTypeRegistered(genericType, tag);
                        if (isOpenGenericTypeRegistered)
                        {
                            Log.Debug("An open generic type '{0}' is registered, registering new closed generic type '{1}' based on the open registration", genericType.GetSafeFullName(), serviceType.GetSafeFullName());

                            var registrationInfo = GetRegistrationInfo(genericType, tag);
                            var finalType        = registrationInfo.ImplementingType.MakeGenericType(genericArguments.ToArray());

                            RegisterType(serviceType, finalType, tag, registrationInfo.RegistrationType);

                            return(true);
                        }
                    }
                }

                // CTL-271 Support generic lists (array specific type)
                // TODO: Can register,

                // Last resort
                var eventArgs = new MissingTypeEventArgs(serviceType);
                MissingType.SafeInvoke(this, eventArgs);

                if (eventArgs.ImplementingInstance != null)
                {
                    Log.Debug("Late registering type '{0}' to instance of type '{1}' via MissingTypeEventArgs.ImplementingInstance", serviceType.FullName, eventArgs.ImplementingInstance.GetType().FullName);

                    RegisterInstance(serviceType, eventArgs.ImplementingInstance, eventArgs.Tag, this);
                    return(true);
                }

                if (eventArgs.ImplementingType != null)
                {
                    Log.Debug("Late registering type '{0}' to type '{1}' via MissingTypeEventArgs.ImplementingType", serviceType.FullName, eventArgs.ImplementingType.FullName);

                    RegisterType(serviceType, eventArgs.ImplementingType, eventArgs.Tag, eventArgs.RegistrationType, true, this, null);
                    return(true);
                }
            }

            return(false);
        }
예제 #4
0
        /// <summary>
        /// Determines whether the specified service type is registered.
        /// </summary>
        /// <param name="serviceType">The type of the service.</param>
        /// <param name="tag">The tag to register the service with. The default value is <c>null</c>.</param>
        /// <returns><c>true</c> if the specified service type is registered; otherwise, <c>false</c>.</returns>
        /// <remarks>Note that the actual implementation lays in the hands of the IoC technique being used.</remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="serviceType"/> is <c>null</c>.</exception>
        public bool IsTypeRegistered(Type serviceType, object tag = null)
        {
            Argument.IsNotNull("serviceType", serviceType);

            var serviceInfo = new ServiceInfo(serviceType, tag);

            lock (this)
            {
                if (_registeredInstances.ContainsKey(serviceInfo))
                {
                    return true;
                }

                if (_registeredTypes.ContainsKey(serviceInfo))
                {
                    return true;
                }

                // CTL-161, support generic types
                if (serviceType.IsGenericTypeEx())
                {
                    var genericArguments = serviceType.GetGenericArgumentsEx().ToList();
                    var hasRealGenericArguments = (from genericArgument in genericArguments
                                                   where !string.IsNullOrEmpty(genericArgument.FullName)
                                                   select genericArgument).Any();
                    if (hasRealGenericArguments)
                    {
                        var genericType = serviceType.GetGenericTypeDefinitionEx();
                        var isOpenGenericTypeRegistered = IsTypeRegistered(genericType, tag);
                        if (isOpenGenericTypeRegistered)
                        {
                            Log.Debug("An open generic type '{0}' is registered, registering new closed generic type '{1}' based on the open registration", genericType.GetSafeFullName(), serviceType.GetSafeFullName());

                            var registrationInfo = GetRegistrationInfo(genericType, tag);
                            var finalType = registrationInfo.ImplementingType.MakeGenericType(genericArguments.ToArray());

                            RegisterType(serviceType, finalType, tag, registrationInfo.RegistrationType);

                            return true;
                        }
                    }
                }

                // CTL-271 Support generic lists (array specific type)
                // TODO: Can register, 

                // Last resort
                var eventArgs = new MissingTypeEventArgs(serviceType);
                MissingType.SafeInvoke(this, eventArgs);

                if (eventArgs.ImplementingInstance != null)
                {
                    Log.Debug("Late registering type '{0}' to instance of type '{1}' via MissingTypeEventArgs.ImplementingInstance", serviceType.FullName, eventArgs.ImplementingInstance.GetType().FullName);

                    RegisterInstance(serviceType, eventArgs.ImplementingInstance, eventArgs.Tag, this);
                    return true;
                }

                if (eventArgs.ImplementingType != null)
                {
                    Log.Debug("Late registering type '{0}' to type '{1}' via MissingTypeEventArgs.ImplementingType", serviceType.FullName, eventArgs.ImplementingType.FullName);

                    RegisterType(serviceType, eventArgs.ImplementingType, eventArgs.Tag, eventArgs.RegistrationType, true, this, null);
                    return true;
                }
            }

            return false;
        }
예제 #5
0
            public void SetsValuesCorrectly()
            {
                var eventArgs = new MissingTypeEventArgs(typeof (ITestInterface));

                Assert.AreEqual(typeof (ITestInterface), eventArgs.InterfaceType);
            }
예제 #6
0
        /// <summary>
        /// Determines whether the specified service type is registered.
        /// </summary>
        /// <param name="serviceType">The type of the service.</param>
        /// <param name="tag">The tag to register the service with. The default value is <c>null</c>.</param>
        /// <returns><c>true</c> if the specified service type is registered; otherwise, <c>false</c>.</returns>
        /// <remarks>Note that the actual implementation lays in the hands of the IoC technique being used.</remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="serviceType"/> is <c>null</c>.</exception>
        public bool IsTypeRegistered(Type serviceType, object tag = null)
        {
            Argument.IsNotNull("serviceType", serviceType);

            var serviceInfo = new ServiceInfo(serviceType, tag);

            lock (_syncObject)
            {
                if (_registeredInstances.ContainsKey(serviceInfo))
                {
                    return true;
                }

                if (_registeredTypes.ContainsKey(serviceInfo))
                {
                    return true;
                }

                foreach (var externalContainerKeyValuePair in _externalContainers)
                {
                    var registrationInfo = externalContainerKeyValuePair.Value.GetRegistrationInfo(externalContainerKeyValuePair.Key, serviceType);
                    if (registrationInfo != null)
                    {
                        // Now we know the container, register it as typeof(object), we will re-register as soon as the actual type is known
                        _registeredTypes[serviceInfo] = new RegisteredTypeInfo(serviceType, typeof(object), tag, registrationInfo.RegistrationType, externalContainerKeyValuePair.Value);
                        return true;
                    }
                }

                // Last resort
                var eventArgs = new MissingTypeEventArgs(serviceType);
                MissingType.SafeInvoke(this, eventArgs);

                if (eventArgs.ImplementingInstance != null)
                {
                    Log.Debug("Late registering type '{0}' to instance of type '{1}' via MissingTypeEventArgs.ImplementingInstance", serviceType.FullName, eventArgs.ImplementingInstance.GetType().FullName);

                    RegisterInstance(serviceType, eventArgs.ImplementingInstance, eventArgs.Tag, this);
                    return true;
                }

                if (eventArgs.ImplementingType != null)
                {
                    Log.Debug("Late registering type '{0}' to type '{1}' via MissingTypeEventArgs.ImplementingType", serviceType.FullName, eventArgs.ImplementingType.FullName);

                    RegisterType(serviceType, eventArgs.ImplementingType, eventArgs.Tag, eventArgs.RegistrationType, true, this);
                    return true;
                }
            }

            return false;
        }