Catel.IoC.IoCFactory.CreateServiceLocator C# (CSharp) Метод

CreateServiceLocator() публичный статический Метод

Creates a service locator with all the customized components.
public static CreateServiceLocator ( bool initializeServiceLocator = true ) : IServiceLocator
initializeServiceLocator bool if set to true, the will be initialized using the interface.
Результат IServiceLocator
        public static IServiceLocator CreateServiceLocator(bool initializeServiceLocator = true)
        {
            lock (_lockObject)
            {
                var serviceLocator = CreateServiceLocatorFunc();
                if (serviceLocator == null)
                {
                    throw Log.ErrorAndCreateException<Exception>("Failed to create the IServiceLocator instance using the factory method");
                }

                if (!serviceLocator.IsTypeRegistered<IDependencyResolver>())
                {
                    var dependencyResolver = CreateDependencyResolverFunc(serviceLocator);
                    if (dependencyResolver == null)
                    {
                        throw Log.ErrorAndCreateException<Exception>("Failed to create the IDependencyResolver instance using the factory method");
                    }

                    serviceLocator.RegisterInstance(typeof(IDependencyResolver), dependencyResolver);
                }

                if (!serviceLocator.IsTypeRegistered<ITypeFactory>())
                {
                    var typeFactory = CreateTypeFactoryFunc(serviceLocator);
                    if (typeFactory == null)
                    {
                        throw Log.ErrorAndCreateException<Exception>("Failed to create the ITypeFactory instance using the factory method");
                    }

                    serviceLocator.RegisterInstance(typeof(ITypeFactory), typeFactory);
                }

                if (initializeServiceLocator)
                {
                    if (_serviceLocatorInitializers == null)
                    {
                        _serviceLocatorInitializers = new List<Type>(TypeCache.GetTypes(x => !x.IsInterfaceEx() & x.ImplementsInterfaceEx<IServiceLocatorInitializer>()));
                    }

                    foreach (var serviceLocatorInitializer in _serviceLocatorInitializers)
                    {
                        try
                        {
                            var initializer = (IServiceLocatorInitializer)Activator.CreateInstance(serviceLocatorInitializer);
                            initializer.Initialize(serviceLocator);
                        }
                        catch (Exception ex)
                        {
                            throw Log.ErrorAndCreateException<Exception>(ex, "Failed to initialize service locator using initializer '{0}'", serviceLocatorInitializer.GetSafeFullName(false));
                        }
                    }
                }

                return serviceLocator;
            }
        }
        #endregion

Usage Example

Пример #1
0
        /// <summary>
        /// Updates the default components.
        /// <para />
        /// This method should be called when any of the factory methods has been changed.
        /// </summary>
        /// <exception cref="System.Exception">The method fails to create the <see cref="IServiceLocator"/> using the factory.</exception>
        public static void UpdateDefaultComponents()
        {
            Log.Info("Updating default components");

            // Don't initialize the first service locator (we are still loading assemblies at that time)
            bool initializeServiceLocator = (_defaultServiceLocator != null);
            var  serviceLocator           = IoCFactory.CreateServiceLocator(initializeServiceLocator);

            lock (_lockObject)
            {
                DefaultServiceLocator     = serviceLocator;
                DefaultDependencyResolver = serviceLocator.ResolveType <IDependencyResolver>();
                DefaultTypeFactory        = serviceLocator.ResolveType <ITypeFactory>();
            }
        }