Пример #1
0
        /// <summary>
        /// 根据应用程序配置,创建文本序列化器。
        /// </summary>
        /// <param name="configName">应用程序配置项的名称。</param>
        /// <returns><paramref name="configName"/>缺省时,如果应用程序未配置,则为 <see cref="MemoryCacheManager"/>,否则为配置项对应的 <see cref="ICacheManager"/> 实例。</returns>
        public static ITextSerializer CreateSerializer(string configName = null)
        {
            ITextSerializer           serializer;
            IConfigurationSettingItem setting = null;
            var section = ConfigurationUnity.GetSection <SerializerConfigurationSection>();

            if (section != null && section.Factory != null)
            {
                serializer = section.Factory.CreateInstance(configName) as ITextSerializer;
                if (serializer != null)
                {
                    return(serializer);
                }
            }

            if (string.IsNullOrEmpty(configName))
            {
                if (section == null || (setting = section.GetDefault()) == null)
                {
                    return(null);
                }
            }
            else if (section != null)
            {
                setting = section.GetSetting(configName);
            }

            if (setting == null)
            {
                return(null);
            }

            return(ConfigurationUnity.CreateInstance <SerializerConfigurationSetting, ITextSerializer>(setting, s => s.SerializerType));
        }
Пример #2
0
        /// <summary>
        /// 根据应用程序配置,创建日志管理器。
        /// </summary>
        /// <param name="configName">应用程序配置项的名称。</param>
        /// <returns><paramref name="configName"/>缺省时,如果应用程序未配置,则为 <see cref="DefaultLogger"/>,否则为配置项对应的 <see cref="ILogger"/> 实例。</returns>
        public static ILogger CreateLogger(string configName = null)
        {
            ILogger logger;
            IConfigurationSettingItem setting = null;
            var section = ConfigurationUnity.GetSection <LoggingConfigurationSection>();

            if (section != null && section.Factory != null)
            {
                logger = section.Factory.CreateInstance(configName) as ILogger;
                if (logger != null)
                {
                    return(logger);
                }
            }

            if (string.IsNullOrEmpty(configName))
            {
                if (section == null || (setting = section.GetDefault()) == null)
                {
                    return(DefaultLogger.Instance);
                }
            }
            else if (section != null)
            {
                setting = section.GetSetting(configName);
            }

            if (setting == null)
            {
                return(null);
            }

            return(ConfigurationUnity.CreateInstance <LoggingConfigurationSetting, ILogger>(setting, s => s.LogType));
        }
Пример #3
0
        /// <summary>
        /// 根据应用程序配置,创建订阅管理器。
        /// </summary>
        /// <param name="configName">应用程序配置项的名称。</param>
        /// <returns><paramref name="configName"/>缺省时,如果应用程序未配置,则为 <see cref="DefaultSubscribeManager"/>,否则为配置项对应的 <see cref="ISubscribeManager"/> 实例。</returns>
        public static ISubscribeManager CreateManager(string configName = null)
        {
            ISubscribeManager         manager;
            IConfigurationSettingItem setting = null;
            var section = ConfigurationUnity.GetSection <SubscribeConfigurationSection>();

            if (section != null && section.Factory != null)
            {
                manager = section.Factory.CreateInstance(configName) as ISubscribeManager;
                if (manager != null)
                {
                    return(manager);
                }
            }

            if (string.IsNullOrEmpty(configName))
            {
                if (section == null || (setting = section.GetDefault()) == null)
                {
                    return(DefaultSubscribeManager.Instance);
                }
            }
            else if (section != null)
            {
                setting = section.GetSetting(configName);
            }

            if (setting == null)
            {
                return(null);
            }

            return(ConfigurationUnity.CreateInstance <SubscribeConfigurationSetting, ISubscribeManager>(setting, s => s.SubscriberType));
        }
        /// <summary>
        /// 根据应用程序配置,创建任务调度管理器。
        /// </summary>
        /// <param name="serviceProvider">应用程序服务提供者实例。</param>
        /// <param name="configName">应用程序配置项的名称。</param>
        /// <returns><paramref name="configName"/>缺省时,如果应用程序未配置,则为 <see cref="DefaultTaskScheduler"/>,否则为配置项对应的 <see cref="ITaskScheduler"/> 实例。</returns>
        private static ITaskScheduler CreateScheduler(IServiceProvider serviceProvider, string configName = null)
        {
            ITaskScheduler            manager;
            IConfigurationSettingItem setting = null;
            var section = ConfigurationUnity.GetSection <TaskScheduleConfigurationSection>();

            if (section != null && section.Factory != null)
            {
                manager = ConfigurationUnity.Cached <ITaskScheduler>($"TaskScheduler_{configName ?? "default"}", serviceProvider,
                                                                     () => section.Factory.CreateInstance(serviceProvider, configName) as ITaskScheduler);

                if (manager != null)
                {
                    return(manager);
                }
            }

            if (string.IsNullOrEmpty(configName))
            {
                if (section == null || (setting = section.GetDefault()) == null)
                {
                    return(serviceProvider != null ?
                           new DefaultTaskScheduler(serviceProvider) : DefaultTaskScheduler.Instance);
                }
            }
            else if (section != null)
            {
                setting = section.GetSetting(configName);
            }

            if (setting == null)
            {
                return(null);
            }

            return(ConfigurationUnity.Cached <ITaskScheduler>($"TaskScheduler_{configName ?? "default"}", serviceProvider,
                                                              () => ConfigurationUnity.CreateInstance <TaskScheduleConfigurationSetting, ITaskScheduler>(serviceProvider, setting, s => s.SchedulerType, (s, t) => InitializeDefinitions(s, t))));
        }
