Lind.DDD.ConfigConstants统一管理系统配置

回到目录

Lind.DDD.ConfigConstants属于新添加的组件,用来帮助我们安全的进行配置消息的管理,我们在开发项目时,有个感觉,你的config配置项会越来越多,越来越难以阅读,随着你引用的组件增多,添加更多的配置项也难以避免,而我自己的Lind.DDD框架也是如此,今天加个日志,明天加个消息,后天又加个缓存,相应的配置项越来越多,越来越零散,如何管理它成了一个大问题,这时ConfigConstants组件诞生了。它支持多种方式的持久化,有着自己的IoC容器,而项目其它IoC(如仓储)是分开管理的,你可以方便的实现xml,mongodb,sql之间的数据持久化的切换!

ConfigConstants架构图

ConfigConstants的配置实体,它能会有更多,但管理只是集中在这一个块中

ConfigConstants的初始化,有自己的IoC容器,避免与其它组件发生冲突

        static object lockObj = new object();
        static IUnityContainer _container = new UnityContainer();
        static UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
        static IRepository<ConfigModel> _repository;
        static ConfigManager()
        {
            //读取关于配置节点的反射信息ConfigManager_Unity
            _container.LoadConfiguration(section, "ConfigManager_Unity");
            _repository = _container.Resolve<IRepository<ConfigModel>>();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        public static void Install()
        {
            if (_repository.GetModel().Count() == 0)
                _repository.Insert(new ConfigModel
                {
                    Cache_ExpireMinutes = 20,
                    Cache_Provider = "RedisCache",
                    Cache_RedisHost = "localhost:6379",

                    AoP_CacheStrategy = "EntLib",

                    Logger_Level = "DEBUG",
                    Logger_ProjectName = "Lind.DDD",
                    Logger_Type = "MongoDB",

                    MongoDB_DbName = "Test",
                    MongoDB_Host = "localhost:27017",
                    MongoDB_UserName = string.Empty,
                    MongoDB_Password = string.Empty,

                    Queue_FilePath = "FileQueue",
                    Queue_Type = "Redis",

                    Messaging_Email_Address = "bfyxzls@sina.com",
                    Messaging_Email_DisplayName = "bfyxzls",
                    Messaging_Email_Host = "smtp.sina.com",
                    Messaging_Email_Password = "123456",
                    Messaging_Email_Port = 21,
                    Messaging_Email_UserName = "仓储大叔",
                    Messaging_RtxApi = "http://192.168.1.8:8012/sendnotifynew.cgi?",
                    Messaging_SMSCharset = "utf-8",
                    Messaging_SMSGateway = "http://sms.yourname.com/Msg/SendMessage",
                    Messaging_SMSKey = "04fa25475e07669d4809d334f08fb35b",
                    Messaging_SMSSignType = "MD5"
                });

        }

ConfigConstants的属性对象,直接拿到你想要的配置字段

使用之前需要对它进行配置

  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <container name="ConfigManager_Unity">
      <!--泛型类型注入-->
      <register  type="Lind.DDD.IRepositories.IRepository`1,Lind.DDD" mapTo="Lind.DDD.Repositories.Xml.XmlRepository`1, Lind.DDD.Repositories.Xml" />
    </container>
  </unity>

当你使用大叔的XML进行持久化后,得到很友好的XML文档

感谢您的阅读!

回到目录

posted @ 2015-11-23 12:01  张占岭  阅读(2241)  评论(0编辑  收藏  举报