Esempio n. 1
0
        /// <summary>
        /// Registers a specific instance of an service.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="instance">The specific instance to register.</param>
        /// <param name="tag">The tag to register the service with. The default value is <c>null</c>.</param>
        /// <param name="originalContainer">The original container where the instance was found in.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="serviceType"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="instance"/> is <c>null</c>.</exception>
        private void RegisterInstance(Type serviceType, object instance, object tag, object originalContainer)
        {
            Argument.IsNotNull("serviceType", serviceType);
            Argument.IsNotNull("instance", instance);

            Log.Debug("Registering type '{0}' to instance of type '{1}'", serviceType.FullName, instance.GetType().FullName);

            var registeredTypeInfo = new ServiceLocatorRegistration(serviceType, instance.GetType(), tag, RegistrationType.Singleton, x => instance);

            lock (this)
            {
                var serviceInfo = new ServiceInfo(serviceType, tag);

                if (_registeredTypes.ContainsKey(serviceInfo))
                {
                    // Re-use previous subscription
                    registeredTypeInfo = _registeredTypes[serviceInfo];
                }
                else
                {
                    _registeredTypes[serviceInfo] = registeredTypeInfo;
                }

                _registeredInstances[serviceInfo] = new RegisteredInstanceInfo(registeredTypeInfo, instance);
            }

            TypeRegistered.SafeInvoke(this, new TypeRegisteredEventArgs(registeredTypeInfo.DeclaringType, registeredTypeInfo.ImplementingType, tag, RegistrationType.Singleton));

            Log.Debug("Registered type '{0}' to instance of type '{1}'", serviceType.FullName, instance.GetType().FullName);
        }
Esempio n. 2
0
        /// <summary>
        /// Registers a specific instance of an service.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="instance">The specific instance to register.</param>
        /// <param name="tag">The tag to register the service with. The default value is <c>null</c>.</param>
        /// <param name="originalContainer">The original container where the instance was found in.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="serviceType"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="instance"/> is <c>null</c>.</exception>
        internal void RegisterInstance(Type serviceType, object instance, object tag, object originalContainer)
        {
            Argument.IsNotNull("serviceType", serviceType);
            Argument.IsNotNull("instance", instance);

            Log.Debug("Registering type '{0}' to instance of type '{1}'", serviceType.FullName, instance.GetType().FullName);

            if (originalContainer == null)
            {
                originalContainer = this;
            }

            var registeredTypeInfo = new RegisteredTypeInfo(serviceType, instance.GetType(), tag, RegistrationType.Singleton, originalContainer);

            lock (_syncObject)
            {
                var serviceInfo = new ServiceInfo(serviceType, tag);
                _registeredInstances[serviceInfo] = new RegisteredInstanceInfo(serviceType, instance, RegistrationType.Singleton, tag, originalContainer);
                _registeredTypes[serviceInfo]     = registeredTypeInfo;

                if (AutomaticallyKeepContainersSynchronized)
                {
                    ExportToExternalContainers();
                }
            }

            TypeRegistered.SafeInvoke(this, new TypeRegisteredEventArgs(registeredTypeInfo.DeclaringType, registeredTypeInfo.ImplementingType, tag, RegistrationType.Singleton));

            Log.Debug("Registered type '{0}' to instance of type '{1}'", serviceType.FullName, instance.GetType().FullName);
        }
