Browse Source

增加JSON格式程序配置管理器

yuzhengyang 5 years ago
parent
commit
7a1f3f6952

+ 43 - 0
Azylee.Utils/Azylee.Core/AppUtils/AppConfigUtils/AppConfig.cs

@@ -0,0 +1,43 @@
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.AppUtils.AppConfigUtils
+{
+    /// <summary>
+    /// AppConfig 配置管理器
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    public abstract class AppConfig<T> where T : IAppConfigModel, new()
+    {
+        /// <summary>
+        /// 配置信息模型
+        /// </summary>
+        public T Config { get; set; }
+
+        /// <summary>
+        /// 默认构造函数
+        /// </summary>
+        public AppConfig()
+        { }
+
+        /// <summary>
+        /// 初始化配置信息
+        /// </summary>
+        /// <returns></returns>
+        public abstract bool OnCreate();
+        /// <summary>
+        /// 销毁配置信息
+        /// </summary>
+        /// <returns></returns>
+        public abstract bool OnDestroy();
+        /// <summary>
+        /// 保存配置信息
+        /// </summary>
+        /// <returns></returns>
+        public abstract bool DoSave();
+    }
+}

+ 39 - 0
Azylee.Utils/Azylee.Core/AppUtils/AppConfigUtils/AppConfigModels/AppConfigRegionModel.cs

@@ -0,0 +1,39 @@
+using Azylee.Core.DataUtils.CollectionUtils;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
+{
+    public class AppConfigRegionModel<T> where T : IAppConfigItemModel
+    {
+        public List<T> Items { get; set; }
+
+        public AppConfigRegionModel()
+        {
+            this.Items = new List<T>();
+        }
+        public bool HasItems()
+        {
+            return Ls.Ok(Items);
+        }
+        public void AddItem(T item, bool overwrite = false)
+        {
+            bool repeat = false;
+            for (int i = 0; i < Items.Count; i++)
+            {
+                if (Items[i].GetUniqueName() == item.GetUniqueName())
+                {
+                    if (overwrite) { Items[i] = item; }
+                    repeat = true;
+                }
+            }
+            if (!repeat) { this.Items.Add(item); }
+        }
+        public void OrderByNumber()
+        {
+            Items = Items.OrderBy(x => x.GetOrderNumber()).ToList();
+        }
+    }
+}

+ 13 - 0
Azylee.Utils/Azylee.Core/AppUtils/AppConfigUtils/AppConfigModels/IAppConfigItemModel.cs

@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
+{
+    public interface IAppConfigItemModel
+    {
+        int GetOrderNumber();
+        string GetUniqueName();
+    }
+}

+ 18 - 0
Azylee.Utils/Azylee.Core/AppUtils/AppConfigUtils/AppConfigModels/IAppConfigModel.cs

@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
+{
+    /// <summary>
+    /// AppConfig 配置管理器 指定模型接口
+    /// </summary>
+    public interface IAppConfigModel
+    {
+        /// <summary>
+        /// 强制设置配置值
+        /// </summary>
+        void ForceSet();
+    }
+}

+ 4 - 0
Azylee.Utils/Azylee.Core/Azylee.Core.csproj

@@ -46,6 +46,10 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="AppUtils\AppConfigUtils\AppConfig.cs" />
+    <Compile Include="AppUtils\AppConfigUtils\AppConfigModels\IAppConfigItemModel.cs" />
+    <Compile Include="AppUtils\AppConfigUtils\AppConfigModels\AppConfigRegionModel.cs" />
+    <Compile Include="AppUtils\AppConfigUtils\AppConfigModels\IAppConfigModel.cs" />
     <Compile Include="AppUtils\AppInfoTool.cs" />
     <Compile Include="AppUtils\AppLaunchTool.cs" />
     <Compile Include="AppUtils\AppSettleTool.cs" />

+ 1 - 2
Azylee.Utils/Azylee.Jsons/Azylee.Jsons.csproj

@@ -46,8 +46,7 @@
   <ItemGroup>
     <Compile Include="ConvertJson.cs" />
     <Compile Include="Json.cs" />
-    <Compile Include="JsonConfigUtils\IJsonConfigModel.cs" />
-    <Compile Include="JsonConfigUtils\JsonConfig.cs" />
+    <Compile Include="JsonAppConfigUtils\JsonAppConfig.cs" />
     <Compile Include="JsonFormat.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>

+ 24 - 19
Azylee.Utils/Azylee.Jsons/JsonConfigUtils/JsonConfig.cs

@@ -1,4 +1,6 @@
-using Azylee.Core.IOUtils.FileUtils;
+using Azylee.Core.AppUtils.AppConfigUtils;
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels;
+using Azylee.Core.IOUtils.FileUtils;
 using Azylee.Core.IOUtils.TxtUtils;
 using System;
 using System.Collections.Generic;
@@ -6,28 +8,36 @@ using System.IO;
 using System.Linq;
 using System.Text;
 
