Commit e76ace8

mokhan <mokhan@ce5e1baf-6525-42e4-a1b2-857ea38da20a>
2009-03-20 02:41:55
git-svn-id: https://svn.xp-dev.com/svn/mokhan-mo.money@85 ce5e1baf-6525-42e4-a1b2-857ea38da20a
1 parent 00a8624
trunk/product/MyMoney/boot/container/run_mass_component_registration_in_to_the.cs
@@ -0,0 +1,39 @@
+using System;
+using Castle.MicroKernel.Registration;
+using Castle.Windsor;
+using MoMoney.Infrastructure.Container.Windsor;
+using MoMoney.Infrastructure.Container.Windsor.configuration;
+using MoMoney.Utility.Core;
+
+namespace MoMoney.boot.container
+{
+    internal class run_mass_component_registration_in_to_the : ICommand
+    {
+        readonly Func<IWindsorContainer> container;
+        readonly IComponentExclusionSpecification criteria_to_satisfy;
+        readonly IRegistrationConfiguration configuration;
+
+        public run_mass_component_registration_in_to_the(Func<IWindsorContainer> container,
+                                                         IComponentExclusionSpecification criteria_to_satisfy,
+                                                         IRegistrationConfiguration configuration)
+        {
+            this.container = container;
+            this.criteria_to_satisfy = criteria_to_satisfy;
+            this.configuration = configuration;
+        }
+
+        public void run()
+        {
+            container().Register(
+                AllTypes
+                    .Pick()
+                    .FromAssembly(GetType().Assembly)
+                    .WithService
+                    .LastInterface()
+                    //.FirstInterface()
+                    .Unless(criteria_to_satisfy.is_satisfied_by)
+                    .Configure(x => configuration.configure(x))
+                );
+        }
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/boot/container/tear_down_the_container.cs
@@ -0,0 +1,13 @@
+using MoMoney.Infrastructure.Container;
+using MoMoney.Utility.Core;
+
+namespace MoMoney.boot.container
+{
+    internal class tear_down_the_container : ICommand
+    {
+        public void run()
+        {
+            resolve.initialize_with(null);
+        }
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/boot/container/wire_up_the_container.cs
@@ -0,0 +1,34 @@
+using System;
+using Castle.Windsor;
+using MoMoney.Infrastructure.Container.Windsor;
+using MoMoney.Infrastructure.Container.Windsor.configuration;
+using MoMoney.Infrastructure.Extensions;
+using MoMoney.Utility.Core;
+using MoMoney.Utility.Extensions;
+using MoMoney.windows.ui;
+
+namespace MoMoney.boot.container
+{
+    internal class wire_up_the_container : ICommand
+    {
+        public void run()
+        {
+            this.log().debug("initializing container");
+
+            Func<IWindsorContainer> container = () => new WindsorContainerFactory().create();
+            container = container.memorize();
+
+            var registry = new WindsorDependencyRegistry(container);
+            var specification = new ComponentExclusionSpecification();
+            var configuration = new ComponentRegistrationConfiguration();
+
+            new wire_up_the_essential_services_into_the(registry)
+                .then(new wire_up_the_mappers_in_to_the(registry))
+                .then(new wire_up_the_views_in_to_the(registry))
+                .then(new wire_up_the_reports_in_to_the(registry))
+                .then(new run_mass_component_registration_in_to_the(container, specification, configuration))
+                .run();
+            this.log().debug("finished initializing container");
+        }
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/windows.ui/wire_up_the_essential_services_into_the.cs → trunk/product/MyMoney/boot/container/wire_up_the_essential_services_into_the.cs
@@ -4,7 +4,7 @@ using MoMoney.Infrastructure.Logging;
 using MoMoney.Infrastructure.Logging.Log4NetLogging;
 using MoMoney.Utility.Core;
 
-namespace MoMoney.windows.ui
+namespace MoMoney.boot.container
 {
     internal class wire_up_the_essential_services_into_the : ICommand
     {
@@ -18,6 +18,7 @@ namespace MoMoney.windows.ui
         public void run()
         {
             resolve.initialize_with(registry);
+            registry.singleton<IDependencyRegistry>(registry);
             registry.singleton<ILogFactory, Log4NetLogFactory>();
         }
     }
trunk/product/MyMoney/windows.ui/wire_up_the_mappers_in_to_the.cs → trunk/product/MyMoney/boot/container/wire_up_the_mappers_in_to_the.cs
@@ -5,9 +5,9 @@ namespace MoMoney.windows.ui
 {
     internal class wire_up_the_mappers_in_to_the : ICommand
     {
-        private readonly WindsorDependencyRegistry registry;
+        private readonly IContainerBuilder registry;
 
-        public wire_up_the_mappers_in_to_the(WindsorDependencyRegistry registry)
+        public wire_up_the_mappers_in_to_the(IContainerBuilder registry)
         {
             this.registry = registry;
         }
trunk/product/MyMoney/windows.ui/wire_up_the_reports_in_to_the.cs → trunk/product/MyMoney/boot/container/wire_up_the_reports_in_to_the.cs
@@ -7,9 +7,9 @@ namespace MoMoney.windows.ui
 {
     internal class wire_up_the_reports_in_to_the : ICommand
     {
-        private readonly WindsorDependencyRegistry registry;
+        private readonly IContainerBuilder registry;
 
-        public wire_up_the_reports_in_to_the(WindsorDependencyRegistry registry)
+        public wire_up_the_reports_in_to_the(IContainerBuilder registry)
         {
             this.registry = registry;
         }
trunk/product/MyMoney/windows.ui/wire_up_the_views_in_to_the.cs → trunk/product/MyMoney/boot/container/wire_up_the_views_in_to_the.cs
@@ -18,9 +18,9 @@ namespace MoMoney.windows.ui
 {
     internal class wire_up_the_views_in_to_the : ICommand
     {
-        readonly WindsorDependencyRegistry register;
+        readonly IContainerBuilder register;
 
-        public wire_up_the_views_in_to_the(WindsorDependencyRegistry registry)
+        public wire_up_the_views_in_to_the(IContainerBuilder registry)
         {
             register = registry;
         }
trunk/product/MyMoney/windows.ui/bootstrap.cs → trunk/product/MyMoney/boot/bootstrap.cs
@@ -1,9 +1,11 @@
 using System;
+using MoMoney.boot.container;
 using MoMoney.Infrastructure.Extensions;
 using MoMoney.Presentation.Presenters.Commands;
 using MoMoney.Utility.Extensions;
+using MoMoney.windows.ui;
 
-namespace MoMoney.windows.ui
+namespace MoMoney.boot
 {
     internal static class bootstrap
     {
trunk/product/MyMoney/windows.ui/check_for_updates.cs → trunk/product/MyMoney/boot/check_for_updates.cs
File renamed without changes
trunk/product/MyMoney/windows.ui/global_error_handling.cs → trunk/product/MyMoney/boot/global_error_handling.cs
File renamed without changes
trunk/product/MyMoney/windows.ui/hookup.cs → trunk/product/MyMoney/boot/hookup.cs
File renamed without changes
trunk/product/MyMoney/windows.ui/start_the_application.cs → trunk/product/MyMoney/boot/start_the_application.cs
File renamed without changes
trunk/product/MyMoney/Infrastructure/Container/Windsor/WindsorContainerFactory.cs
@@ -1,7 +1,6 @@
 using System;
 using Castle.MicroKernel.Registration;
 using Castle.Windsor;
-using MoMoney.Infrastructure.Container.Windsor.configuration;
 using MoMoney.Utility.Core;
 
 namespace MoMoney.Infrastructure.Container.Windsor
@@ -12,52 +11,9 @@ namespace MoMoney.Infrastructure.Container.Windsor
 
     public class WindsorContainerFactory : IWindsorContainerFactory
     {
-        static IWindsorContainer container;
-        static readonly object mutex = new object();
-        readonly IComponentExclusionSpecification criteria_to_satisfy;
-        readonly IRegistrationConfiguration configuration;
-
-        public WindsorContainerFactory()
-            : this(new ComponentExclusionSpecification(), new ComponentRegistrationConfiguration())
-        {
-        }
-
-        public WindsorContainerFactory(IComponentExclusionSpecification criteria_to_satisfy,
-                                       IRegistrationConfiguration configuration)
-        {
-            this.criteria_to_satisfy = criteria_to_satisfy;
-            this.configuration = configuration;
-        }
-
         public IWindsorContainer create()
         {
-            if (null == container)
-            {
-                lock (mutex)
-                {
-                    if (null == container)
-                    {
-                        container = register_components_into_container();
-                    }
-                }
-            }
-            return container;
-        }
-
-        IWindsorContainer register_components_into_container()
-        {
-            var the_container = new WindsorContainer();
-            the_container.Register(
-                AllTypes
-                    .Pick()
-                    .FromAssembly(GetType().Assembly)
-                    .WithService
-                    .LastInterface()
-                    //.FirstInterface()
-                    .Unless(criteria_to_satisfy.is_satisfied_by)
-                    .Configure(x => configuration.configure(x))
-                );
-            return the_container;
+            return new WindsorContainer();
         }
     }
 
@@ -75,7 +31,7 @@ namespace MoMoney.Infrastructure.Container.Windsor
                                                  first = interfaces[0];
                                              }
 
-                                             return (first != null) ? new Type[] {first} : null;
+                                             return (first != null) ? new[] {first} : null;
                                          });
         }
     }
trunk/product/MyMoney/Infrastructure/Container/Windsor/WindsorDependencyRegistry.cs
@@ -8,45 +8,41 @@ using MoMoney.Utility.Extensions;
 
 namespace MoMoney.Infrastructure.Container.Windsor
 {
-    public class WindsorDependencyRegistry : IDependencyRegistry
+    internal class WindsorDependencyRegistry : IDependencyRegistry, IContainerBuilder
     {
-        readonly IWindsorContainer underlying_container;
+        readonly Func<IWindsorContainer> underlying_container;
 
-        public WindsorDependencyRegistry():this(new WindsorContainerFactory())
+        public WindsorDependencyRegistry(Func<IWindsorContainer> container)
         {
-        }
-
-        public WindsorDependencyRegistry(IWindsorContainerFactory factory)
-        {
-            underlying_container = factory.create();
+            underlying_container = container;
         }
 
         public Interface get_a<Interface>()
         {
-            return underlying_container.Kernel.Resolve<Interface>();
+            return underlying_container().Kernel.Resolve<Interface>();
         }
 
         public IEnumerable<Interface> all_the<Interface>()
         {
-            return underlying_container.ResolveAll<Interface>();
+            return underlying_container().ResolveAll<Interface>();
         }
 
         public void singleton<Interface, Implementation>() where Implementation : Interface
         {
             var interface_type = typeof (Interface);
             var implementation_type = typeof (Implementation);
-            underlying_container.AddComponent(create_a_key_using(interface_type, implementation_type), interface_type,
+            underlying_container().AddComponent(create_a_key_using(interface_type, implementation_type), interface_type,
                                               implementation_type);
         }
 
         public void singleton<Interface>(Interface instanceOfTheInterface)
         {
-            underlying_container.Kernel.AddComponentInstance<Interface>(instanceOfTheInterface);
+            underlying_container().Kernel.AddComponentInstance<Interface>(instanceOfTheInterface);
         }
 
         public void transient<Interface, Implementation>() where Implementation : Interface
         {
-            underlying_container.AddComponentLifeStyle(
+            underlying_container().AddComponentLifeStyle(
                 create_a_key_using(typeof (Interface), typeof (Implementation)),
                 typeof (Interface), typeof (Implementation), LifestyleType.Transient);
         }
trunk/product/MyMoney/Infrastructure/Container/Windsor/WindsorDependencyRegistrySpecs.cs
@@ -8,6 +8,7 @@ using MoMoney.Testing.spechelpers.core;
 namespace MoMoney.Infrastructure.Container.Windsor
 {
     [Concern(typeof (WindsorDependencyRegistry))]
+    [run_in_real_container]
     public class when_registering_a_singleton_component_with_the_windsor_container : concerns_for<IDependencyRegistry>
     {
         it should_return_the_same_instance_each_time_its_resolved =
@@ -17,7 +18,7 @@ namespace MoMoney.Infrastructure.Container.Windsor
 
         public override IDependencyRegistry create_sut()
         {
-            return new WindsorDependencyRegistry();
+            return resolve.dependency_for<IDependencyRegistry>();
         }
 
         context c = () =>
trunk/product/MyMoney/Testing/MetaData/run_in_real_container.cs
@@ -1,8 +1,7 @@
 using System.Collections;
 using MbUnit.Core.Framework;
 using MbUnit.Core.Invokers;
-using MoMoney.Infrastructure.Container;
-using MoMoney.Infrastructure.Container.Windsor;
+using MoMoney.boot.container;
 
 namespace MoMoney.Testing.MetaData
 {
@@ -24,12 +23,12 @@ namespace MoMoney.Testing.MetaData
         {
             try
             {
-                resolve.initialize_with(new WindsorDependencyRegistry());
+                new wire_up_the_container().run();
                 return Invoker.Execute(o, args);
             }
             finally
             {
-                resolve.initialize_with(null);
+                new tear_down_the_container().run();
             }
         }
     }
trunk/product/MyMoney/windows.ui/wire_up_the_container.cs
@@ -1,21 +0,0 @@
-using MoMoney.Infrastructure.Container.Windsor;
-using MoMoney.Infrastructure.Extensions;
-using MoMoney.Utility.Core;
-using MoMoney.Utility.Extensions;
-
-namespace MoMoney.windows.ui
-{
-    internal class wire_up_the_container : ICommand
-    {
-        public void run()
-        {
-            this.log().debug("initializing container");
-            var registry = new WindsorDependencyRegistry();
-            new wire_up_the_essential_services_into_the(registry)
-                .then(new wire_up_the_mappers_in_to_the(registry))
-                .then(new wire_up_the_views_in_to_the(registry))
-                .then(new wire_up_the_reports_in_to_the(registry))
-                .run();
-        }
-    }
-}
\ No newline at end of file
trunk/product/MyMoney/MyMoney.csproj
@@ -156,6 +156,7 @@
     </Reference>
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="boot\container\tear_down_the_container.cs" />
     <Compile Include="DataAccess\db40\ConnectionFactory.cs" />
     <Compile Include="DataAccess\db40\DatabaseConfiguration.cs" />
     <Compile Include="DataAccess\db40\EmptySession.cs" />
@@ -212,6 +213,7 @@
     <Compile Include="Infrastructure\Container\Windsor\configuration\NoInterfaces.cs" />
     <Compile Include="Infrastructure\Container\Windsor\configuration\RegisterComponentContract.cs" />
     <Compile Include="Infrastructure\Container\Windsor\configuration\SubclassesForm.cs" />
+    <Compile Include="Infrastructure\Container\Windsor\IContainerBuilder.cs" />
     <Compile Include="Infrastructure\Container\Windsor\WindsorContainerFactory.cs" />
     <Compile Include="Infrastructure\debugging\Launch.cs" />
     <Compile Include="Infrastructure\eventing\EventAggregator.cs" />
@@ -637,8 +639,8 @@
     <Compile Include="Utility\Extensions\TypeExtensions.cs" />
     <Compile Include="Utility\Extensions\TypeExtensionsSpecs.cs" />
     <Compile Include="Utility\Extensions\visitor_extensions.cs" />
-    <Compile Include="windows.ui\check_for_updates.cs" />
-    <Compile Include="windows.ui\hookup.cs" />
+    <Compile Include="boot\check_for_updates.cs" />
+    <Compile Include="boot\hookup.cs" />
     <Compile Include="Presentation\Model\Menu\ISubMenu.cs" />
     <Compile Include="Utility\Core\IRegistry.cs" />
     <Compile Include="Infrastructure\Container\dependency_resolution_exception.cs" />
@@ -657,7 +659,7 @@
     <Compile Include="Presentation\Presenters\Menu\MainMenuPresenterSpecs.cs" />
     <Compile Include="Presentation\Presenters\Commands\LoadPresentationModulesCommand.cs" />
     <Compile Include="Presentation\Views\Menu\ApplicationMenuHostSpecs.cs" />
-    <Compile Include="windows.ui\bootstrap.cs" />
+    <Compile Include="boot\bootstrap.cs" />
     <EmbeddedResource Include="Presentation\Views\AddCompanyView.resx">
       <DependentUpon>AddCompanyView.cs</DependentUpon>
       <SubType>Designer</SubType>
@@ -746,13 +748,14 @@
     <Compile Include="Testing\spechelpers\core\assertions.cs" />
     <Compile Include="Testing\spechelpers\core\mocking_extensions.cs" />
     <Compile Include="Testing\MetaData\ConcernAttribute.cs" />
-    <Compile Include="windows.ui\start_the_application.cs" />
-    <Compile Include="windows.ui\wire_up_the_container.cs" />
-    <Compile Include="windows.ui\global_error_handling.cs" />
-    <Compile Include="windows.ui\wire_up_the_essential_services_into_the.cs" />
-    <Compile Include="windows.ui\wire_up_the_mappers_in_to_the.cs" />
-    <Compile Include="windows.ui\wire_up_the_reports_in_to_the.cs" />
-    <Compile Include="windows.ui\wire_up_the_views_in_to_the.cs" />
+    <Compile Include="boot\start_the_application.cs" />
+    <Compile Include="boot\container\wire_up_the_container.cs" />
+    <Compile Include="boot\global_error_handling.cs" />
+    <Compile Include="boot\container\run_mass_component_registration_in_to_the.cs" />
+    <Compile Include="boot\container\wire_up_the_essential_services_into_the.cs" />
+    <Compile Include="boot\container\wire_up_the_mappers_in_to_the.cs" />
+    <Compile Include="boot\container\wire_up_the_reports_in_to_the.cs" />
+    <Compile Include="boot\container\wire_up_the_views_in_to_the.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="DataAccess\sqlcompact\" />