背景
我經常應用C#開發一些小的桌面程序,這些桌面程序往往有以下幾個特點:
- 程序比較小,開發周期很短。
- 程序的數據量不大,多數情況下不超過1萬行記錄。
- 對程序的性能要求不高。
- 程序並發很少或者基本沒有。
- 盡量程序部署簡單。
因為C#程序很多情況下都是CURD,結合上面的需求,我一直考慮做一個簡單的框架,以達到快速開發的目的。應用XML序列化(XmlSerializer)功能,我開發了一個簡單符合上面要求的底層框架。
框架思路
我准備用XML文件作為數據存儲,為了保證數據同步,同時在內存中存儲一份數據,每次操作時,都是操作內存中的數據,操作完之后再同步到數據庫中。
另外,為了保證框架的易用性,我把底層實現寫成了一個泛型類,所有操作類繼承此泛型類。
框架功能描述
框架主要包括以下幾個功能:
- 應用XML文件作為數據庫,不依賴其他數據庫系統。
- 對外提供基本的CURD功能。
- 減少配置,做到0配置。
數據會存儲在運行目錄下面的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>
由上面的例子可以看到,應用此框架進行開發還是非常容易的。
總結
框架優點:
- 快速開發,完全不需要考慮底層
- 易於部署
- 框架代碼比較短小,總共200行左右。
框架缺點:
- 效率低下
- 未考慮並發,非線程安全
我會在下一篇文章里面介紹如何應用這個框架開發一個代碼片段管理系統
附:框架源代碼
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();
}
}
}