基于框架的全局配置模型
艰苦奋斗
昨天晚上搞到23点多,终于有了比较满意的全局框架配置模块,这个在之前在Lind.DDD.ConfigConstants模块中出现,可以说大叔对之前全局配置的不满,而对原有功能进行的改进,对于生产和获取采用了单例,对于配置项采用了模块的概念,这样在XML表现上更加符合面向对象的原则,并且在可读性上也更加友好!
劳动成果
单例模式获取配置信息
全局的保持不变的信息,没有必要每次都new实例,直接使用单例模块是最好的选择
/// <summary> /// 配置字典,单例模式 /// </summary> /// <returns></returns> public static ConfigModel Config { get { if (_config == null) { lock (_lockObj) { var old = Utils.SerializationHelper.DeserializeFromXml<ConfigModel>(_fileName); if (old == null) { _config = _init; Utils.SerializationHelper.SerializeToXml(_fileName, _init); } else { _config = old; } } } return _config; } }
配置实体
对于配置的实体类,大叔也公开一下,大家可以进行借鉴
public class ConfigModel { public ConfigModel() { Caching = new Caching(); Queue = new Queue(); Logger = new Logger(); Pub_Sub = new Pub_Sub(); MongoDB = new MongoDB(); Redis = new Redis(); Messaging = new Messaging(); DomainEvent = new DomainEvent(); Socket = new Socket(); CatServers = new List<CatServer>(); } /// <summary> /// 启用属性变化跟踪 /// </summary> [System.Xml.Serialization.XmlElementAttribute(Order = 0)] public int PropertyChanged { get; set; } /// <summary> /// 缓存相关配置 /// </summary> [System.Xml.Serialization.XmlElementAttribute(Order = 1)] public Caching Caching { get; set; } /// <summary> /// 队列相关配置 /// </summary> [System.Xml.Serialization.XmlElementAttribute(Order = 2)] public Queue Queue { get; set; } /// <summary> /// 日志相关 /// </summary> [System.Xml.Serialization.XmlElementAttribute(Order = 3)] public Logger Logger { get; set; } /// <summary> /// Pub_Sub相关 /// </summary> [System.Xml.Serialization.XmlElementAttribute(Order = 4)] public Pub_Sub Pub_Sub { get; set; } /// <summary> /// MongoDB相关 /// </summary> [System.Xml.Serialization.XmlElementAttribute(Order = 5)] public MongoDB MongoDB { get; set; } /// <summary> /// redis相关 /// </summary> [System.Xml.Serialization.XmlElementAttribute(Order = 6)] public Redis Redis { get; set; } /// <summary> /// Messaging消息相关 /// </summary> [System.Xml.Serialization.XmlElementAttribute(Order = 7)] public Messaging Messaging { get; set; } /// <summary> /// 领域事件相关 /// </summary> [System.Xml.Serialization.XmlElementAttribute(Order = 8)] public DomainEvent DomainEvent { get; set; } /// <summary> /// Socket通讯配置 /// </summary> [System.Xml.Serialization.XmlElementAttribute(Order = 10)] public Socket Socket { get; set; } /// <summary> /// Cat实时监控配置 /// XmlArray表示以数组的形式 /// </summary> [XmlArray(Order = 11)] public List<CatServer> CatServers { get; set; } }
对以上的设计完全是出于一种兴趣,感觉把每个模块都单独设置,在部署上不方便,所以把它们统一了,当然只适合于Lind.DDD框架,你自己的框架需要自己去定义实体了!