-namespace Azylee.Jsons.JsonConfigUtils
+namespace Azylee.Jsons.JsonAppConfigUtils
 {
     /// <summary>
     /// Json 配置管理器
     /// </summary>
     /// <typeparam name="T"></typeparam>
-    public class JsonConfig<T> where T : IJsonConfigModel, new()
+    public class JsonAppConfig<T> : AppConfig<T> where T : IAppConfigModel, new()
     {
-        private T Config { get; set; }
-        private string FilePath { get; set; }
-        private string FilePathBackup { get; set; }
+        private string FilePath;
+        private string FilePathBackup;
 
-        private JsonConfig() { }
+        private JsonAppConfig() { }
         /// <summary>
         /// 构造配置管理器
         /// </summary>
         /// <param name="filepath">配置文件路径</param>
-        public JsonConfig(string filepath)
+        public JsonAppConfig(string filepath)
         {
             this.FilePath = filepath;
             this.FilePathBackup = filepath + ".backup";
 
+            // 配置初始化
+            OnCreate();
+            // 重置配置项
+            this.Config.ForceSet();
+            // 保存配置
+            DoSave();
+        }
+        public override bool OnCreate()
+        {
             // 读取默认配置文件
             if (File.Exists(this.FilePath))
             {
@@ -46,23 +56,18 @@ namespace Azylee.Jsons.JsonConfigUtils
             {
                 this.Config = new T();
             }
-            // 重置配置项
-            this.Config.Reset();
-            Save();
+            return true;
         }
-        /// <summary>
-        /// 获取配置信息
-        /// </summary>
-        /// <returns></returns>
-        public T Get()
+
+        public override bool OnDestroy()
         {
-            return this.Config;
+            return DoSave();
         }
         /// <summary>
         /// 保存配置信息
         /// </summary>
-        /// <returns></returns>
-        public bool Save()
+        /// <returns></returns> 
+        public override bool DoSave()
         {
             string s = Json.Object2String(this.Config);
             s = JsonFormat.Format(s);

+ 0 - 18
Azylee.Utils/Azylee.Jsons/JsonConfigUtils/IJsonConfigModel.cs

@@ -1,18 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace Azylee.Jsons.JsonConfigUtils
-{
-    /// <summary>
-    /// Json 配置管理器 指定模型接口
-    /// </summary>
-    public interface IJsonConfigModel
-    {
-        /// <summary>
-        /// 重置配置项
-        /// </summary>
-        void Reset();
-    }
-}

+ 5 - 4
Azylee.Utils/Tests/Test.Ges/Program.cs

@@ -1,4 +1,4 @@
-using Azylee.Jsons.JsonConfigUtils;
+using Azylee.Jsons.JsonAppConfigUtils;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -15,9 +15,10 @@ namespace Test.Ges
 
             //Console.Read();
 
-            JsonConfig<User> jsonConfig = new JsonConfig<User>(@"D:\tmp\test\AppConfig.json");
-            jsonConfig.Get().Age = 1;
-            jsonConfig.Save();
+            JsonAppConfig<User> appConfig = new JsonAppConfig<User>(@"D:\tmp\test\AppConfig.json");
+            //JsonConfig<User> jsonConfig = new JsonConfig<User>(@"D:\tmp\test\AppConfig.json");
+            appConfig.Config.Age = 1;
+            appConfig.DoSave();
 
         }
     }

+ 1 - 0
Azylee.Utils/Tests/Test.Ges/Test.Ges.csproj

@@ -46,6 +46,7 @@
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="User.cs" />
+    <Compile Include="UserBook.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="App.config" />

+ 40 - 3
Azylee.Utils/Tests/Test.Ges/User.cs

@@ -1,4 +1,5 @@
-using Azylee.Jsons.JsonConfigUtils;
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels;
+using Azylee.Core.DataUtils.CollectionUtils;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -7,11 +8,47 @@ using System.Threading.Tasks;
 
 namespace Test.Ges
 {
-    public class User
+    public class User : IAppConfigModel
     {
-        public int Name { get; set; }
+        public string Name { get; set; }
         public int Sex { get; set; }
         public int Age { get; set; }
+        public List<Tuple<string, string>> School { get; set; }
+        public List<Tuple<string, string>> Job { get; set; }
+        public AppConfigRegionModel<UserBook> Book { get; set; }
 
+        public User()
+        {
+            School = new List<Tuple<string, string>>();
+            School.Add(new Tuple<string, string>("2000", "幼儿园"));
+            School.Add(new Tuple<string, string>("2005", "小学"));
+            School.Add(new Tuple<string, string>("2010", "初中"));
+            School.Add(new Tuple<string, string>("2013", "高中"));
+            Job = new List<Tuple<string, string>>();
+            Book = new AppConfigRegionModel<UserBook>();
+        }
+
+        public void ForceSet()
+        {
+            this.Name = "张无忌";
+            if (!Ls.ok(Job))
+            {
+                Job = new List<Tuple<string, string>>();
+                Job.Add(new Tuple<string, string>("2010", "志愿者工作-01"));
+                Job.Add(new Tuple<string, string>("2015", "志愿者工作-02"));
+            }
+
+            if (Book == null) 
+            Book.AddItem(new UserBook() { Number = 0, Name = "三国演义", Page = 5000, Price = 200 });
+            Book.AddItem(new UserBook() { Number = 1, Name = "红楼梦", Page = 8000, Price = 260 });
+            Book.AddItem(new UserBook() { Number = 2, Name = "西游记", Page = 9000, Price = 180 });
+            Book.AddItem(new UserBook() { Number = 3, Name = "水浒传", Page = 2000, Price = 100 });
+            Book.AddItem(new UserBook() { Name = "C#", Page = 1000, Price = 80 });
+            Book.AddItem(new UserBook() { Number = 15, Name = "JAVA", Page = 1200, Price = 20.99 });
+            Book.OrderByNumber();
+
+
+            Job.Add(new Tuple<string, string>("2010", "志愿者工作-99"));
+        }
     }
 }

+ 27 - 0
Azylee.Utils/Tests/Test.Ges/UserBook.cs

@@ -0,0 +1,27 @@
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Test.Ges
+{
+    public class UserBook : IAppConfigItemModel
+    {
+        public int Number { get; set; }
+        public string Name { get; set; }
+        public int Page { get; set; }
+        public double Price { get; set; }
+
+        public int GetOrderNumber()
+        {
+            return Number;
+        }
+
+        public string GetUniqueName()
+        {
+            return Name;
+        }
+    }
+}