我经常应用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 =
新闻热点
疑难解答