<?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;
}