菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
271
0

EF架构~XMLRepository仓储的实现~续(XAttribute方式)

原创
05/13 14:22
阅读数 3706

回到目录

之前我写过关于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) ?? 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)))//只找简单类型的属性
                {
                    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一共六大仓储了,感觉已经可以写一篇

关于如何实现仓储的文章了,哈哈。

回到目录

发表评论

0/200
271 点赞
0 评论
收藏
为你推荐 换一批