Пример #5
0
        /// <summary>
        /// 根据应用程序配置,创建文本序列化器。
        /// </summary>
        /// <param name="serviceProvider">应用程序服务提供者实例。</param>
        /// <param name="configName">应用程序配置项的名称。</param>
        /// <returns></returns>
        private static ISerializer CreateSerializer(IServiceProvider serviceProvider, string configName = null)
        {
            ISerializer serializer;
            IConfigurationSettingItem setting = null;
            var section = ConfigurationUnity.GetSection <SerializerConfigurationSection>();

            if (section != null && section.Factory != null)
            {
                serializer = ConfigurationUnity.Cached <ISerializer>($"Serializer_{configName}", serviceProvider,
                                                                     () => section.Factory.CreateInstance(serviceProvider, configName) as ISerializer);

                if (serializer != null)
                {
                    return(serializer);
                }
            }

            if (string.IsNullOrEmpty(configName))
            {
                if (section == null || (setting = section.GetDefault()) == null)
                {
                    return(null);
                }
            }
            else if (section != null)
            {
                setting = section.GetSetting(configName);
            }

            if (setting == null)
            {
                return(null);
            }

            return(ConfigurationUnity.Cached <ISerializer>($"Serializer_{configName}", serviceProvider,
                                                           () => ConfigurationUnity.CreateInstance <SerializerConfigurationSetting, ISerializer>(serviceProvider, setting, s => s.SerializerType)));
        }
Пример #6
0
        /// <summary>
        /// 根据应用程序配置,创建缓存管理器。
        /// </summary>
        /// <param name="serviceProvider">应用程序服务提供者实例。</param>
        /// <param name="configName">应用程序配置项的名称。</param>
        /// <returns><paramref name="configName"/>缺省时,如果应用程序未配置,则为 <see cref="MemoryCacheManager"/>,否则为配置项对应的 <see cref="ICacheManager"/> 实例。</returns>
        private static ICacheManager CreateManager(IServiceProvider serviceProvider, string configName = null)
        {
            ICacheManager             manager;
            IConfigurationSettingItem setting = null;
            var section = ConfigurationUnity.GetSection <CachingConfigurationSection>();

            if (section != null && section.Factory != null)
            {
                manager = ConfigurationUnity.Cached <ICacheManager>($"CacheManager_{configName}", serviceProvider,
                                                                    () => section.Factory.CreateInstance(serviceProvider, configName) as ICacheManager);
                if (manager != null)
                {
                    return(manager);
                }
            }

            if (string.IsNullOrEmpty(configName))
            {
                if (section == null || (setting = section.GetDefault()) == null)
                {
                    return(serviceProvider != null ?
                           new MemoryCacheManager(serviceProvider) : MemoryCacheManager.Instance);
                }
            }
            else if (section != null)
            {
                setting = section.GetSetting(configName);
            }

            if (setting == null)
            {
                return(null);
            }

            return(ConfigurationUnity.Cached <ICacheManager>($"CacheManager_{configName}", serviceProvider,
                                                             () => ConfigurationUnity.CreateInstance <CachingConfigurationSetting, ICacheManager>(serviceProvider, setting, s => s.CacheType)));
        }
Пример #7
0
        /// <summary>
        /// 根据应用程序配置,创建一个分布式锁。
        /// </summary>
        /// <param name="serviceProvider">应用程序服务提供者实例。</param>
        /// <param name="configName">应用程序配置项的名称。</param>
        /// <returns><paramref name="configName"/>为配置项对应的 <see cref="IDistributedLocker"/> 实例。</returns>
        private static IDistributedLocker CreateLocker(IServiceProvider serviceProvider, string configName = null)
        {
            IDistributedLocker        locker;
            IConfigurationSettingItem setting = null;
            var section = ConfigurationUnity.GetSection <LockerConfigurationSection>();

            if (section != null && section.Factory != null)
            {
                locker = ConfigurationUnity.Cached <IDistributedLocker>($"Locker_{configName}", serviceProvider,
                                                                        () => section.Factory.CreateInstance(serviceProvider, configName) as IDistributedLocker);
                if (locker != null)
                {
                    return(locker);
                }
            }

            if (string.IsNullOrEmpty(configName))
            {
                if (section == null || (setting = section.GetDefault()) == null)
                {
                    throw new NotSupportedException();
                }
            }
            else if (section != null)
            {
                setting = section.GetSetting(configName);
            }

            if (setting == null)
            {
                return(null);
            }

            return(ConfigurationUnity.Cached <IDistributedLocker>($"Locker_{configName}", serviceProvider,
                                                                  () => ConfigurationUnity.CreateInstance <LockerConfigurationSetting, IDistributedLocker>(serviceProvider, setting, s => s.LockerType)));
        }