我經常應用C#開發一些小的桌面程序,這些桌面程序往往有以下幾個特點:
因為C#程序很多情況下都是CURD,結合上面的需求,我一直考慮做一個簡單的框架,以達到快速開發的目的。應用XML序列化(XmlSerializer)功能,我開發了一個簡單符合上面要求的底層框架。
我準備用XML文件作為數據存儲,為了保證數據同步,同時在內存中存儲一份數據,每次操作時,都是操作內存中的數據,操作完之后再同步到數據庫中。另外,為了保證框架的易用性,我把底層實現寫成了一個泛型類,所有操作類繼承此泛型類。
框架主要包括以下幾個功能:
數據會存儲在運行目錄下面的data目錄下,數據文件可以由開發者指定,也可以采用默認數據文件。
如何應用框架進行開發呢?我把框架打成了一個DLL文件,開發項目時,需要引用這個DLL。開發者每定義一個實體類,需要對應定義一個操作類,此操作類需要繼承我的泛型操作類。
注意:實體類需要有一個string類型的ID,我一般用GUID實體類示例代碼:
namespace zDash{ public class CodeEntity { public string Id { get; set; } public string Key { get; set; } public string Lang { get; set; } public byte[] RealContent { get; set; } }}
我把操作類寫成了單例模式,操作類示例代碼:
namespace zDash{ public class CodeBll : Wisdombud.xmldb.BaseXmlBll<CodeEntity> { PRivate static CodeBll inst = new CodeBll(); private CodeBll() { } public static CodeBll getInst() { return inst; } }}
如何應用:
CodeBll.getInst().Insert(entity);
XML文件的內容
<?xml version="1.0"?><ArrayOfCodeEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <CodeEntity> <Id>1</Id> <Key>符號</Key> <Lang>C#</Lang> <RealContent>e1</RealContent> </CodeEntity> <CodeEntity> <Id>2</Id> <Key>符號1</Key> <Lang>C#</Lang> <RealContent>e1</RealContent> </CodeEntity></ArrayOfCodeEntity>
由上面的例子可以看到,應用此框架進行開發還是非常容易的。
我會在下一篇文章里面介紹如何應用這個框架開發一個代碼片段管理系統
using System;using System.Collections.Generic;using System.IO;using System.Reflection;using System.Xml.Serialization;namespace Wisdombud.xmldb{ public class XmlSerializerBll<T> { private static XmlSerializerBll<T> instance; private string dbFile; public string Dbfile { get { return dbFile; } set { if (!string.IsNullOrEmpty(value) && !value.Equals(dbFile)) { this.entityList.Clear(); } dbFile = value; this.ReadDb(); } } private List<T> entityList = new List<T>(); private XmlSerializerBll() { this.SetDbFile(); this.ReadDb(); } private void SetDbFile() { string folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data"); try { if (Directory.Exists(folder) == false) { Directory.CreateDirectory(folder); } Type type = typeof(T); if (string.IsNullOrEmpty(this.Dbfile)) { this.Dbfile = Path.Combine(folder, type.Name + ".xml"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } public static XmlSerializerBll<T> GetInstance() { if (instance == null) { instance = new XmlSerializerBll<T>(); } return instance; } public void Insert(T entity) { this.entityList.Add(entity); this.WriteDb(); } public void InsertRange(IList<T> list) { this.entityList.AddRange(list); this.WriteDb(); } public System.Collections.Generic.List<T> SelectBy(string name, Object value) { System.Collections.Generic.List<T> list = new List<T>(); if (value == null) { return list; } Type t = typeof(T); foreach (var inst in this.entityList) { foreach (PropertyInfo pro in t.GetProperties()) { if (pro.Name.ToLower() == name.ToLower()) { if (value.ToString() == (pro.GetValue(inst, null) ?? string.Empty).ToString()) { list.Add(inst); } } } } return list; } public T SelectById(string id) { Type t = typeof(T); foreach (var inst in this.entityList) { foreach (PropertyInfo pro in t.GetProperties()) { if (pro.Name.ToLower() == "id") { if (id == (pro.GetValue(inst, null) ?? string.Empty).ToString()) { return inst; } } } } return default(T); } public void UpdateById(T entity) { Type t = typeof(T); string id = string.Empty; foreach (PropertyInfo pro in t.GetProperties()) { if (pro.Name.ToLower() == "id") { id = (pro.GetValue(entity, null) ?? string.Empty).ToString(); break; } } this.DeleteById(id); this.Insert(entity); } public void DeleteById(string id) { Type t = typeof(T); T entity = default(T); foreach (var inst in this.entityList) { foreach (PropertyInfo pro in t.GetProperties()) { if (pro.Name.ToLower() == "id") { if ((pro.GetValue(inst, null) ?? string.Empty).ToString() == id) { entity = inst; goto FinishLoop; } } } } FinishLoop: this.entityList.Remove(entity); this.WriteDb(); } public List<T> SelectAll() { this.ReadDb(); return this.entityList; } public void DeleteAll() { this.entityList.Clear(); this.WriteDb(); } private void WriteDb() { XmlSerializer ks = new XmlSerializer(typeof(List<T>)); FileInfo fi = new FileInfo(this.Dbfile); var dir = fi.Directory; if (!dir.Exists) { dir.Create(); } Stream writer = new FileStream(this.Dbfile, FileMode.Create, Fileaccess.ReadWrite); ks.Serialize(writer, this.entityList); writer.Close(); } private void ReadDb() { if (File.Exists(this.Dbfile)) { XmlSerializer ks = new XmlSerializer(typeof(List<T>)); Stream reader = new FileStream(this.Dbfile, FileMode.Open, FileAccess.ReadWrite); this.entityList = ks.Deserialize(reader) as List<T>; reader.Close(); } else { this.entityList = new List<T>(); } } }}
using System.Collections.Generic;namespace Wisdombud.xmldb{ public class BaseXmlBll<T> where T : new() { public string DbFile { get { return this.bll.Dbfile; } set { bll.Dbfile = value; } } private XmlSerializerBll<T> bll =
新聞熱點
疑難解答