Beispiel #1
0
        /// <summary>
        /// Creates the service instance.
        /// </summary>
        /// <param name="registration">The registration.</param>
        /// <returns>The service instance.</returns>
        private object CreateServiceInstance(ServiceLocatorRegistration registration)
        {
            object instance;

            if (SupportDependencyInjection)
            {
                instance = TypeFactory.CreateInstance(registration.ImplementingType);
            }
            else
            {
                instance = TypeFactory.CreateInstanceUsingActivator(registration.ImplementingType);
            }

            if (instance != null)
            {
                var handler = TypeInstantiated;
                if (handler != null)
                {
                    handler(this, new TypeInstantiatedEventArgs(registration.DeclaringType, registration.ImplementingType,
                                                                registration.Tag, registration.RegistrationType));
                }
            }

            return(instance);
        }
Beispiel #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>
        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);
        }
Beispiel #3
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);
        }
Beispiel #4
0
        /// <summary>
        /// Creates the service instance.
        /// </summary>
        /// <param name="registration">The registration.</param>
        /// <returns>The service instance.</returns>
        private object CreateServiceInstance(ServiceLocatorRegistration registration)
        {
            object instance = _typeFactory.CreateInstance(registration.ImplementingType);

            if (instance == null)
            {
                ThrowTypeNotRegisteredException(registration.DeclaringType, "Failed to instantiate the type using the TypeFactory. Check if the required dependencies are registered as well or that the type has a valid constructor that can be used.");
            }

            var handler = TypeInstantiated;

            if (handler != null)
            {
                handler(this, new TypeInstantiatedEventArgs(registration.DeclaringType, registration.ImplementingType,
                                                            registration.Tag, registration.RegistrationType));
            }

            return(instance);
        }
Beispiel #5
0
 public RegisteredInstanceInfo(ServiceLocatorRegistration registration, object instance)
     : base(registration.DeclaringType, registration.ImplementingType, registration.Tag, registration.RegistrationType, registration.CreateServiceFunc)
 {
     ImplementingInstance = instance;
 }
        /// <summary>
        /// Creates the service instance.
        /// </summary>
        /// <param name="registration">The registration.</param>
        /// <returns>The service instance.</returns>
        private object CreateServiceInstance(ServiceLocatorRegistration registration)
        {
            object instance = _typeFactory.CreateInstance(registration.ImplementingType);
            if (instance == null)
            {
                ThrowTypeNotRegisteredException(registration.DeclaringType, "Failed to instantiate the type using the TypeFactory. Check if the required dependencies are registered as well or that the type has a valid constructor that can be used.");
            }

            var handler = TypeInstantiated;
            if (handler != null)
            {
                handler(this, new TypeInstantiatedEventArgs(registration.DeclaringType, registration.ImplementingType,
                    registration.Tag, registration.RegistrationType));
            }

            return instance;
        }
        /// <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())
            {
                throw Log.ErrorAndCreateException<InvalidOperationException>("Cannot register interface type '{0}' as implementation, make sure to specify an actual class", serviceImplementationType.GetSafeFullName());
            }

            /* 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);
        }
        /// <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);
        }
 public RegisteredInstanceInfo(ServiceLocatorRegistration registration, object instance)
     : base(registration.DeclaringType, registration.ImplementingType, registration.Tag, registration.RegistrationType, registration.CreateServiceFunc)
 {
     ImplementingInstance = instance;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceLocatorRegistrationGroup"/> class.
        /// </summary>
        /// <param name="entryRegistration">The entry registration.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="entryRegistration"/> is <c>null</c>.</exception>
        public ServiceLocatorRegistrationGroup(ServiceLocatorRegistration entryRegistration)
        {
            Argument.IsNotNull("entryRegistration", entryRegistration);

            EntryRegistration = entryRegistration;
        }
Beispiel #11
0
        /// <summary>
        /// Default create service function, uses <see cref="_typeFactory"/> to create a service instance.
        /// </summary>
        /// <param name="registration">The registration.</param>
        /// <returns>The service instance.</returns>
        private object DefaultCreateServiceFunc(ServiceLocatorRegistration registration)
        {
            var instance = _typeFactory.CreateInstanceWithTag(registration.ImplementingType, registration.Tag);

            return(instance);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceLocatorRegistrationGroup"/> class.
        /// </summary>
        /// <param name="entryRegistration">The entry registration.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="entryRegistration"/> is <c>null</c>.</exception>
        public ServiceLocatorRegistrationGroup(ServiceLocatorRegistration entryRegistration)
        {
            Argument.IsNotNull("entryRegistration", entryRegistration);

            EntryRegistration = entryRegistration;
        }
Beispiel #13
0
        /// <summary>
        /// Creates the service instance.
        /// </summary>
        /// <param name="registration">The registration.</param>
        /// <returns>The service instance.</returns>
        private object CreateServiceInstance(ServiceLocatorRegistration registration)
        {
            object instance;

            if (SupportDependencyInjection)
            {
                instance = TypeFactory.CreateInstance(registration.ImplementingType);
            }
            else
            {
                instance = TypeFactory.CreateInstanceUsingActivator(registration.ImplementingType);
            }

            if (instance != null)
            {
                var handler = TypeInstantiated;
                if (handler != null)
                {
                    handler(this, new TypeInstantiatedEventArgs(registration.DeclaringType, registration.ImplementingType,
                        registration.Tag, registration.RegistrationType));
                }
            }

            return instance;
        }