PageRenderTime 36ms CodeModel.GetById 21ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

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