Commit 61826be

mokhan <mokhan@ce5e1baf-6525-42e4-a1b2-857ea38da20a>
2009-03-21 04:06:06
slowly moving from windsor container to autofac.
git-svn-id: https://svn.xp-dev.com/svn/mokhan-mo.money@94 ce5e1baf-6525-42e4-a1b2-857ea38da20a
1 parent 0a8ef11
Changed files (6)
trunk/product/MyMoney/boot/container/registration/wire_up_the_reports_in_to_the.cs
@@ -3,7 +3,7 @@ using MoMoney.Presentation.Views.billing;
 using MoMoney.Presentation.Views.reporting;
 using MoMoney.Utility.Core;
 
-namespace MoMoney.windows.ui
+namespace MoMoney.boot.container.registration
 {
     internal class wire_up_the_reports_in_to_the : ICommand
     {
trunk/product/MyMoney/boot/container/wire_up_the_container.cs
@@ -1,10 +1,12 @@
-using Castle.Windsor;
+using System;
+using System.Linq;
+using System.Reflection;
 using MoMoney.boot.container.registration;
 using MoMoney.Infrastructure.Container.Windsor;
 using MoMoney.Infrastructure.Container.Windsor.configuration;
+using MoMoney.Infrastructure.Logging;
 using MoMoney.Utility.Core;
 using MoMoney.Utility.Extensions;
-using MoMoney.windows.ui;
 
 namespace MoMoney.boot.container
 {
@@ -23,10 +25,52 @@ namespace MoMoney.boot.container
                 .then(new wire_up_the_infrastructure_in_to_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_presentation_modules(registry))
+                .then(new wire_up_the_presentation_modules(registry))
                 .then(new wire_up_the_reports_in_to_the(registry))
                 .then(new run_mass_component_registration_in_to_the(container, specification, configuration))
+                //.then(new auto_wire_components_in_to_the(registry, specification))
                 .run();
         }
     }
+
+    internal class auto_wire_components_in_to_the : ICommand
+    {
+        readonly IContainerBuilder builder;
+        readonly IComponentExclusionSpecification specification;
+
+        public auto_wire_components_in_to_the(IContainerBuilder builder,
+                                              IComponentExclusionSpecification specification)
+        {
+            this.builder = builder;
+            this.specification = specification;
+        }
+
+        public void run()
+        {
+            Assembly
+                .GetExecutingAssembly()
+                .GetTypes()
+                .Where(x => !specification.is_satisfied_by(x))
+                .each(register);
+        }
+
+        void register(Type type)
+        {
+            if (type.GetInterfaces().Length > 0)
+            {
+                if (typeof(ILoggable).IsAssignableFrom(type))
+                {
+                    //builder.proxy(h
+                }
+                else
+                {
+                    builder.transient(type.last_interface(), type);
+                }
+            }
+            else
+            {
+                builder.transient(type, type);
+            }
+        }
+    }
 }
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/Container/Autofac/AutofacDependencyRegistry.cs
@@ -1,25 +1,64 @@
+using System;
 using System.Collections.Generic;
 using Autofac;
+using Autofac.Builder;
+using MoMoney.Infrastructure.proxies;
+using MoMoney.Utility.Core;
+using MoMoney.Utility.Extensions;
 
 namespace MoMoney.Infrastructure.Container.Autofac
 {
     internal class AutofacDependencyRegistry : IDependencyRegistry
     {
-        readonly IContainer container;
+        ContainerBuilder builder;
+        Func<IContainer> container;
 
-        public AutofacDependencyRegistry(IContainer container)
+        public AutofacDependencyRegistry() : this(new ContainerBuilder())
         {
-            this.container = container;
+        }
+
+        public AutofacDependencyRegistry(ContainerBuilder builder)
+        {
+            this.builder = builder;
+            container = () => builder.Build();
+            container = container.memorize();
+        }
+
+        public void singleton<Contract, Implementation>() where Implementation : Contract
+        {
+            builder.Register<Implementation>().As<Contract>().SingletonScoped();
+        }
+
+        public void singleton<Contract>(Contract instance_of_the_contract)
+        {
+            builder.Register(instance_of_the_contract).As<Contract>().SingletonScoped();
+        }
+
+        public void transient<Contract, Implementation>() where Implementation : Contract
+        {
+            transient(typeof (Contract), typeof (Implementation));
+        }
+
+        public void transient(Type contract, Type implementation)
+        {
+            builder.Register(implementation).As(contract).FactoryScoped();
+        }
+
+        public void proxy<T>(IConfiguration<IProxyBuilder<T>> configuration, Func<T> target)
+        {
+            var proxy_builder = new ProxyBuilder<T>();
+            configuration.configure(proxy_builder);
+            builder.Register(x => proxy_builder.create_proxy_for(target)).As<T>().FactoryScoped();
         }
 
         public Interface get_a<Interface>()
         {
-            return container.Resolve<Interface>();
+            return container().Resolve<Interface>();
         }
 
         public IEnumerable<Interface> all_the<Interface>()
         {
-            return container.Resolve<IEnumerable<Interface>>();
+            return container().Resolve<IEnumerable<Interface>>();
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/Container/Windsor/IContainerBuilder.cs
@@ -4,7 +4,7 @@ using MoMoney.Utility.Core;
 
 namespace MoMoney.Infrastructure.Container.Windsor
 {
-    public interface IContainerBuilder : IDependencyRegistry
+    public interface IContainerBuilder
     {
         void singleton<Contract, Implementation>() where Implementation : Contract;
         void singleton<Contract>(Contract instance_of_the_contract);
trunk/product/MyMoney/Infrastructure/Container/Windsor/WindsorDependencyRegistry.cs
@@ -8,7 +8,7 @@ using MoMoney.Utility.Extensions;
 
 namespace MoMoney.Infrastructure.Container.Windsor
 {
-    internal class WindsorDependencyRegistry : IContainerBuilder
+    internal class WindsorDependencyRegistry : IContainerBuilder, IDependencyRegistry
     {
         readonly IWindsorContainer underlying_container;
 
trunk/product/MyMoney/Infrastructure/proxies/ProxyBuilderSpecs.cs
@@ -1,9 +1,7 @@
-using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;
 using Castle.Core.Interceptor;
-using Ec.AuditTool.Infrastructure.Proxies;
 using developwithpassion.bdd.contexts;
 using MoMoney.Testing.MetaData;
 using MoMoney.Testing.spechelpers.contexts;
@@ -42,7 +40,7 @@ namespace MoMoney.Infrastructure.proxies
                         {
                             sut.add_interceptor<SomeInterceptor>();
                             sut.add_interceptor<AnotherInterceptor>();
-                            var proxy = sut.create_proxy_for(an_implementation_of_the_interface);
+                            var proxy = sut.create_proxy_for(() => an_implementation_of_the_interface);
                             proxy.OneMethod();
                             proxy.SecondMethod();
                         };
@@ -73,7 +71,7 @@ namespace MoMoney.Infrastructure.proxies
                             var constraint = sut.add_interceptor<SomeInterceptor>();
                             constraint.InterceptOn.OneMethod();
 
-                            var proxy = sut.create_proxy_for(an_implementation);
+                            var proxy = sut.create_proxy_for(() => an_implementation);
                             proxy.OneMethod();
                             proxy.SecondMethod();
                         };