PageRenderTime 11ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/src/LinFu.Proxy/ProxyCache.cs

http://github.com/philiplaureano/LinFu
C# | 63 lines | 31 code | 6 blank | 26 comment | 0 complexity | 37604ac5568de47cf566a0809f948fa5 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using LinFu.IoC.Configuration;
  4. using LinFu.Proxy.Interfaces;
  5. namespace LinFu.Proxy
  6. {
  7. /// <summary>
  8. /// Represents the default implementation of the <see cref="IProxyCache" /> interface.
  9. /// </summary>
  10. [Implements(typeof(IProxyCache), LifecycleType.OncePerRequest)]
  11. internal class ProxyCache : IProxyCache
  12. {
  13. private static readonly Dictionary<ProxyCacheEntry, Type> _cache =
  14. new Dictionary<ProxyCacheEntry, Type>(new ProxyCacheEntry.EqualityComparer());
  15. /// <summary>
  16. /// Determines whether or not the cache contains an existing proxy type
  17. /// that is derived from the <paramref name="baseType" /> and implements
  18. /// the given <paramref name="baseInterfaces" />.
  19. /// </summary>
  20. /// <param name="baseType">The base type of the dynamically-generated proxy type.</param>
  21. /// <param name="baseInterfaces">The list of interfaces that the generated proxy type must implement.</param>
  22. /// <returns>Returns <c>true</c> if the proxy type already exists; otherwise, it will return <c>false.</c></returns>
  23. public bool Contains(Type baseType, params Type[] baseInterfaces)
  24. {
  25. var entry = new ProxyCacheEntry(baseType, baseInterfaces);
  26. return _cache.ContainsKey(entry);
  27. }
  28. /// <summary>
  29. /// Retrieves an existing proxy type from the cache.
  30. /// </summary>
  31. /// <param name="baseType">The base type of the dynamically-generated proxy type.</param>
  32. /// <param name="baseInterfaces">The list of interfaces that the generated proxy type must implement.</param>
  33. /// <returns>
  34. /// Returns a valid <see cref="Type" /> if the type already exists; otherwise, it might return <c>null</c> or opt
  35. /// to throw an exception.
  36. /// </returns>
  37. public Type Get(Type baseType, params Type[] baseInterfaces)
  38. {
  39. var entry = new ProxyCacheEntry(baseType, baseInterfaces);
  40. return _cache[entry];
  41. }
  42. /// <summary>
  43. /// Stores a proxy type in the cache.
  44. /// </summary>
  45. /// <param name="result">The proxy type to be stored.</param>
  46. /// <param name="baseType">The base type of the dynamically-generated proxy type.</param>
  47. /// <param name="baseInterfaces">The list of interfaces that the generated proxy type must implement.</param>
  48. public void Store(Type result, Type baseType, params Type[] baseInterfaces)
  49. {
  50. var entry = new ProxyCacheEntry(baseType, baseInterfaces);
  51. lock (_cache)
  52. {
  53. _cache[entry] = result;
  54. }
  55. }
  56. }
  57. }