zoukankan      html  css  js  c++  java
  • EF架构~XMLRepository仓储的实现

    回到目录

    对于数据仓储大家应该都很熟悉了,它一般由几个仓储规范和实现它的具体类组成,而仓储的接口与架构本身无关,对于仓储的实现,你可以选择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();
            }
        }

    感觉面向对象也是一种病,但这种病我认为是正确的,当你对它的理解达到某种程度时,这种病就会犯了,并且你会相信,世间万物,皆为对象

    回到目录

  • 相关阅读:
    LeetCode.1(两数之和)
    LeetCode.56(合并区间)
    c++ 数字与字符串的相互转换
    软件工程作业-面向对象方法学
    linux终端下解决you need to be root to perform this command
    vue中 v-bind 与 v-model的区别
    vue的核心:虚拟DOM 和 diff 算法
    弱实体集的必要性、属性随笔
    Ubuntu 18.04下Intel SGX应用程序程序开发——获得OCALL调用的返回值
    Ubuntu 18.04 INTEL SGX 修改案例打印Hello Enclave
  • 原文地址:https://www.cnblogs.com/lori/p/3666816.html
Copyright © 2011-2022 走看看