/// <summary>
 /// 指定具体的提供程序
 /// </summary>
 /// <param name="value"></param>
 public static void SetProvider(IObjectContainerFactory value)
 {
     if (value == null)
     {
         throw new ArgumentNullException("value");
     }
     _provider = value;
 }
Esempio n. 2
0
 /// <summary>
 /// 创建一个独立的的 IOC 容器
 /// </summary>
 /// <returns></returns>
 public static IObjectContainer CreateContainer()
 {
     if (_provider == null)
     {
         lock (lockobject)
         {
             if (_provider == null)
             {
                 _provider = new DefaultObjectContainerFactory();
             }
         }
     }
     return(_provider.CreateContainer());
 }
Esempio n. 3
0
        public App(IConfigSource configSource)
        {
            #region Check whether the required or mandatory EApp configuration has been configured in config file.

            if (configSource == null)
            {
                throw new ArgumentNullException("configSource");
            }

            if (configSource.Config == null)
            {
                throw new ConfigException("EAppConfigSource has not been defined in the ConfigSource instance.");
            }

            if (configSource.Config.ObjectContainer == null)
            {
                throw new ConfigException("No ObjectContainer instance has been specified in the EAppConfigSource.");
            }

            #endregion

            this.configSource = configSource;

            #region If default used object container (i.e. default used IOC component) has been configured in config file then create it by Activator.CreateInstance

            string objectContainerFactoryProviderName = configSource.Config.ObjectContainer.Provider;

            if (string.IsNullOrEmpty(objectContainerFactoryProviderName) ||
                objectContainerFactoryProviderName.IsNullOrWhiteSpace())
            {
                throw new ConfigException("The ObjectContainer provider has not been defined in the ConfigSource.");
            }

            Type objectContainerFactoryType = Type.GetType(objectContainerFactoryProviderName);

            if (objectContainerFactoryType == null)
            {
                throw new InfrastructureException("The ObjectContainer defined by type {0} doesn't exist.", objectContainerFactoryProviderName);
            }

            IObjectContainerFactory currentObjectContainerFactory = (IObjectContainerFactory)Activator.CreateInstance(objectContainerFactoryType);

            if (currentObjectContainerFactory != null)
            {
                this.objectContainer = currentObjectContainerFactory.ObjectContainer;
            }

            // if object container need to be initialized from config file. E.g. Unity from config file.
            if (this.configSource.Config.ObjectContainer.InitFromConfigFile)
            {
                string sectionName = this.configSource.Config.ObjectContainer.SectionName;

                string configFilePath = this.configSource.Config.ObjectContainer.File;

                if (!string.IsNullOrEmpty(sectionName) && !sectionName.IsNullOrWhiteSpace())
                {
                    this.objectContainer.InitializeFromConfigFile(sectionName, configFilePath);
                }
                else
                {
                    throw new ConfigException("Section name for the ObjectContainer configuration should also be provided when InitFromConfigFile has been set to true.");
                }
            }

            #endregion

            #region if resource managers have been configured in config file then create these resource managers

            if (configSource.Config.ResourceManagers != null &&
                configSource.Config.ResourceManagers.ElementInformation.IsPresent)
            {
                foreach (NameTypeElement resourceItem in configSource.Config.ResourceManagers)
                {
                    string resourceName = resourceItem.Name;

                    string resourceAssemblyName = resourceItem.Type;

                    if (!string.IsNullOrEmpty(resourceName) &&
                        !string.IsNullOrEmpty(resourceAssemblyName))
                    {
                        Type resourceAssemblyType = Type.GetType(resourceAssemblyName);

                        if (resourceAssemblyType == null)
                        {
                            continue;
                        }

                        this.ObjectContainer.RegisterType <IResourceManager>(resourceAssemblyType, resourceName);

                        this.resourceManagers.Add(resourceName, this.ObjectContainer.Resolve <IResourceManager>(resourceName));
                    }
                }
            }

            #endregion

            #region If module plugin architecture has been configured in config file then create plugin host, plugin provider and service provider

            if (configSource.Config.PluginContainer != null &&
                configSource.Config.PluginContainer.ElementInformation.IsPresent &&
                configSource.Config.PluginContainer.Host != null &&
                configSource.Config.PluginContainer.Host.ElementInformation.IsPresent)
            {
                if (configSource.Config.PluginContainer.Host.ServiceProvider == null)
                {
                    throw new ConfigException("Plugin Service Provider configuration has not been initialized in the ConfigSource instance.");
                }

                if (configSource.Config.PluginContainer.Host.PluginProvider == null)
                {
                    throw new ConfigException("Plugin Provider configuration has not been initialized in the ConfigSource instance.");
                }

                string hostTypeName = configSource.Config.PluginContainer.Host.Provider;

                if (string.IsNullOrEmpty(hostTypeName) ||
                    hostTypeName.IsNullOrWhiteSpace())
                {
                    throw new ConfigException("The Plugin Host Type Name has not been defined in the Plugin Config Source.");
                }

                string pluginProviderTypeName = configSource.Config.PluginContainer.Host.PluginProvider.Provider;

                if (string.IsNullOrEmpty(pluginProviderTypeName) ||
                    pluginProviderTypeName.IsNullOrWhiteSpace())
                {
                    throw new ConfigException("The Plugin Provider Type Name has not been defined in the Plugin Config Source.");
                }

                string serviceProviderFacotryTypeName = configSource.Config.PluginContainer.Host.ServiceProvider.Provider;

                if (string.IsNullOrEmpty(serviceProviderFacotryTypeName) ||
                    serviceProviderFacotryTypeName.IsNullOrWhiteSpace())
                {
                    throw new ConfigException("The Plugin Service Provider Type Name has not been defined in the Plugin Config Source.");
                }

                Type hostType = Type.GetType(hostTypeName);

                if (hostType == null)
                {
                    throw new InfrastructureException("The Plugin Host defined by type {0} doesn't exist.", hostTypeName);
                }

                Type pluginProviderType = Type.GetType(pluginProviderTypeName);

                if (pluginProviderType == null)
                {
                    throw new InfrastructureException("The Plugin Provider defined by type {0} doesn't exist.", pluginProviderTypeName);
                }

                Type serviceProviderFacotryType = Type.GetType(serviceProviderFacotryTypeName);

                if (serviceProviderFacotryType == null)
                {
                    throw new InfrastructureException("The Plugin Service Provider defined by type {0} doesn't exist.", serviceProviderFacotryTypeName);
                }

                IPluginProvider pluginProvider = (IPluginProvider)Activator.CreateInstance(pluginProviderType);

                IPluginServiceProviderFactory serviceProviderFactory = (IPluginServiceProviderFactory)Activator.CreateInstance(serviceProviderFacotryType, new object[] { pluginProvider });

                this.pluginHost = (IHost)Activator.CreateInstance(hostType, new object[] { pluginProvider, serviceProviderFactory });

                // If plugin types have been configured in config file then register them into the object container for IOC.
                // e.g. when clicking a button to load a module plugin UI get the plugin instance by name from object container.
                if (configSource.Config.PluginContainer != null &&
                    configSource.Config.PluginContainer.ElementInformation.IsPresent &&
                    configSource.Config.PluginContainer.PluginRegisters != null &&
                    configSource.Config.PluginContainer.PluginRegisters.Count > 0)
                {
                    foreach (PluginRegisterElement pluginRegisterItem in configSource.Config.PluginContainer.PluginRegisters)
                    {
                        string pluginName = pluginRegisterItem.Name;

                        string pluginTypeName = pluginRegisterItem.Type;

                        if (!string.IsNullOrEmpty(pluginName) &&
                            !string.IsNullOrEmpty(pluginTypeName))
                        {
                            Type pluginAssemblyType = Type.GetType(pluginTypeName);

                            if (pluginAssemblyType == null)
                            {
                                continue;
                            }

                            this.ObjectContainer.RegisterType(typeof(IPlugin), pluginAssemblyType, pluginName);
                        }
                    }
                }
            }

            #endregion

            #region If windows mvc controller list have been configured in config file then register controller type

            if (configSource.Config.WindowsMvc != null &&
                configSource.Config.WindowsMvc.ElementInformation.IsPresent &&
                configSource.Config.WindowsMvc.Controllers != null)
            {
                foreach (NameTypeElement controllerRegisterItem in configSource.Config.WindowsMvc.Controllers)
                {
                    string controllerName = controllerRegisterItem.Name;

                    string controllerTypeName = controllerRegisterItem.Type;

                    if (!string.IsNullOrEmpty(controllerName) &&
                        !string.IsNullOrEmpty(controllerTypeName))
                    {
                        Type controllerAssemblyType = Type.GetType(controllerTypeName);

                        if (controllerAssemblyType == null)
                        {
                            continue;
                        }

                        this.ObjectContainer.RegisterType(typeof(IController), controllerAssemblyType, controllerName);
                    }
                }
            }

            #endregion
        }
 public ObjectContainerManager(SerializationInfo info, StreamingContext context)
 {
     _objectContainerStore   = (ObjectContainerStore)info.GetValue("objectContainerStore", typeof(ObjectContainerStore));
     _objectContainerFactory = (ObjectContainerFactory)info.GetValue("objectContainerFactory", typeof(ObjectContainerFactory));
     _keyFactory             = (KeyGenerator)info.GetValue("keyfactory", typeof(KeyGenerator));
 }
 public ObjectContainerManager(IObjectContainerStore objectContainerStore, IObjectContainerFactory objectContainerFactory, IKeyFactory keyFactory)
 {
     _objectContainerStore   = objectContainerStore;
     _objectContainerFactory = objectContainerFactory;
     _keyFactory             = keyFactory;
 }
