PageRenderTime 87ms CodeModel.GetById 82ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.IoC/BaseFactoryStorage.cs

http://github.com/philiplaureano/LinFu
C# | 59 lines | 28 code | 7 blank | 24 comment | 1 complexity | d6c4e9d142e622a6b1e3b00fe1cb52e0 MD5 | raw file
 1using System.Collections.Generic;
 2using LinFu.IoC.Interfaces;
 3
 4namespace LinFu.IoC
 5{
 6    /// <summary>
 7    ///     Represents the default base implementation of the <see cref="IFactoryStorage" /> class.
 8    /// </summary>
 9    public abstract class BaseFactoryStorage : IFactoryStorage
10    {
11        private readonly Dictionary<IServiceInfo, IFactory> _entries = new Dictionary<IServiceInfo, IFactory>();
12        private readonly object _lock = new object();
13
14
15        /// <summary>
16        ///     Determines which factories should be used
17        ///     for a particular service request.
18        /// </summary>
19        /// <param name="serviceInfo">The <see cref="IServiceInfo" /> object that describes the target factory.</param>
20        /// <returns>A factory instance.</returns>
21        public virtual IFactory GetFactory(IServiceInfo serviceInfo)
22        {
23            if (_entries.ContainsKey(serviceInfo))
24                return _entries[serviceInfo];
25
26            return null;
27        }
28
29        /// <summary>
30        ///     Adds a <see cref="IFactory" /> to the current <see cref="IFactoryStorage" /> object.
31        /// </summary>
32        /// <param name="serviceInfo">The <see cref="IServiceInfo" /> object that describes the target factory.</param>
33        /// <param name="factory">The <see cref="IFactory" /> instance that will create the object instance.</param>
34        public virtual void AddFactory(IServiceInfo serviceInfo, IFactory factory)
35        {
36            lock (_lock)
37            {
38                _entries[serviceInfo] = factory;
39            }
40        }
41
42        /// <summary>
43        ///     Determines whether or not a factory exists in storage.
44        /// </summary>
45        /// <param name="serviceInfo">The <see cref="IServiceInfo" /> object that describes the target factory.</param>
46        /// <returns>Returns <c>true</c> if the factory exists; otherwise, it will return <c>false</c>.</returns>
47        public virtual bool ContainsFactory(IServiceInfo serviceInfo)
48        {
49            return _entries.ContainsKey(serviceInfo);
50        }
51
52        /// <summary>
53        ///     Gets a value indicating the list of <see cref="IServiceInfo" /> objects
54        ///     that describe each available factory in the current <see cref="IFactoryStorage" />
55        ///     instance.
56        /// </summary>
57        public virtual IEnumerable<IServiceInfo> AvailableFactories => _entries.Keys;
58    }
59}