我常常应用C#开发一些小的桌面程序,这些桌面程序每每有如下几个特色:数据库
由于C#程序不少状况下都是CURD,结合上面的需求,我一直考虑作一个简单的框架,以达到快速开发的目的。应用XML序列化(XmlSerializer)功能,我开发了一个简单符合上面要求的底层框架。安全
我准备用XML文件做为数据存储,为了保证数据同步,同时在内存中存储一份数据,每次操做时,都是操做内存中的数据,操做完以后再同步到数据库中。
另外,为了保证框架的易用性,我把底层实现写成了一个泛型类,全部操做类继承此泛型类。并发
框架主要包括如下几个功能:框架
数据会存储在运行目录下面的data目录下,数据文件能够由开发者指定,也能够采用默认数据文件。dom
如何应用框架进行开发呢?我把框架打成了一个DLL文件,开发项目时,须要引用这个DLL。开发者每定义一个实体类,须要对应定义一个操做类,此操做类须要继承个人泛型操做类。oop
注意:实体类须要有一个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; } } }
我把操做类写成了单例模式,操做类示例代码:this
namespace zDash { public class CodeBll : Wisdombud.xmldb.BaseXmlBll<CodeEntity> { private static CodeBll inst = new CodeBll(); private CodeBll() { } public static CodeBll getInst() { return inst; } } }
如何应用:spa
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 = XmlSerializerBll<T>.GetInstance(); public void Delete(string id) { var entity = this.Select(id); bll.DeleteById(id); } public void Insert(T entity) { bll.Insert(entity); } public void Insert(List<T> list) { bll.InsertRange(list); } public System.Collections.Generic.List<T> SelectAll() { return bll.SelectAll(); } public void Update(string oldId, T entity) { bll.UpdateById(entity); } public T Select(string id) { return bll.SelectById(id); } public System.Collections.Generic.List<T> SelectBy(string name, object value) { return bll.SelectBy(name, value); } public void DeleteAll() { bll.DeleteAll(); } } }