EF架構~XMLRepository倉儲的實現~續(XAttribute方式)

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   ar   for   

回到目錄

之前我寫過關於XMLRepository倉儲的實現的文章,主要是針對XElement方式的,對於XML的結構,一般來說有兩種,一是使用節點方式的,我習慣稱為XElement方式,別一種是屬性方式的,我習慣稱為XAttribute,這兩種方式,我比較取向於後面的,好處就是更簡潔,將C#裡的類看作節點,而將C#裡的屬性看作是節點的每個特性(XAttribute),這種方式感覺更容易被人接受!

如果您還看不懂這兩方式指的是什麼,就看一下兩種方式的XML舉例吧

XElement方式

<User> <id>1</id> <name>zzl</name></User>

XAttribute方式

<User id="1" name="zzl" />

怎麼樣,第二種方式更簡潔吧,但要注意,XAttribute方式書寫時,需要為每個特性的值加雙引號的,因為linq to xml認為每個XAttribute都是字元型的,在進行操作時,你可以根據實體類型進行轉換的

下面看一下我為XAttribute方式進行封裝的Repository吧

XML2Repository.cs原始碼

    /// <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)));            }            _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));                }                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.Attribute(member.Name).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();        }    }

至此,對於XML的倉儲實現就已經完都講完了,而對於其它倉儲我也都在這前介紹過,其中包括EFRepsitory,LinqRepository,MemoryRepositoy,RedisRepository再加上今天的XMLRepository和XML2Repository一共六大倉儲了,感覺已經可以寫一篇

關於如何?倉儲的文章了,哈哈。

回到目錄

EF架構~XMLRepository倉儲的實現~續(XAttribute方式)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.