Created
December 4, 2012 17:56
-
-
Save msarchet/4206865 to your computer and use it in GitHub Desktop.
A Quick Overview of a problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class IRedisClientsManagerExtensions() | |
{ | |
public static ManagedList<T> GetManagedList(this IRedisClientsManager manager, string key) | |
{ | |
return new ManagedList<T>(manager, key); | |
} | |
} | |
public class ManagedList<T> : IList<T> | |
{ | |
private string _key = null; | |
private IRedisClientsManager _manager; | |
private ManagedList() { } | |
public ManagedList(IRedisClientsManager manager, string key) | |
{ | |
_key = key; | |
_manager = manager; | |
} | |
#region IList<T> Members | |
private IRedisClient GetClient() | |
{ | |
return _manager.GetClient(); | |
} | |
private List<T> getRedisList() | |
{ | |
using(var redis = GetClient()) | |
{ | |
var client = redis.As<T>(); | |
return client.Lists[_key].ToList(); | |
} | |
} | |
public int IndexOf(T item) | |
{ | |
return getRedisList().IndexOf(item); | |
} | |
public void Insert(int index, T item) | |
{ | |
using(var redis = GetClient()) | |
{ | |
redis.As<T>().Lists[_key].Insert(index, item); | |
} | |
} | |
public void RemoveAt(int index) | |
{ | |
using(var redis = GetClient()) | |
{ | |
redis.As<T>().Lists[_key].RemoveAt(index); | |
} | |
} | |
public T this[int index] | |
{ | |
get | |
{ | |
return getRedisList()[index]; | |
} | |
set | |
{ | |
using(var redis = GetClient()) | |
{ | |
redis.As<T>().Lists[_key][index] = value; | |
} | |
} | |
} | |
#endregion | |
#region ICollection<T> Members | |
public void Add(T item) | |
{ | |
using(var redis = GetClient()) | |
{ | |
redis.As<T>().Lists[_key].Add(item); | |
} | |
} | |
public void Clear() | |
{ | |
using(var redis = GetClient()) | |
{ | |
redis.As<T>().Lists[_key].Clear(); | |
} | |
} | |
public bool Contains(T item) | |
{ | |
return getRedisList().Contains(item); | |
} | |
public void CopyTo(T[] array, int arrayIndex) | |
{ | |
getRedisList().CopyTo(array, arrayIndex); | |
} | |
public int Count | |
{ | |
get { return getRedisList().Count(); } | |
} | |
public bool IsReadOnly | |
{ | |
get { return false; } | |
} | |
public bool Remove(T item) | |
{ | |
var index = this.IndexOf(item); | |
if (index != -1) | |
{ | |
this.RemoveAt(index); | |
return true; | |
} | |
return false; | |
} | |
#endregion | |
#region IEnumerable<T> Members | |
public IEnumerator<T> GetEnumerator() | |
{ | |
return getRedisList().GetEnumerator(); | |
} | |
#endregion | |
#region IEnumerable Members | |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() | |
{ | |
return ((IEnumerable)getRedisList()).GetEnumerator(); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
okie i'm going to be really picky about the code-style to get it more consistent with everything else in the code-base.
But the code itself looks fine :)