NoSQLRedis学习笔记~是下呢Redis实现一个囤了,RedisRepository来了

回目录

事先写了多关于仓储的章,所以,自己习惯把自己让仓储大叔,上次勾勒的XMLRepository得到了大家的好评,也产生成千上万情人让自身发email,进行有文化之探赜索隐,今天根本来促成一个RedisRepository,它一直是集成IRepository接口的,我这边的Redis仓储主要服务啊复杂性类型的政工,对于只有存string这种要求,不待采取它们。

对Redis仓储以及说,它与XML仓储有些不同,由于XML文件一般存储于WWW服务器,所以没网络通讯问题,而redis一般安排于第三高服务器上,我们一般叫NoSQL服务器,它跟WWW通讯是经socket协议就的,正是要把,我们于展开仓储设计时,应该考虑到何以错过自由它的资源,因为这种资源是非托管的,所以要人工干预一下,.net提供了using关键字来开就行,而每个动作写using这肯定是未友善的,所以,我者redis仓储是在析构方法里完成对资源的灭绝之,请看源代码:

第一是redis基类,它是贯彻统一操作的前提

   /// <summary>
    /// Redis实体基类,所有redis实体类都应该集成它
    /// </summary>
    public abstract class RedisEntity
    {
        public RedisEntity()
        {
            RootID = Guid.NewGuid().ToString();
        }
        /// <summary>
        /// Redis实体主键,方法查询,删除,更新等操作
        /// </summary>
        public virtual string RootID { get; set; }
    }

下面才是RedisRepository仓储NoSQL的代码

    /// <summary>
    /// Redis仓储实现
    /// </summary>
    public class RedisRepository<TEntity> :
        IDisposable,
        IRepository<TEntity>
        where TEntity : RedisEntity
    {
        IRedisClient redisDB;
        IRedisTypedClient<TEntity> redisTypedClient;
        IRedisList<TEntity> table;
        public RedisRepository()
        {
            redisDB = RedisManager.GetClient();
            redisTypedClient = redisDB.GetTypedClient<TEntity>();
            table = redisTypedClient.Lists[typeof(TEntity).Name];
        }

        #region IRepository<TEntity>成员
        public void SetDbContext(IUnitOfWork unitOfWork)
        {
            throw new NotImplementedException();
        }

        public void Insert(TEntity item)
        {
            if (item != null)
              {
                redisTypedClient.AddItemToList(table, item);
                redisDB.Save();
              }

        }

        public void Delete(TEntity item)
        {
            if (item != null)
            {
                var entity = Find(item.RootID);
                redisTypedClient.RemoveItemFromList(table, entity);
                redisDB.Save();
            }
        }

        public void Update(TEntity item)
        {
            if (item != null)
            {
                var old = Find(item.RootID);
                if (old != null)
                {
                    redisTypedClient.RemoveItemFromList(table, old);
                    redisTypedClient.AddItemToList(table, item);
                    redisDB.Save();
                }
            }
        }

        public IQueryable<TEntity> GetModel()
        {
            return table.GetAll().AsQueryable();
        }

        public TEntity Find(params object[] id)
        {
            return table.Where(i => i.RootID == (string)id[0]).FirstOrDefault();
        }
        #endregion

        #region IDisposable成员
        public void Dispose()
        {
            this.ExplicitDispose();
        }
        #endregion

        #region Protected Methods

        /// <summary>
        /// Provides the facility that disposes the object in an explicit manner,
        /// preventing the Finalizer from being called after the object has been
        /// disposed explicitly.
        /// </summary>
        protected void ExplicitDispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected void Dispose(bool disposing)
        {
            if (disposing)//清除非托管资源
            {
                table = null;
                redisTypedClient = null;
                redisDB.Dispose();
            }
        }
        #endregion

        #region Finalization Constructs
        /// <summary>
        /// Finalizes the object.
        /// </summary>
        ~RedisRepository()
        {
            this.Dispose(false);
        }
        #endregion
    }
}

下是当控制台中开展仓储的调用

        IRepository<Car> repository = new Redis.Data.Core.RedisRepository<Car>();
            repository.Insert(new Car { ID = 3, Name = "占" });
            var entity = repository.GetModel().Where(i => i.ID == 3).FirstOrDefault();
            entity.Name = "修改了";
            repository.Update(entity);
            repository.GetModel().ToList().ForEach(e =>
            {
                Console.WriteLine(e.ID + "/" + e.RootID + "/" + e.Name);
            });

脚是贯彻的结果的截图

NoSQL 1

返回目录

网站地图xml地图