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();
        }
    }

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

回到目录

posted @ 2014-04-15 18:02  张占岭  阅读(2638)  评论(0编辑  收藏  举报