Esempio n. 6
0
 /// <summary>
 /// 指定具体的提供程序
 /// </summary>
 /// <param name="value"></param>
 public static void SetProvider(IObjectContainerFactory value)
 {
     if (value == null) throw new ArgumentNullException("value");
     _provider = value;
 }
Esempio n. 7
0
        public App(IConfigSource configSource)
        {
            if (configSource == null)
            {
                throw new ArgumentNullException("configSource");
            }

            if (configSource.Config == null)
            {
                throw new ConfigException("EAppConfigSource has not been defined in the ConfigSource instance.");
            }

            if (configSource.Config.ObjectContainer == null)
            {
                throw new ConfigException("No ObjectContainer instance has been specified in the EAppConfigSource.");
            }

            this.configSource = configSource;

            string objectContainerProviderName = configSource.Config.ObjectContainer.Provider;

            if (string.IsNullOrEmpty(objectContainerProviderName) ||
                string.IsNullOrWhiteSpace(objectContainerProviderName))
            {
                throw new ConfigException("The ObjectContainer provider has not been defined in the ConfigSource.");
            }

            string objectContainerAssemblyTypeName = this.configSource.Config.ObjectContainer.Provider;

            if (string.IsNullOrEmpty(objectContainerAssemblyTypeName) ||
                string.IsNullOrWhiteSpace(objectContainerAssemblyTypeName))
            {
                throw new ConfigException("The ObjectContainer Type Name has not been defined in the ConfigSource.");
            }

            string[] objectContainerAssemblyTypeNameArray = objectContainerAssemblyTypeName.Split(
                new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            if (objectContainerAssemblyTypeNameArray.Length < 2)
            {
                throw new ConfigException("The ObjectContainer Type Name error in the ConfigSource.");
            }

            string objectContainerTypeName     = objectContainerAssemblyTypeNameArray[0];
            string objectContainerAssemblyName = objectContainerAssemblyTypeNameArray[1];

            Assembly objectContainerAssembly = Assembly.Load(new AssemblyName(objectContainerAssemblyName));

            Type objectContainerType = objectContainerAssembly.GetType(objectContainerTypeName);

            if (objectContainerType == null)
            {
                throw new InfrastructureException("The ObjectContainer defined by type {0} doesn't exist.", objectContainerAssemblyTypeName);
            }

            IObjectContainerFactory currentObjectContainerFactory = (IObjectContainerFactory)Activator.CreateInstance(objectContainerType);

            if (currentObjectContainerFactory != null)
            {
                this.objectContainer = currentObjectContainerFactory.ObjectContainer;
            }

            if (this.configSource.Config.ObjectContainer.InitFromConfigFile)
            {
                string sectionName = this.configSource.Config.ObjectContainer.SectionName;

                if (!string.IsNullOrEmpty(sectionName) && !string.IsNullOrWhiteSpace(sectionName))
                {
                    this.objectContainer.InitializeFromConfigFile(sectionName);
                }
                else
                {
                    throw new ConfigException("Section name for the ObjectContainer configuration should also be provided when InitFromConfigFile has been set to true.");
                }
            }
        }
Esempio n. 8
0
 public Db4OSession(IObjectContainerFactory objectContainerFactory)
 {
     object_container_in_session = objectContainerFactory.create();
 }