回到目錄
對于資料倉儲大家應該都很熟悉了,它一般由幾個倉儲規範和實作它的具體類組成,而倉儲的接口與架構本身無關,對于倉儲的實作,你可以選擇linq2Sql,EF,Nosql,及XML
等等,之前我介紹過linq2Sql,ef和nosql(redis)的倉儲實作,今天主要說一下xml倉儲的實作。
下面的相關核心代碼
XML實體基類
/// <summary>
/// XML實體基類
/// </summary>
public abstract class XMLEntity
{
private string id = Guid.NewGuid().ToString();
/// <summary>
/// XML實體主鍵
/// </summary>
public string RootID
{
get { return id; }
set { id = value; }
}
}
XML實體的倉儲操作
/// <summary>
/// XML檔案資料倉儲
/// XML結構為Element
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public class XMLRepository<TEntity> :
IRepository<TEntity>
where TEntity : XMLEntity, new()
{
XDocument _doc;
string _filePath;
static object lockObj = new object();
public XMLRepository(string filePath)
{
_filePath = filePath;
_doc = XDocument.Load(filePath);
}
public void Insert(TEntity item)
{
if (item == null)
throw new ArgumentException("The database entity can not be null.");
XElement db = new XElement(typeof(TEntity).Name);
foreach (var member in item.GetType()
.GetProperties()
.Where(i => i.PropertyType.IsValueType
|| i.PropertyType == typeof(String)))//隻找簡單類型的屬性
{
db.Add(new XElement(member.Name, new XAttribute("value", member.GetValue(item, null))));
}
_doc.Root.Add(db);
lock (lockObj)
{
_doc.Save(_filePath);
}
}
public void Delete(TEntity item)
{
if (item == null)
throw new ArgumentException("The database entity can not be null.");
XElement xe = (from db in _doc.Root.Elements(typeof(TEntity).Name)
where db.Element("RootID").Attribute("value").Value == item.RootID
select db).Single() as XElement;
xe.Remove();
lock (lockObj)
{
_doc.Save(_filePath);
}
}
public void Update(TEntity item)
{
if (item == null)
throw new ArgumentException("The database entity can not be null.");
XElement xe = (from db in _doc.Root.Elements(typeof(TEntity).Name)
where db.Element("RootID").Attribute("value").Value == item.RootID
select db).Single();
try
{
foreach (var member in item.GetType()
.GetProperties()
.Where(i => i.PropertyType.IsValueType
|| i.PropertyType == typeof(String)))//隻找簡單類型的屬性
{
xe.Add(new XElement(member.Name, new XAttribute("value", member.GetValue(item, null))));
}
lock (lockObj)
{
_doc.Save(_filePath);
}
}
catch
{
throw;
}
}
public IQueryable<TEntity> GetModel()
{
IEnumerable<XElement> list = _doc.Root.Elements(typeof(TEntity).Name);
IList<TEntity> returnList = new List<TEntity>();
foreach (var item in list)
{
TEntity entity = new TEntity();
foreach (var member in entity.GetType()
.GetProperties()
.Where(i => i.PropertyType.IsValueType
|| i.PropertyType == typeof(String)))//隻找簡單類型的屬性
{
if (item.Attribute(member.Name) != null)
member.SetValue(entity, Convert.ChangeType(item.Element(member.Name).Attribute("value").Value, member.PropertyType), null);
}
returnList.Add(entity);
}
return returnList.AsQueryable();
}
public TEntity Find(params object[] id)
{
return GetModel().FirstOrDefault(i => i.RootID == Convert.ToString(id[0]));
}
public void SetDbContext(IUnitOfWork unitOfWork)
{
throw new NotImplementedException();
}
}
感覺面向對象也是一種病,但這種病我認為是正确的,當你對它的了解達到某種程度時,這種病就會犯了,并且你會相信,世間萬物,皆為對象。
作者:倉儲大叔,張占嶺,
榮譽:微軟MVP
QQ:853066980
支付寶掃一掃,為大叔打賞!