首页 > 学院 > 开发设计 > 正文

应用XML作为数据库的快速开发框架

2019-11-17 02:42:23
字体:
来源:转载
供稿:网友
应用xml作为数据库的快速开发框架

背景

我经常应用C#开发一些小的桌面程序,这些桌面程序往往有以下几个特点:

  1. 程序比较小,开发周期很短。
  2. 程序的数据量不大,多数情况下不超过1万行记录。
  3. 对程序的性能要求不高。
  4. 程序并发很少或者基本没有。
  5. 尽量程序部署简单。

因为C#程序很多情况下都是CURD,结合上面的需求,我一直考虑做一个简单的框架,以达到快速开发的目的。应用XML序列化(XmlSerializer)功能,我开发了一个简单符合上面要求的底层框架。

框架思路

我准备用XML文件作为数据存储,为了保证数据同步,同时在内存中存储一份数据,每次操作时,都是操作内存中的数据,操作完之后再同步到数据库中。另外,为了保证框架的易用性,我把底层实现写成了一个泛型类,所有操作类继承此泛型类。

框架功能描述

框架主要包括以下几个功能:

  1. 应用XML文件作为数据库,不依赖其他数据库系统。
  2. 对外提供基本的CURD功能。
  3. 减少配置,做到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>

由上面的例子可以看到,应用此框架进行开发还是非常容易的。

总结

框架优点:
  1. 快速开发,完全不需要考虑底层
  2. 易于部署
  3. 框架代码比较短小,总共200行左右。
框架缺点:
  1. 效率低下
  2. 未考虑并发,非线程安全

我会在下一篇文章里面介绍如何应用这个框架开发一个代码片段管理系统

附:框架源代码

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 =
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表