C#應(yīng)用XML作為數(shù)據(jù)庫的快速開發(fā)框架實現(xiàn)方法
本文實例講述了C#應(yīng)用XML作為數(shù)據(jù)庫的快速開發(fā)框架實現(xiàn)方法。分享給大家供大家參考。具體如下:
背景
我經(jīng)常應(yīng)用C#開發(fā)一些小的桌面程序,這些桌面程序往往有以下幾個特點:
① 程序比較小,開發(fā)周期很短。
② 程序的數(shù)據(jù)量不大,多數(shù)情況下不超過1萬行記錄。
③ 對程序的性能要求不高。
④ 程序并發(fā)很少或者基本沒有。
⑤ 盡量程序部署簡單。
因為C#程序很多情況下都是CURD,結(jié)合上面的需求,我一直考慮做一個簡單的框架,以達到快速開發(fā)的目的。應(yīng)用XML序列化(XmlSerializer)功能,我開發(fā)了一個簡單符合上面要求的底層框架。
框架思路
我準備用XML文件作為數(shù)據(jù)存儲,為了保證數(shù)據(jù)同步,同時在內(nèi)存中存儲一份數(shù)據(jù),每次操作時,都是操作內(nèi)存中的數(shù)據(jù),操作完之后再同步到數(shù)據(jù)庫中。
另外,為了保證框架的易用性,我把底層實現(xiàn)寫成了一個泛型類,所有操作類繼承此泛型類。
框架功能描述
框架主要包括以下幾個功能:
① 應(yīng)用XML文件作為數(shù)據(jù)庫,不依賴其他數(shù)據(jù)庫系統(tǒng)。
② 對外提供基本的CURD功能。
③ 減少配置,做到0配置。
數(shù)據(jù)會存儲在運行目錄下面的data目錄下,數(shù)據(jù)文件可以由開發(fā)者指定,也可以采用默認數(shù)據(jù)文件。
框架應(yīng)用示例
如何應(yīng)用框架進行開發(fā)呢?我把框架打成了一個DLL文件,開發(fā)項目時,需要引用這個DLL。開發(fā)者每定義一個實體類,需要對應(yīng)定義一個操作類,此操作類需要繼承我的泛型操作類。
注意:實體類需要有一個string類型的ID,我一般用GUID
實體類示例代碼:
{
public class CodeEntity
{
public string Id { get; set; }
public string Key { get; set; }
public string Lang { get; set; }
public byte[] RealContent { get; set; }
}
}
我把操作類寫成了單例模式,操作類示例代碼:
{
public class CodeBll : Wisdombud.xmldb.BaseXmlBll<CodeEntity>
{
private static CodeBll inst = new CodeBll();
private CodeBll() { }
public static CodeBll getInst()
{
return inst;
}
}
}
如何應(yīng)用:
XML文件的內(nèi)容
<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>
由上面的例子可以看到,應(yīng)用此框架進行開發(fā)還是非常容易的。
總結(jié)
框架優(yōu)點:
① 快速開發(fā),完全不需要考慮底層
② 易于部署
③ 框架代碼比較短小,總共200行左右。
框架缺點:
① 效率低下
② 未考慮并發(fā),非線程安全
后續(xù)還會介紹如何應(yīng)用這個框架開發(fā)一個代碼片段管理系統(tǒng)
附:框架源代碼
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>();
}
}
}
}
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();
}
}
}
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
winform 實現(xiàn)選擇文件和選擇文件夾對話框的簡單實例
下面小編就為大家?guī)硪黄獁inform 實現(xiàn)選擇文件和選擇文件夾對話框的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01Unity的Console的控制類LogEntries深入解析與實用案例
這篇文章主要為大家介紹了Unity的Console的控制類LogEntries深入解析與實用案例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07C#使用RenderControl將GridView控件導(dǎo)出到EXCEL的方法
這篇文章主要介紹了C#使用RenderControl將GridView控件導(dǎo)出到EXCEL的方法,是C#應(yīng)用程序設(shè)計中非常實用的一個功能,需要的朋友可以參考下2014-08-08在類庫或winform項目中打開另一個winform項目窗體的方法
這篇文章主要介紹了在類庫或winform項目中打開另一個winform項目窗體的方法,可以實現(xiàn)Winform項目間窗體的調(diào)用,在進行Winform項目開發(fā)中非常具有實用價值,需要的朋友可以參考下2014-11-11