C#应用XML作为数据库的快速开发框架实现方法
作者:shichen2014 时间:2024-01-19 12:00:01
本文实例讲述了C#应用XML作为数据库的快速开发框架实现方法。分享给大家供大家参考。具体如下:
背景
我经常应用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();
}
}
}
希望本文所述对大家的C#程序设计有所帮助。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python学习之流程控制与条件判断总结
![](https://img.aspxhome.com/file/2023/8/70148_0s.jpg)
python 实现定时任务的四种方式
Golang编译器介绍
教你怎么用Python实现多路径迷宫
![](https://img.aspxhome.com/file/2023/4/112784_0s.png)
PHP和JavaScrip分别获取关联数组的键值示例代码
PHP动态页生成静态页的3种常用方法
python+opencv实现堆叠图片
![](https://img.aspxhome.com/file/2023/8/105588_0s.jpg)
python实现读取excel文件中所有sheet操作示例
![](https://img.aspxhome.com/file/2023/4/127144_0s.png)
FrontPage2002简明教程八:站点的管理
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017113823118s.jpg)
Python面向对象编程之类的继承
![](https://img.aspxhome.com/file/2023/6/83896_0s.png)
php中常量DIRECTORY_SEPARATOR用法深入分析
flask-socketio实现前后端实时通信的功能的示例
![](https://img.aspxhome.com/file/2023/9/97379_0s.png)
手机版远程网站文件删除ASP程序
让你一文弄懂Pandas文本数据处理
![](https://img.aspxhome.com/file/2023/8/61218_0s.png)
javascript实现圣旨卷轴展开效果(代码分享)
![](https://img.aspxhome.com/file/2023/1/136801_0s.gif)
vue-cli的eslint相关用法
python中单下划线(_)和双下划线(__)的特殊用法
详解Tensorflow数据读取有三种方式(next_batch)
![](https://img.aspxhome.com/file/2023/1/71461_0s.jpg)
vue-router懒加载速度缓慢问题及解决方法
![](https://img.aspxhome.com/file/2023/5/133005_0s.png)