EF架構~XMLRepository倉儲的實現
對于數據倉儲大家(jia)應該都(dou)很熟悉了,它(ta)(ta)一般由(you)幾個倉儲規范(fan)和實現它(ta)(ta)的(de)(de)具體類組成,而倉儲的(de)(de)接口與架構本身無關,對于倉儲的(de)(de)實現,你可以選擇linq2Sql,EF,Nosql,及(ji)XML
等等,之前我介紹過linq2Sql,ef和nosql(redis)的倉儲實現,今天主要說一(yi)下xml倉儲的實現(xian)。
下面的相關核心代碼
XML實體基類
/// <summary> /// XML實體基(ji)類(lei) /// </summary> public abstract class XMLEntity { private string id = Guid.NewGuid().ToString(); /// <summary> /// XML實(shi)體主鍵 /// </summary> public string RootID { get { return id; } set { id = value; } } }
XML實體的倉儲操作
/// <summary> /// XML文件數(shu)據倉儲 /// XML結構(gou)為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)))//只找簡單(dan)類型的屬性 { 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)))//只(zhi)找簡單類型的屬性 { 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)))//只(zhi)找(zhao)簡(jian)單類型的(de)屬性 { 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(); } }
感覺面向對象也是一種病,但這種病我認為是正確的,當你對它的理解達到某種程度時,這種病就會犯了,并且你會相信,世間萬物,皆為對象。