目前系統提供了多個配置文件,一些開發過程當中經常使用到的一些配置。緩存
既然是配置,那麼說明一些設置可能會根據項目的不一樣而有所不一樣。好比web.config其實就是個配置文件。app
當咱們定義好配置文件後,在項目運行的時調用了配置,都將在:~/App_Data/ 文件夾中生成(若是不存在這個配置文件)。這個你們要記住。框架
目前系統提供了(命名空間:FS.Configs):工具
先說說如何使用:網站
1 /// <summary> 2 /// 返回配置的實體 3 /// </summary> 4 public static T ConfigInfo 5 6 /// <summary> 7 /// 保存(序列化)指定路徑下的配置文件 8 /// </summary> 9 /// <param name="t">Config配置</param> 10 public static bool SaveConfig(T t = null)
這裏只有兩個方法,一個是讀取,另外一個是保存。T 是泛型,指的配置類。咱們的配置是class 類結構。咱們以數據庫配置DbConfigs舉例spa
1 namespace FS.Configs 2 { 3 /// <summary> 4 /// 全局 5 /// </summary> 6 public class DbConfigs : BaseConfigs<DbConfig> { } 7 8 /// <summary> 9 /// 默認數據庫路徑 10 /// </summary> 11 [Serializable] 12 public class DbConfig 13 { 14 /// <summary> 15 /// 數據庫鏈接列表,從/App_Data/Db.Configs讀取回來 16 /// </summary> 17 public List<DbInfo> DbList = new List<DbInfo>(); 18 } 19 20 /// <summary> 21 /// 數據庫鏈接配置 22 /// </summary> 23 public class DbInfo 24 { 25 /// <summary> 26 /// 數據庫鏈接串 27 /// </summary> 28 public string Server { get; set; } 29 30 /// <summary> 31 /// 數據庫賬號 32 /// </summary> 33 public string UserID { get; set; } 34 35 /// <summary> 36 /// 數據庫密碼 37 /// </summary> 38 public string PassWord { get; set; } 39 40 /// <summary> 41 /// 端口號 42 /// </summary> 43 public string Port { get; set; } 44 45 /// <summary> 46 /// Oracle SID 47 /// </summary> 48 public string SID { get; set; } 49 50 /// <summary> 51 /// 數據庫類型 52 /// </summary> 53 public DataBaseType DataType { get; set; } 54 55 /// <summary> 56 /// 數據庫版本 57 /// </summary> 58 public string DataVer { get; set; } 59 60 /// <summary> 61 /// 數據庫目錄 62 /// </summary> 63 public string Catalog { get; set; } 64 65 /// <summary> 66 /// 數據庫表前綴 67 /// </summary> 68 public string TablePrefix { get; set; } 69 70 /// <summary> 71 /// 最小鏈接池 72 /// </summary> 73 public int PoolMinSize { get; set; } 74 75 /// <summary> 76 /// 最大鏈接池 77 /// </summary> 78 public int PoolMaxSize { get; set; } 79 80 /// <summary> 81 /// 數據庫鏈接時間限制,單位秒 82 /// </summary> 83 public int ConnectTimeout { get; set; } 84 85 /// <summary> 86 /// 數據庫執行時間限制,單位秒 87 /// </summary> 88 public int CommandTimeout { get; set; } 89 90 /// <summary> 91 /// 經過索引返回實體 92 /// </summary> 93 public static implicit operator DbInfo(int index) 94 { 95 return DbConfigs.ConfigInfo.DbList.Count <= index ? null : DbConfigs.ConfigInfo.DbList[index]; 96 } 97 } 98 }
DbConfigs繼承了BaseConfigs,泛型傳入了DbConfig 的類。而DbConfig類裏的屬性是List<DbInfo> 這樣咱們不難看出,由於數據庫配置是個多配置的。有時你的項目用了多個不一樣物理地址的數據庫。.net
假設咱們如今有個配置文件,以下圖,是存放在~/App_Data/db.config 中的,以XML結構存儲(序列化)
1 <?xml version="1.0"?> 2 <DbConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 3 <DbList> 4 <DbInfo> 5 <Server>.</Server> 6 <UserID>sa</UserID> 7 <PassWord>123456</PassWord> 8 <DataType>SqlServer</DataType> 9 <DataVer>2005</DataVer> 10 <Catalog>Farseer</Catalog> 11 <PoolMinSize>16</PoolMinSize> 12 <PoolMaxSize>100</PoolMaxSize> 13 <ConnectTimeout>30</ConnectTimeout> 14 <CommandTimeout>60</CommandTimeout> 15 </DbInfo> 16 </DbList> 17 </DbConfig>
那咱們要讀取這個配置是,只須要簡單的操做便可:
1 // 返回數據庫配置文件中第1個索引的配置。 2 FS.Configs.DbInfo config = FS.Configs.DbConfigs.ConfigInfo.DbList[0];
對其重寫了操做符號,所以能夠更簡單的調用,二者是同等的:
1 // 返回數據庫配置文件中第1個索引的配置。 2 FS.Configs.DbInfo config = 0;
這樣咱們就能夠讀取配置文件的值,來進行調用了。另外也支持寫入並保存(序列化)到文件中:
1 // 返回數據庫配置文件中的配置。 2 FS.Configs.DbConfig dbConfig = FS.Configs.DbConfigs.ConfigInfo; 3 // 修改第1個配置文件的Server節點 4 dbConfig.DbList[0].Server = "192.168.1.1"; 5 // 添加新的配置 6 dbConfig.DbList.Add(new FS.Configs.DbInfo() { Server = "." }); 7 // 保存 8 FS.Configs.DbConfigs.SaveConfig(dbConfig);
讀者可能會問,那若是我手動在文件中修改了配置呢?好比在:~/App_Data/db.config 文件 用記事本打開手動修改以後,我要怎麼從新讀取?
細心的讀者會發現,這裏只有讀取、保存,並無Reload方法。由於開頭也提到了。配置文件是會緩存起來的。
Farseer在每次訪問這個緩存以前都作了一個檢查,就是檢查配置文件的最後保存時間。若是與緩存的文件保存時間不一致,那麼就會自動從新去硬盤上讀取並緩存哦。
上文中已經貼出了類的結構,這裏就不重複貼了。在這裏提一下一些細節的地方。
DataVer數據庫版本:此處填的是:2000、200五、2008 這種格式,有關版本,能夠到DbFactory.CreateConnString 查看
TablePrefix表前綴:有可能你的實際物理數據庫中的表都有共同的一個前綴。在處此能夠設置。
1 namespace FS.Configs 2 { 3 /// <summary> 4 /// 全局 5 /// </summary> 6 public class GeneralConfigs : BaseConfigs<GeneralConfig> { } 7 8 /// <summary> 9 /// 網站基本設置描述類, 加[Serializable]標記爲可序列化 10 /// </summary> 11 [Serializable] 12 public class GeneralConfig 13 { 14 // 此處省略 15 } 16 }
RewriterDomain:重寫域名替換(多個用;分隔) 這個是在重寫配置文件中使用的。在下幾篇的URL地址重寫教程中會提到這裏的使用。你們知道他是用;分隔的就好了。
1 namespace FS.Configs 2 { 3 /// <summary> 4 /// 系統配置 5 /// </summary> 6 public class SystemConfigs : BaseConfigs<SystemConfig> { } 7 8 /// <summary> 9 /// 配置文件 10 /// </summary> 11 [Serializable] 12 public class SystemConfig 13 { 14 // 此處省略 15 } 16 }
這裏的TimeOut的配置。都是有關到Session、Cookies 超時的默認值時間。而且包括了保存KEY的前綴。這樣有利於多個項目的不衝突。
1 namespace FS.Configs 2 { 3 /// <summary> 4 /// 全局 5 /// </summary> 6 public class RewriterConfigs : BaseConfigs<RewriterConfig> { } 7 8 /// <summary> 9 /// 重寫地址規則 10 /// </summary> 11 [Serializable] 12 public class RewriterConfig 13 { 14 // 此處省略 15 } 16 }
1 namespace FS.Configs 2 { 3 /// <summary> 4 /// 全局 5 /// </summary> 6 public class EmailConfigs : BaseConfigs<EmailConfig> { } 7 8 /// <summary> 9 /// Email配置信息類 10 /// </summary> 11 [Serializable] 12 public class EmailConfig 13 { 14 /// <summary> 15 /// Email配置,從/App_Data/Db.Configs讀取回來 16 /// </summary> 17 public List<EmailInfo> EmailList = new List<EmailInfo>(); 18 } 19 20 /// <summary> 21 /// E-mail配置 22 /// </summary> 23 public class EmailInfo 24 { 25 // 此處省略 26 } 27 }
1 namespace FS.Configs 2 { 3 /// <summary> 4 /// 緩存配置 5 /// </summary> 6 public class CacheConfigs : BaseConfigs<CacheConfig> { } 7 8 /// <summary> 9 /// 緩存配置 10 /// </summary> 11 [Serializable] 12 public class CacheConfig 13 { 14 // 此處省略 15 } 16 }
還記得普通邏輯層:BaseModel中的Data.Cache 屬性嗎?他們是如何工做的。這個在普通邏輯層裏也介紹過了。
前面介紹了框架預先寫好的一些配置,從這些配置文件裏,也容易看出,實質上,只須要你們在寫好配置類後,繼承BaseConfigs<T>就能夠實現配置文件的管理了。
BaseConfigs<T> 將對派生類進行序列化、反序列化的託管。
怎麼樣,很簡單吧!
QQ羣:116228666 (Farseer.net開源框架交流) 請註明:Farseer.Net
Farseer.Net是一款ORM框架 + 經常使用工具 + 擴展集合。
Farseer 意爲:先知、預言家 一般在某些場合時,提供計謀、策略。也但願該框架能給你們提供最大化的便捷。
ORM:其英文全稱是:Object(對象) Relational(關係) Mapping(映射)
Farseer.Net的目標是:快速上手、快速開發、簡單方便。
1 new User { ID = 1, Name = "張三" }.Insert()