回到目录
之前写了不少关于仓储的文章,所以,自己习惯把自己叫仓储大叔,上次写的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仓储的代码
/// <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);
});
下面是实现的结果的截图
作者:仓储大叔,张占岭,
荣誉:微软MVP
QQ:853066980
支付宝扫一扫,为大叔打赏!
