解决MySql布尔型新旧版本兼容问题,采用枚举来表示布尔型的数据表。由正向工程赋值
大石头 authored at 2018-05-15 21:21:05
8.34 KiB
X
# ÅäÖÃϵͳ Config ## ¸ÅÊö NewLife.Core ÌṩÁËÇ¿´óÁé»îµÄÅäÖÃϵͳ£¬Ö§³Ö XML¡¢JSON¡¢INI µÈ¶àÖÖ¸ñʽ£¬ÒÔ¼°±¾µØÎļþºÍÔ¶³ÌÅäÖÃÖÐÐÄ¡£Í¨¹ý `Config<T>` »ùÀà¿ÉÒÔ¿ìËÙ´´½¨Ç¿ÀàÐÍÅäÖã¬Ö§³ÖÈȸüкÍ×Ô¶¯±£´æ¡£ **ÃüÃû¿Õ¼ä**£º`NewLife.Configuration` **ÎĵµµØÖ·**£ºhttps://newlifex.com/core/config ## ºËÐÄÌØÐÔ - **Ç¿ÀàÐÍÅäÖÃ**£º¼Ì³Ð `Config<T>` ×Ô¶¯¹ÜÀíÅäÖÃÎļþ - **¶à¸ñʽ֧³Ö**£ºXML¡¢JSON¡¢INI¡¢HTTP µÈÅäÖÃÌṩÕß - **ÈȸüÐÂ**£ºÅäÖÃÎļþ±ä»¯Ê±×Ô¶¯ÖØÔØ - **×¢ÊÍÖ§³Ö**£ºXML ¸ñʽ֧³Ö×Ô¶¯Éú³É×¢ÊÍ - **·Ö²ãÅäÖÃ**£ºÖ§³Ö¶à¼¶Ç¶Ì×ÅäÖýṹ - **ÅäÖÃÖÐÐÄ**£ºÖ§³ÖÔ¶³ÌÅäÖÃÖÐÐÄ£¨ÈçÐdz¾£© ## ¿ìËÙ¿ªÊ¼ ### ¶¨ÒåÅäÖÃÀà ```csharp using NewLife.Configuration; using System.ComponentModel; /// <summary>Ó¦ÓÃÅäÖÃ</summary> [Config("App")] // ÅäÖÃÎļþÃûΪ App.config public class AppConfig : Config<AppConfig> { [Description("Ó¦ÓÃÃû³Æ")] public String Name { get; set; } = "MyApp"; [Description("·þÎñ¶Ë¿Ú")] public Int32 Port { get; set; } = 8080; [Description("µ÷ÊÔģʽ")] public Boolean Debug { get; set; } [Description("Êý¾Ý¿âÁ¬½Ó")] public String ConnectionString { get; set; } = "Server=.;Database=test"; } ``` ### ʹÓÃÅäÖà ```csharp // ¶ÁÈ¡ÅäÖã¨×Ô¶¯¼ÓÔØ/´´½¨ÅäÖÃÎļþ£© var config = AppConfig.Current; Console.WriteLine($"Ó¦ÓÃ: {config.Name}"); Console.WriteLine($"¶Ë¿Ú: {config.Port}"); // Ð޸IJ¢±£´æ config.Debug = true; config.Save(); ``` **×Ô¶¯Éú³ÉµÄÅäÖÃÎļþ** (`App.config`)£º ```xml <?xml version="1.0" encoding="utf-8"?> <App> <!--Ó¦ÓÃÃû³Æ--> <Name>MyApp</Name> <!--·þÎñ¶Ë¿Ú--> <Port>8080</Port> <!--µ÷ÊÔģʽ--> <Debug>false</Debug> <!--Êý¾Ý¿âÁ¬½Ó--> <ConnectionString>Server=.;Database=test</ConnectionString> </App> ``` ## API ²Î¿¼ ### Config&lt;T&gt; »ùÀà ```csharp public class Config<TConfig> where TConfig : Config<TConfig>, new() { /// <summary>µ±Ç°Ê¹ÓõÄÌṩÕß</summary> public static IConfigProvider? Provider { get; set; } /// <summary>µ±Ç°ÊµÀý</summary> public static TConfig Current { get; } /// <summary>¼ÓÔØÅäÖÃ</summary> public virtual Boolean Load(); /// <summary>±£´æÅäÖÃ</summary> public virtual Boolean Save(); /// <summary>ÅäÖüÓÔØºó´¥·¢</summary> protected virtual void OnLoaded() { } } ``` ### ConfigAttribute ÌØÐÔ ```csharp [AttributeUsage(AttributeTargets.Class)] public class ConfigAttribute : Attribute { /// <summary>ÅäÖÃÃû£¨ÎļþÃû£¬²»º¬À©Õ¹Ãû£©</summary> public String Name { get; set; } /// <summary>ÅäÖÃÌṩÕßÀàÐÍ</summary> public Type? Provider { get; set; } } ``` ### IConfigProvider ½Ó¿Ú ```csharp public interface IConfigProvider { /// <summary>Ãû³Æ</summary> String Name { get; set; } /// <summary>¸ùÔªËØ</summary> IConfigSection Root { get; set; } /// <summary>ÊÇ·ñÐÂÅäÖÃ</summary> Boolean IsNew { get; set; } /// <summary>»ñÈ¡/ÉèÖÃÅäÖÃÖµ</summary> String? this[String key] { get; set; } /// <summary>¼ÓÔØÅäÖÃ</summary> Boolean LoadAll(); /// <summary>±£´æÅäÖÃ</summary> Boolean SaveAll(); /// <summary>¼ÓÔØµ½Ä£ÐÍ</summary> T? Load<T>(String? path = null) where T : new(); /// <summary>°ó¶¨Ä£ÐÍ£¨ÈȸüУ©</summary> void Bind<T>(T model, Boolean autoReload = true, String? path = null); /// <summary>ÅäÖøıäʼþ</summary> event EventHandler? Changed; } ``` ## ÅäÖÃÌṩÕß ### XmlConfigProvider£¨Ä¬ÈÏ£© ```csharp // ×Ô¶¯Ê¹Óà XML ¸ñʽ [Config("Database")] public class DbConfig : Config<DbConfig> { } // »òÏÔʽָ¶¨ [Config("Database", Provider = typeof(XmlConfigProvider))] public class DbConfig : Config<DbConfig> { } ``` ### JsonConfigProvider ```csharp [Config("appsettings", Provider = typeof(JsonConfigProvider))] public class AppSettings : Config<AppSettings> { public String Name { get; set; } public LoggingConfig Logging { get; set; } = new(); } public class LoggingConfig { public String Level { get; set; } = "Information"; public Boolean Console { get; set; } = true; } ``` **Éú³ÉµÄ JSON Îļþ**£º ```json { "Name": "MyApp", "Logging": { "Level": "Information", "Console": true } } ``` ### InIConfigProvider ```csharp [Config("settings", Provider = typeof(InIConfigProvider))] public class IniConfig : Config<IniConfig> { public String Server { get; set; } public Int32 Port { get; set; } } ``` ### HttpConfigProvider ÓÃÓÚÁ¬½ÓÔ¶³ÌÅäÖÃÖÐÐÄ£º ```csharp [HttpConfig("http://config.server.com", AppId = "myapp", Secret = "xxx")] public class RemoteConfig : Config<RemoteConfig> { public String Setting1 { get; set; } } ``` ## ʹÓó¡¾° ### 1. Êý¾Ý¿âÅäÖà ```csharp [Config("Database")] public class DatabaseConfig : Config<DatabaseConfig> { [Description("Êý¾Ý¿âÀàÐÍ")] public String DbType { get; set; } = "MySql"; [Description("Á¬½Ó×Ö·û´®")] public String ConnectionString { get; set; } [Description("×î´óÁ¬½ÓÊý")] public Int32 MaxPoolSize { get; set; } = 100; [Description("ÃüÁʱ£¨Ã룩")] public Int32 CommandTimeout { get; set; } = 30; } // ʹÓà var db = DatabaseConfig.Current; var connStr = db.ConnectionString; ``` ### 2. ǶÌ×ÅäÖà ```csharp [Config("Service")] public class ServiceConfig : Config<ServiceConfig> { public String Name { get; set; } = "MyService"; public HttpConfig Http { get; set; } = new(); public CacheConfig Cache { get; set; } = new(); } public class HttpConfig { public Int32 Port { get; set; } = 8080; public Int32 Timeout { get; set; } = 30; public Boolean Ssl { get; set; } } public class CacheConfig { public String Type { get; set; } = "Memory"; public Int32 Expire { get; set; } = 3600; } ``` ### 3. Êý×éÅäÖà ```csharp [Config("Servers")] public class ServersConfig : Config<ServersConfig> { public String[] Hosts { get; set; } = ["localhost"]; public List<EndpointConfig> Endpoints { get; set; } = new(); } public class EndpointConfig { public String Host { get; set; } public Int32 Port { get; set; } } ``` ### 4. ÅäÖÃÑéÖ¤ ```csharp [Config("App")] public class AppConfig : Config<AppConfig> { public Int32 Port { get; set; } = 8080; protected override void OnLoaded() { // ÑéÖ¤ÅäÖà if (Port <= 0 || Port > 65535) { Port = 8080; XTrace.WriteLine("¶Ë¿ÚÅäÖÃÎÞЧ£¬Ê¹ÓÃĬÈÏÖµ 8080"); } } } ``` ### 5. ÅäÖñä¸ü¼àÌý ```csharp var config = AppConfig.Current; AppConfig.Provider.Changed += (s, e) => { XTrace.WriteLine("ÅäÖÃÒѸüÐÂ"); // ÖØÐ¶ÁÈ¡ÅäÖà config = AppConfig.Current; }; ``` ### 6. Ö±½ÓʹÓÃÌṩÕß ```csharp // ²»¼Ì³Ð Config<T>£¬Ö±½ÓʹÓÃÌṩÕß var provider = new JsonConfigProvider { FileName = "custom.json" }; provider.LoadAll(); // ¶Áȡֵ var name = provider["Name"]; var port = provider["Server:Port"].ToInt(); // ÉèÖÃÖµ provider["Debug"] = "true"; provider.SaveAll(); ``` ## ÅäÖÃÎļþ·¾¶ ĬÈÏÅäÖÃÎļþ´æ·ÅÔÚÓ¦ÓóÌÐòĿ¼£¬¿Éͨ¹ýÒÔÏ·½Ê½×Ô¶¨Ò壺 ```csharp // Ïà¶Ô·¾¶ [Config("Config/App")] public class AppConfig : Config<AppConfig> { } // ÅäÖÃÌṩÕß³õʼ»¯ var provider = new XmlConfigProvider(); provider.Init("Config/App.config"); ``` ## ×î¼Ñʵ¼ù ### 1. ʹÓà Description ÌØÐÔ ```csharp [Description("ÈÕÖ¾¼¶±ð£ºDebug/Info/Warn/Error")] public String LogLevel { get; set; } = "Info"; ``` ### 2. ÌṩĬÈÏÖµ ```csharp public Int32 MaxRetry { get; set; } = 3; public String[] AllowedHosts { get; set; } = ["*"]; ``` ### 3. Ãô¸ÐÐÅÏ¢´¦Àí ```csharp [Config("Secrets")] public class SecretsConfig : Config<SecretsConfig> { // ½¨Òé´Ó»·¾³±äÁ¿¶ÁÈ¡ public String ApiKey { get; set; } = Environment.GetEnvironmentVariable("API_KEY") ?? ""; } ``` ### 4. ÅäÖÃÖØÔØ ```csharp // Ç¿ÖÆÖØÐ¼ÓÔØÅäÖà AppConfig._Current = null; var fresh = AppConfig.Current; ``` ## Óë appsettings.json ¼¯³É ```csharp // ¼ÓÔØ ASP.NET Core ·ç¸ñµÄÅäÖà var provider = JsonConfigProvider.LoadAppSettings(); var connectionString = provider["ConnectionStrings:Default"]; var logLevel = provider["Logging:LogLevel:Default"]; ``` ## ÅäÖü̳Р```csharp public abstract class BaseConfig<T> : Config<T> where T : BaseConfig<T>, new() { [Description("Ó¦Óð汾")] public String Version { get; set; } = "1.0.0"; [Description("ÆôÓõ÷ÊÔ")] public Boolean Debug { get; set; } } [Config("MyApp")] public class MyAppConfig : BaseConfig<MyAppConfig> { [Description("ÌØ¶¨ÉèÖÃ")] public String CustomSetting { get; set; } } ``` ## Ïà¹ØÁ´½Ó - [JSON ÐòÁл¯](/NewLife/X/Blob/dev/Doc/json-JSONÐòÁл¯.md) - [XML ÐòÁл¯](/NewLife/X/Blob/dev/Doc/xml-XMLÐòÁл¯.md) - [ÈÕ־ϵͳ ILog](/NewLife/X/Blob/dev/Doc/log-ÈÕÖ¾ILog.md)