PageRenderTime 122ms CodeModel.GetById 41ms app.highlight 3ms RepoModel.GetById 57ms app.codeStats 0ms

/src/LinFu.AOP.Interfaces/BootStrapRegistry.cs

http://github.com/philiplaureano/LinFu
C# | 71 lines | 47 code | 9 blank | 15 comment | 1 complexity | 3bfec87fd9665c006ae57bf92453ff31 MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using LinFu.Reflection;
 4
 5namespace LinFu.AOP.Interfaces
 6{
 7    /// <summary>
 8    ///     Represents a registry class that bootstraps components into memory when the application starts.
 9    /// </summary>
10    public sealed class BootStrapRegistry
11    {
12        private readonly IList<IBootStrappedComponent> _components = new List<IBootStrappedComponent>();
13
14        private BootStrapRegistry()
15        {
16            Initialize();
17        }
18
19        /// <summary>
20        ///     Gets the value indicating the BootStrapRegistry instance.
21        /// </summary>
22        public static BootStrapRegistry Instance => NestedLoader.Instance;
23
24        /// <summary>
25        ///     Initializes the BootStrapRegistry.
26        /// </summary>
27        private void Initialize()
28        {
29            lock (_components)
30            {
31                _components.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
32                foreach (var component in _components)
33                    try
34                    {
35                        component.Initialize();
36                    }
37                    catch (Exception ex)
38                    {
39                        var componentName = component != null ? component.GetType().Name : "(unknown)";
40                        var message = string.Format("{0} Error: Unable to load component '{1}' - {2}",
41                            GetType().FullName,
42                            componentName, ex);
43
44                        throw new BootstrapException(message, ex);
45                    }
46            }
47        }
48
49        /// <summary>
50        ///     Returns the list of components that have been initialized by the bootstrapper.
51        /// </summary>
52        /// <returns></returns>
53        public IEnumerable<IBootStrappedComponent> GetComponents()
54        {
55            return _components;
56        }
57
58
59        private class NestedLoader
60        {
61            internal static readonly BootStrapRegistry Instance;
62
63            // Explicit static constructor to tell C# compiler
64            // not to mark type as beforefieldinit
65            static NestedLoader()
66            {
67                Instance = new BootStrapRegistry();
68            }
69        }
70    }
71}