Esempio n. 3
0
        /// <summary>
        /// Registers the given type into the given registry, if possible.
        /// </summary>
        /// <param name="register">The base type or interface that the given Type inherits or implements.</param>
        /// <param name="type">The type to register.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="register"/> or <paramref name="type"/> is null.</exception>
        public void RegisterType(TypeInfo register, TypeInfo type)
        {
            if (register == null)
            {
                throw new ArgumentNullException(nameof(register));
            }

            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            // Only register types that can be created
            // This excludes generic types, abstract classes, and interfaces.
            if (this.CanCreateInstance(type))
            {
                // Ensure the register was in fact registered
                RegisterTypeRegister(register);

                // Duplicates can cause minor issues
                if (!TypeRegistry[register].Contains(type))
                {
                    TypeRegistry[register].Add(type);
                }

                TypeRegistered?.Invoke(this, new TypeRegisteredEventArgs {
                    BaseType = register, RegisteredType = type
                });
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Registers the specific implementing type for the service type.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="serviceImplementationType">Type of the implementing.</param>
        /// <param name="tag">The tag to register the service with. The default value is <c>null</c>.</param>
        /// <param name="registrationType">The registration type.</param>
        /// <param name="registerIfAlreadyRegistered">if set to <c>true</c>, an older type registration is overwritten by this new one.</param>
        /// <param name="originalContainer">The original container where the type was found in.</param>
        /// <param name="createServiceFunc">The create service function.</param>
        /// <exception cref="System.InvalidOperationException"></exception>
        /// <exception cref="ArgumentNullException">The <paramref name="serviceType" /> is <c>null</c>.</exception>
        private void RegisterType(Type serviceType, Type serviceImplementationType, object tag, RegistrationType registrationType, bool registerIfAlreadyRegistered, object originalContainer, Func <ServiceLocatorRegistration, object> createServiceFunc)
        {
            Argument.IsNotNull("serviceType", serviceType);

            if (serviceImplementationType == null)
            {
                // Dynamic late-bound type
                serviceImplementationType = typeof(LateBoundImplementation);
            }

            if (serviceImplementationType.IsInterfaceEx())
            {
                string error = string.Format("Cannot register interface type '{0}' as implementation, make sure to specify an actual class", serviceImplementationType.GetSafeFullName());
                Log.Error(error);
                throw new InvalidOperationException(error);
            }

            /* TODO: This code have to be here to ensure the right usage of non-generic overloads of register methods.
             * TODO: If it is finally accepted then remove it from ServiceLocatorAutoRegistrationManager
             * if (serviceImplementationType.IsAbstractEx() || !serviceType.IsAssignableFromEx(serviceImplementationType))
             * {
             *  string message = string.Format("The type '{0}' is abstract or can't be registered as '{1}'", serviceImplementationType, serviceType);
             *  Log.Error(message);
             *  throw new InvalidOperationException(message);
             * }
             */

            // Outside lock scope for event
            ServiceLocatorRegistration registeredTypeInfo = null;

            lock (this)
            {
                if (!registerIfAlreadyRegistered && IsTypeRegistered(serviceType, tag))
                {
                    //Log.Debug("Type '{0}' already registered, will not overwrite registration", serviceType.FullName);
                    return;
                }

                var serviceInfo = new ServiceInfo(serviceType, tag);
                if (_registeredInstances.ContainsKey(serviceInfo))
                {
                    _registeredInstances.Remove(serviceInfo);
                }

                Log.Debug("Registering type '{0}' to type '{1}'", serviceType.FullName, serviceImplementationType.FullName);

                registeredTypeInfo = new ServiceLocatorRegistration(serviceType, serviceImplementationType, tag, registrationType,
                                                                    x => (createServiceFunc != null) ? createServiceFunc(x) : CreateServiceInstance(x));
                _registeredTypes[serviceInfo] = registeredTypeInfo;
            }

            TypeRegistered.SafeInvoke(this, new TypeRegisteredEventArgs(registeredTypeInfo.DeclaringType, registeredTypeInfo.ImplementingType, tag, registeredTypeInfo.RegistrationType));

            Log.Debug("Registered type '{0}' to type '{1}'", serviceType.FullName, serviceImplementationType.FullName);
        }
Esempio n. 5
0
        /// <summary>
        /// Registers the specific implementing type for the service type.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="serviceImplementationType">Type of the implementing.</param>
        /// <param name="tag">The tag to register the service with. The default value is <c>null</c>.</param>
        /// <param name="registrationType">The registration type.</param>
        /// <param name="registerIfAlreadyRegistered">if set to <c>true</c>, an older type registration is overwritten by this new one.</param>
        /// <param name="originalContainer">The original container where the type was found in.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="serviceType"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="serviceImplementationType"/> is <c>null</c>.</exception>
        internal void RegisterType(Type serviceType, Type serviceImplementationType, object tag, RegistrationType registrationType, bool registerIfAlreadyRegistered, object originalContainer)
        {
            Argument.IsNotNull("serviceType", serviceType);
            Argument.IsNotNull("implementingType", serviceImplementationType);

            /* TODO: This code have to be here to ensure the right usage of non-generic overloads of register methods.
             * TODO: If it is finally accepted then remove it from ServiceLocatorAutoRegistrationManager
             * if (serviceImplementationType.IsAbstractEx() || !serviceType.IsAssignableFromEx(serviceImplementationType))
             * {
             *  string message = string.Format("The type '{0}' is abstract or can't be registered as '{1}'", serviceImplementationType, serviceType);
             *  Log.Error(message);
             *  throw new InvalidOperationException(message);
             * }
             */

            // Outside lock scope for event
            RegisteredTypeInfo registeredTypeInfo = null;

            lock (_syncObject)
            {
                if (!registerIfAlreadyRegistered && IsTypeRegistered(serviceType, tag))
                {
                    //Log.Debug("Type '{0}' already registered, will not overwrite registration", serviceType.FullName);
                    return;
                }

                var serviceInfo = new ServiceInfo(serviceType, tag);
                if (_registeredInstances.ContainsKey(serviceInfo))
                {
                    _registeredInstances.Remove(serviceInfo);
                }

                Log.Debug("Registering type '{0}' to type '{1}'", serviceType.FullName, serviceImplementationType.FullName);

                registeredTypeInfo            = new RegisteredTypeInfo(serviceType, serviceImplementationType, tag, registrationType, originalContainer);
                _registeredTypes[serviceInfo] = registeredTypeInfo;

                if (AutomaticallyKeepContainersSynchronized)
                {
                    ExportToExternalContainers();
                }
            }

            TypeRegistered.SafeInvoke(this, new TypeRegisteredEventArgs(registeredTypeInfo.DeclaringType, registeredTypeInfo.ImplementingType, tag, registeredTypeInfo.RegistrationType));

            Log.Debug("Registered type '{0}' to type '{1}'", serviceType.FullName, serviceImplementationType.FullName);
        }