国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

應用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 =
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建水县| 宣武区| 灵山县| 达孜县| 五河县| 临汾市| 大姚县| 凤台县| 绵阳市| 罗源县| 沙雅县| 沁水县| 浠水县| 肇州县| 灵璧县| 南康市| 临湘市| 新密市| 樟树市| 华亭县| 文水县| 历史| 广元市| 新安县| 县级市| 上高县| 屏南县| 沾益县| 浑源县| 志丹县| 吉木萨尔县| 和田县| 德惠市| 铜鼓县| 平定县| 东山县| 聂拉木县| 长沙县| 区。| 长岭县| 文成县|