Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Created simple implementation of MiniConcurrentDictionary for SqlProf…

…iler.
  • Loading branch information...
commit 35e6cc21e2c2f7ae6f88167a1a1f4bae14995fbb 1 parent e28dba7
@akmad authored
Showing with 170 additions and 3 deletions.
  1. +170 −3 MvcMiniProfiler/DotNet35Support.cs
View
173 MvcMiniProfiler/DotNet35Support.cs
@@ -1,6 +1,11 @@
using System;
+
+using System.Collections;
+
using System.Collections.Generic;
+using System.Threading;
+
#if !CSHARP30
using System.Collections.Concurrent;
#endif
@@ -70,12 +75,25 @@ public Tuple(T1 item1, T2 item2)
public override int GetHashCode()
{
- throw new NotImplementedException();
+ return Item1.GetHashCode() ^ Item2.GetHashCode();
}
public override bool Equals(object obj)
{
- throw new NotImplementedException();
+ if (obj == null)
+ {
+ return false;
+ }
+
+ var other = obj as Tuple<T1, T2>;
+ if (other == null)
+ {
+ return false;
+ }
+
+ var comparer1 = EqualityComparer<T1>.Default;
+ var comparer2 = EqualityComparer<T2>.Default;
+ return comparer1.Equals(Item1, other.Item1)&& comparer2.Equals(Item2, other.Item2);
}
public override string ToString()
@@ -84,12 +102,161 @@ public override string ToString()
}
}
- internal class MiniConcurrentDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IConcurrentDictionary<TKey, TValue>
+ /// <summary>
+ /// This is a dictionary that only implements the methods required by the <see cref="SqlProfiler"/>. Concurrency is very simply implemented via a <see cref="ReaderWriterLockSlim"/>.
+ /// </summary>
+ /// <typeparam name="TKey">The type of the key.</typeparam>
+ /// <typeparam name="TValue">The type of the value.</typeparam>
+ internal class MiniConcurrentDictionary<TKey, TValue> : IConcurrentDictionary<TKey, TValue>
{
+ private Dictionary<TKey, TValue> _dict = new Dictionary<TKey, TValue>();
+ private ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
+
+ public TValue this[TKey key]
+ {
+ get
+ {
+ _lock.EnterReadLock();
+ try
+ {
+ return _dict[key];
+ }
+ finally
+ {
+ _lock.ExitReadLock();
+ }
+ }
+ set
+ {
+ _lock.EnterWriteLock();
+ try
+ {
+ _dict[key] = value;
+ }
+ finally
+ {
+ _lock.ExitWriteLock();
+ }
+ }
+ }
+
+ public ICollection<TValue> Values
+ {
+ get
+ {
+ _lock.EnterReadLock();
+ try
+ {
+ return _dict.Values;
+ }
+ finally
+ {
+ _lock.ExitReadLock();
+ }
+ }
+ }
+
+ public bool TryGetValue(TKey key, out TValue value)
+ {
+ var result = false;
+ _lock.EnterReadLock();
+ try
+ {
+ result = _dict.TryGetValue(key, out value);
+ }
+ finally
+ {
+ _lock.ExitReadLock();
+ }
+
+ return result;
+ }
+
public bool TryRemove(TKey key, out TValue value)
{
+ var result = false;
+ _lock.EnterWriteLock();
+ try
+ {
+ if (_dict.TryGetValue(key, out value))
+ {
+ result = _dict.Remove(key);
+ }
+ }
+ finally
+ {
+ _lock.ExitWriteLock();
+ }
+
+ return result;
+ }
+
+ #region Not Implemented
+
+ public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
+ {
throw new NotImplementedException();
}
+
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+
+ public void Add(KeyValuePair<TKey, TValue> item)
+ {
+ throw new NotImplementedException();
+ }
+
+ public void Clear()
+ {
+ throw new NotImplementedException();
+ }
+
+ public bool Contains(KeyValuePair<TKey, TValue> item)
+ {
+ throw new NotImplementedException();
+ }
+
+ public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
+ {
+ throw new NotImplementedException();
+ }
+
+ public bool Remove(KeyValuePair<TKey, TValue> item)
+ {
+ throw new NotImplementedException();
+ }
+
+ public int Count
+ {
+ get { throw new NotImplementedException(); }
+ }
+ public bool IsReadOnly
+ {
+ get { throw new NotImplementedException(); }
+ }
+ public bool ContainsKey(TKey key)
+ {
+ throw new NotImplementedException();
+ }
+
+ public void Add(TKey key, TValue value)
+ {
+ throw new NotImplementedException();
+ }
+
+ public bool Remove(TKey key)
+ {
+ throw new NotImplementedException();
+ }
+
+ public ICollection<TKey> Keys
+ {
+ get { throw new NotImplementedException(); }
+ }
+
+ #endregion
}
#else
internal class MiniConcurrentDictionary<TKey, TValue> : ConcurrentDictionary<TKey, TValue>, IConcurrentDictionary<TKey, TValue>
Please sign in to comment.
Something went wrong with that request. Please try again.