EF架構~XMLRepository倉(cang)儲(chu)的(de)實現~續(XAttribute方(fang)式)
之前我寫過關于XMLRepository倉儲的實現的文章,主要是(shi)針對XElement方(fang)式的(de)(de),對于(yu)XML的(de)(de)結構,一般來說有(you)兩種(zhong),一是(shi)使用節(jie)點(dian)方(fang)式的(de)(de),我(wo)習慣(guan)稱為XElement方(fang)式,別一種(zhong)是(shi)屬(shu)性(xing)方(fang)式的(de)(de),我(wo)習慣(guan)稱為XAttribute,這兩種(zhong)方(fang)式,我(wo)比較取向于(yu)后面的(de)(de),好處就是(shi)更簡潔,將(jiang)C#里(li)(li)的(de)(de)類看作(zuo)節(jie)點(dian),而將(jiang)C#里(li)(li)的(de)(de)屬(shu)性(xing)看作(zuo)是(shi)節(jie)點(dian)的(de)(de)每個特性(xing)(XAttribute),這種(zhong)方(fang)式感覺更容易被(bei)人接(jie)受!
如果(guo)您(nin)還看不(bu)懂這兩方式指的(de)是什么,就(jiu)看一下兩種方式的(de)XML舉例吧
XElement方式
<User>
<id>1</id>
<name>zzl</name>
</User>
XAttribute方(fang)式
<User id="1" name="zzl" />
怎么(me)樣,第二種方(fang)式(shi)更簡潔吧,但要注(zhu)意,XAttribute方(fang)式(shi)書寫時,需要為每個特性的(de)值加雙引號的(de),因為linq to xml認為每個XAttribute都(dou)是(shi)字符(fu)型(xing)的(de),在進行操作時,你可(ke)以根(gen)據(ju)實體(ti)類型(xing)進行轉換的(de)
下面看一(yi)下我為(wei)XAttribute方(fang)式進行封裝的Repository吧
XML2Repository.cs源(yuan)代碼
/// <summary> /// XML文件數據倉儲 /// XML結構為Attribute /// </summary> /// <typeparam name="TEntity"></typeparam> public class XML2Repository<TEntity> : IRepository<TEntity> where TEntity : XMLEntity, new() { XDocument _doc; string _filePath; static object lockObj = new object(); public XML2Repository(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 XAttribute(member.Name, member.GetValue(item, null) ?? string.Empty)); } _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.Attribute("RootID").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.Attribute("RootID").Value == item.RootID select db).Single(); try { foreach (var member in item.GetType() .GetProperties() .Where(i => i.PropertyType.IsValueType || i.PropertyType == typeof(String))) { xe.SetAttributeValue(member.Name, member.GetValue(item, null) ?? string.Empty); } 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)簡單類(lei)型(xing)的屬性 { if (item.Attribute(member.Name) != null) member.SetValue(entity, Convert.ChangeType(item.Attribute(member.Name).Value, member.PropertyType), null);//動態(tai)轉換為指(zhi)定類型 } 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(); } }
至此,對于XML的倉(cang)儲實現就(jiu)已經完(wan)都講(jiang)完(wan)了,而對于其它倉(cang)儲我也都在這(zhe)前介紹過(guo),其中包括EFRepsitory,LinqRepository,MemoryRepositoy,RedisRepository再加(jia)上今天的XMLRepository和XML2Repository一(yi)共六(liu)大(da)倉(cang)儲了,感覺已經可以寫一(yi)篇
關于如(ru)何(he)實現倉儲的(de)文(wen)章了,哈哈。