Commit 87ab8a3

mokhan <mokhan@ce5e1baf-6525-42e4-a1b2-857ea38da20a>
2009-05-01 19:37:37
trying out a different way to register items in to the container.
git-svn-id: https://svn.xp-dev.com/svn/mokhan-mo.money@202 ce5e1baf-6525-42e4-a1b2-857ea38da20a
1 parent 01ca372
trunk/product/Gorilla.Commons.Infrastructure.ThirdParty/Experiments/Autofac/AutofacContainerBuilder.cs
@@ -0,0 +1,75 @@
+using System;
+using System.Linq.Expressions;
+using Autofac;
+using Autofac.Builder;
+using Autofac.Modules;
+using Autofac.Registrars;
+using AutofacContrib.DynamicProxy2;
+using Gorilla.Commons.Infrastructure.Castle.DynamicProxy;
+using Gorilla.Commons.Infrastructure.Container;
+using Gorilla.Commons.Infrastructure.Experiments;
+using Gorilla.Commons.Utility.Core;
+using Gorilla.Commons.Utility.Extensions;
+
+namespace Gorilla.Commons.Infrastructure.New.Autofac
+{
+    public class AutofacContainerBuilder : IContainerBuilder
+    {
+        readonly ContainerBuilder builder;
+        readonly Func<IContainer> container;
+
+        public AutofacContainerBuilder() : this(new ContainerBuilder())
+        {
+        }
+
+        public AutofacContainerBuilder(ContainerBuilder builder)
+        {
+            this.builder = builder;
+            builder.RegisterModule(new ImplicitCollectionSupportModule());
+            builder.RegisterModule(new StandardInterceptionModule());
+            builder.SetDefaultScope(InstanceScope.Factory);
+            container = () => builder.Build();
+            container = container.memorize();
+        }
+
+        public IDependencyRegistry build()
+        {
+            throw new NotImplementedException();
+        }
+
+        public IExtendedRegistration<T> register<T>(Expression<Func<T>> func) where T : class
+        {
+            return new AutofacExtendedRegistration<T>(builder, func);
+        }
+    }
+
+    public class AutofacExtendedRegistration<T> :  IExtendedRegistration<T> where T : class
+    {
+        IConcreteRegistrar registrar;
+
+        public AutofacExtendedRegistration(ContainerBuilder builder, Expression<Func<T>> expression)
+        {
+            pretty_print = expression.ToString();
+            registrar = builder.Register(x => expression.Compile()).As<T>();
+            registrar.FactoryScoped();
+        }
+
+        public string pretty_print { get; set; }
+
+        public IExtendedRegistration<T> as_singleton()
+        {
+            registrar.SingletonScoped();
+            return this;
+        }
+
+        public IExtendedRegistration<T> with_expiry(string date_time)
+        {
+            throw new NotImplementedException();
+        }
+
+        public IExtendedRegistration<T> with_proxy(IConfiguration<IProxyBuilder<T>> configuration)
+        {
+            throw new NotImplementedException();
+        }
+    }
+}
\ No newline at end of file
trunk/product/Gorilla.Commons.Infrastructure.ThirdParty/Experiments/ExtendedRegistration.cs
@@ -0,0 +1,57 @@
+using System;
+using System.Globalization;
+using System.Linq.Expressions;
+using Gorilla.Commons.Infrastructure.Castle.DynamicProxy;
+using Gorilla.Commons.Utility;
+using Gorilla.Commons.Utility.Core;
+using Gorilla.Commons.Utility.Extensions;
+
+namespace Gorilla.Commons.Infrastructure.Experiments
+{
+    public class ExtendedRegistration<T> : IResolver<T>, IExtendedRegistration<T> where T : class
+    {
+        public Func<T> build { get; private set; }
+        const string time_format = "dd/MM/yyyy HH:mm:ss";
+
+        public ExtendedRegistration(Expression<Func<T>> expression)
+        {
+            pretty_print = expression.ToString();
+            build = expression.Compile();
+        }
+
+        public string pretty_print { get; set; }
+
+        public IExtendedRegistration<T> as_singleton()
+        {
+            build = build.memorize();
+            return this;
+        }
+
+        public IExtendedRegistration<T> with_expiry(string date_time)
+        {
+            var the_date_time = DateTime.ParseExact(date_time, time_format, CultureInfo.InvariantCulture);
+            var original_func = build;
+
+            build = () =>
+                        {
+                            if (Clock.now() > the_date_time)
+                                throw new ObjectUsageHasExpiredException(original_func().GetType(), date_time);
+
+                            return original_func();
+                        };
+            return this;
+        }
+
+        public IExtendedRegistration<T> with_proxy(IConfiguration<IProxyBuilder<T>> configuration)
+        {
+            var original_func = build;
+            build = () =>
+                        {
+                            var builder = new ProxyBuilder<T>();
+                            configuration.configure(builder);
+                            return builder.create_proxy_for(original_func);
+                        };
+            return this;
+        }
+    }
+}
\ No newline at end of file
trunk/product/Gorilla.Commons.Infrastructure.ThirdParty/Experiments/IContainerBuilder.cs
@@ -0,0 +1,12 @@
+using System;
+using System.Linq.Expressions;
+using Gorilla.Commons.Infrastructure.Container;
+using Gorilla.Commons.Utility.Core;
+
+namespace Gorilla.Commons.Infrastructure.Experiments
+{
+    public interface IContainerBuilder : IBuilder<IDependencyRegistry>
+    {
+        IExtendedRegistration<T> register<T>(Expression<Func<T>> func) where T : class;
+    }
+}
\ No newline at end of file
trunk/product/Gorilla.Commons.Infrastructure.ThirdParty/Experiments/IExtendedRegistration.cs
@@ -0,0 +1,17 @@
+using Gorilla.Commons.Infrastructure.Castle.DynamicProxy;
+using Gorilla.Commons.Utility.Core;
+
+namespace Gorilla.Commons.Infrastructure.Experiments
+{
+    public interface IExtendedRegistration
+    {
+        string pretty_print { get; }
+    }
+
+    public interface IExtendedRegistration<T> : IExtendedRegistration where T : class
+    {
+        IExtendedRegistration<T> as_singleton();
+        IExtendedRegistration<T> with_expiry(string date_time);
+        IExtendedRegistration<T> with_proxy(IConfiguration<IProxyBuilder<T>> configuration);
+    }
+}
\ No newline at end of file
trunk/product/Gorilla.Commons.Infrastructure.ThirdParty/Experiments/IResolver.cs
@@ -0,0 +1,9 @@
+using System;
+
+namespace Gorilla.Commons.Infrastructure.Experiments
+{
+    public interface IResolver<T>
+    {
+        Func<T> build { get; }
+    }
+}
\ No newline at end of file
trunk/product/Gorilla.Commons.Infrastructure.ThirdParty/Experiments/ObjectUsageHasExpiredException.cs
@@ -0,0 +1,16 @@
+using System;
+
+namespace Gorilla.Commons.Infrastructure.Experiments
+{
+    internal class ObjectUsageHasExpiredException : Exception
+    {
+        public ObjectUsageHasExpiredException(Type type, string date_time) : base(build_message(type, date_time))
+        {
+        }
+
+        static string build_message(Type type, string date_time)
+        {
+            return string.Format("Cannot use {0} after {1}.", type.Name, date_time);
+        }
+    }
+}
\ No newline at end of file
trunk/product/Gorilla.Commons.Infrastructure.ThirdParty/Experiments/SimpleContainerBuilder.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.Linq.Expressions;
+using Gorilla.Commons.Infrastructure.Container;
+
+namespace Gorilla.Commons.Infrastructure.Experiments
+{
+    public class SimpleContainerBuilder : IContainerBuilder
+    {
+        readonly IDictionary<Type, IExtendedRegistration> registries = new Dictionary<Type, IExtendedRegistration>();
+
+        public IDependencyRegistry build()
+        {
+            return new SimpleRegistry(registries);
+        }
+
+        public IExtendedRegistration<T> register<T>(Expression<Func<T>> func) where T : class
+        {
+            try
+            {
+                var registration = new ExtendedRegistration<T>(func);
+                registries.Add(typeof (T), registration);
+                return registration;
+            }
+            catch (ArgumentException e)
+            {
+                throw new TypeAlreadyRegisteredInContainerException(typeof (T), registries[typeof (T)].pretty_print);
+            }
+        }
+    }
+}
\ No newline at end of file
trunk/product/Gorilla.Commons.Infrastructure.ThirdParty/Experiments/SimpleRegistry.cs
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Gorilla.Commons.Infrastructure.Container;
+using Gorilla.Commons.Utility.Extensions;
+
+namespace Gorilla.Commons.Infrastructure.Experiments
+{
+    public class SimpleRegistry : IDependencyRegistry
+    {
+        readonly IDictionary<Type, IList<IExtendedRegistration>> registrations;
+
+        public SimpleRegistry(IDictionary<Type, IList<IExtendedRegistration>> registrations)
+        {
+            this.registrations = registrations;
+        }
+
+        public Interface get_a<Interface>()
+        {
+            return registrations[typeof (Interface)].First().downcast_to<IResolver<Interface>>().build();
+        }
+
+        public IEnumerable<Interface> all_the<Interface>()
+        {
+            foreach (var registration in registrations[typeof(Interface)])
+            {
+                yield return registration.downcast_to<IResolver<Interface>>().build()
+
+                
+            }
+            yield return registrations[typeof(Interface)].each(x => x.downcast_to<IResolver<Interface>>().build());
+        }
+    }
+}
\ No newline at end of file
trunk/product/Gorilla.Commons.Infrastructure.ThirdParty/Experiments/TypeAlreadyRegisteredInContainerException.cs
@@ -0,0 +1,18 @@
+using System;
+
+namespace Gorilla.Commons.Infrastructure.Experiments
+{
+    public class TypeAlreadyRegisteredInContainerException : Exception
+    {
+        public TypeAlreadyRegisteredInContainerException(Type typeNotFound, string registration)
+            : base(build_message(typeNotFound, registration))
+        {
+        }
+
+        static string build_message(Type typeNotFound, string registration)
+        {
+            return string.Format("The type {0} has already been registered with {1} in the container",
+                                 typeNotFound.FullName, registration);
+        }
+    }
+}
\ No newline at end of file
trunk/product/Gorilla.Commons.Infrastructure.ThirdParty/New/New.cs
@@ -1,121 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Globalization;
-using System.Linq.Expressions;
-using Gorilla.Commons.Infrastructure.Castle.DynamicProxy;
-using Gorilla.Commons.Infrastructure.Container;
-using Gorilla.Commons.Utility;
-using Gorilla.Commons.Utility.Core;
-using Gorilla.Commons.Utility.Extensions;
-
-namespace Gorilla.Commons.Infrastructure.New
-{
-    public interface IContainerBuilder : IBuilder<IDependencyRegistry>
-    {
-        IExtendedRegistration<T> register<T>(Expression<Func<T>> func) where T : class;
-    }
-
-    public interface IExtendedRegistration
-    {
-        string pretty_print { get; }
-    }
-
-    public interface IExtendedRegistration<T> : IExtendedRegistration where T: class
-    {
-        IExtendedRegistration<T> as_singleton();
-        IExtendedRegistration<T> with_expiry(string dateTime);
-        IExtendedRegistration<T> with_proxy(IConfiguration<IProxyBuilder<T>> configuration);
-    }
-
-    public class SimpleContainerBuilder : IContainerBuilder
-    {
-        IDictionary<Type, IExtendedRegistration> registries = new Dictionary<Type, IExtendedRegistration>();
-
-        public IDependencyRegistry build()
-        {
-            throw new NotImplementedException();
-        }
-
-        public IExtendedRegistration<T> register<T>(Expression<Func<T>> func) where T : class
-        {
-            try
-            {
-                var reg = new ExtendedRegistration<T>(func);
-                registries.Add(typeof (T), reg);
-                return reg;
-            }
-            catch (ArgumentException e)
-            {
-                throw new TypeAlreadyRegisteredInContainerException(typeof (T), registries[typeof (T)].pretty_print);
-            }
-        }
-    }
-
-    public class ExtendedRegistration<T> : IExtendedRegistration<T> where T : class
-    {
-        Func<T> func;
-        public const string time_format = "dd/MM/yyyy HH:mm:ss";
-
-        public ExtendedRegistration(Expression<Func<T>> expression)
-        {
-            pretty_print = expression.ToString();
-            func = expression.Compile();
-        }
-
-        public string pretty_print { get; set; }
-
-        public IExtendedRegistration<T> as_singleton()
-        {
-            func = func.memorize<T>();
-            return this;
-        }
-
-        public IExtendedRegistration<T> with_expiry(string dateTime)
-        {
-            var theDateTime = DateTime.ParseExact(dateTime, time_format, CultureInfo.InvariantCulture);
-            var original_func = func;
-
-            func = () =>
-                       {
-                           if (Clock.now() > theDateTime)
-                           {
-                               throw new ObjectUsageHasExpiredException(original_func().GetType(), dateTime);
-                           }
-
-                           return original_func();
-                       };
-            return this;
-        }
-
-        public IExtendedRegistration<T> with_proxy(IConfiguration<IProxyBuilder<T>> configuration)
-        {
-            throw new NotImplementedException();
-        }
-    }
-
-    public class TypeAlreadyRegisteredInContainerException : Exception
-    {
-        public TypeAlreadyRegisteredInContainerException(Type typeNotFound, string registration)
-            : base(build_message(typeNotFound, registration))
-        {
-        }
-
-        static string build_message(Type typeNotFound, string registration)
-        {
-            return string.Format("The type {0} has already been registered with {1} in the container",
-                                 typeNotFound.FullName, registration);
-        }
-    }
-
-    internal class ObjectUsageHasExpiredException : Exception
-    {
-        public ObjectUsageHasExpiredException(Type type, string dateTime) : base(build_message(type, dateTime))
-        {
-        }
-
-        static string build_message(Type type, string dateTime)
-        {
-            return string.Format("Cannot use {0} after {1}.", type.Name, dateTime);
-        }
-    }
-}
\ No newline at end of file
trunk/product/Gorilla.Commons.Infrastructure.ThirdParty/Gorilla.Commons.Infrastructure.ThirdParty.csproj
@@ -131,7 +131,15 @@
     <Compile Include="Castle\Windsor\WindsorContainerFactory.cs" />
     <Compile Include="Castle\Windsor\WindsorDependencyRegistry.cs" />
     <Compile Include="Castle\Windsor\WindsorDependencyRegistrySpecs.cs" />
-    <Compile Include="New\New.cs" />
+    <Compile Include="Experiments\Autofac\AutofacContainerBuilder.cs" />
+    <Compile Include="Experiments\ExtendedRegistration.cs" />
+    <Compile Include="Experiments\IContainerBuilder.cs" />
+    <Compile Include="Experiments\IExtendedRegistration.cs" />
+    <Compile Include="Experiments\IResolver.cs" />
+    <Compile Include="Experiments\ObjectUsageHasExpiredException.cs" />
+    <Compile Include="Experiments\SimpleContainerBuilder.cs" />
+    <Compile Include="Experiments\SimpleRegistry.cs" />
+    <Compile Include="Experiments\TypeAlreadyRegisteredInContainerException.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\Gorilla.Commons.Infrastructure\Gorilla.Commons.Infrastructure.csproj">