Commit 328f67d

mo <mo@mokhan.ca>
2009-10-11 01:34:48
added a get_all behavior to the simple container
1 parent 46c1342
product/application.console/application.console/infrastructure/ComponentFactory.cs
@@ -0,0 +1,8 @@
+namespace gorilla.migrations.console.infrastructure
+{
+    public interface ComponentFactory : ComponentRegistration
+    {
+        bool is_for<T>();
+        object build();
+    }
+}
\ No newline at end of file
product/application.console/application.console/infrastructure/ComponentRegistration.cs
@@ -0,0 +1,8 @@
+namespace gorilla.migrations.console.infrastructure
+{
+    public interface ComponentRegistration
+    {
+        void scope<T>() where T : Scope, new();
+        ComponentRegistration As<T>();
+    }
+}
\ No newline at end of file
product/application.console/application.console/infrastructure/Container.cs
@@ -1,7 +1,10 @@
+using System.Collections.Generic;
+
 namespace gorilla.migrations.console.infrastructure
 {
     public interface Container
     {
         T get_a<T>();
+        IEnumerable<T> get_all<T>();
     }
 }
\ No newline at end of file
product/application.console/application.console/infrastructure/GenericRegistration.cs
@@ -5,11 +5,11 @@ using System.Linq.Expressions;
 
 namespace gorilla.migrations.console.infrastructure
 {
-    public class GenericRegistration<Implementation> : Reg
+    public class GenericRegistration<Implementation> : ComponentFactory
     {
         Func<Implementation> factory;
         ICollection<Type> contracts = new HashSet<Type>();
-        Scope contract_scope;
+        Scope contract_scope = new FactoryScope();
 
         public GenericRegistration(Expression<Func<Implementation>> factory)
         {
@@ -26,7 +26,7 @@ namespace gorilla.migrations.console.infrastructure
             scope(new Scope());
         }
 
-        public Registration As<Contract>()
+        public ComponentRegistration As<Contract>()
         {
             contracts.Add(typeof (Contract));
             return this;
product/application.console/application.console/infrastructure/Registration.cs
@@ -1,14 +0,0 @@
-namespace gorilla.migrations.console.infrastructure
-{
-    public interface Registration
-    {
-        void scope<T>() where T : Scope, new();
-        Registration As<T>();
-    }
-
-    public interface Reg : Registration
-    {
-        bool is_for<T>();
-        object build();
-    }
-}
\ No newline at end of file
product/application.console/application.console/infrastructure/SimpleContainer.cs
@@ -5,16 +5,21 @@ namespace gorilla.migrations.console.infrastructure
 {
     public class SimpleContainer : Container
     {
-        readonly IList<Reg> registrations;
+        readonly IList<ComponentFactory> registrations;
 
-        public SimpleContainer(IList<Reg> registrations)
+        public SimpleContainer(IEnumerable<ComponentFactory> registrations)
         {
-            this.registrations = registrations;
+            this.registrations = registrations.ToList();
         }
 
         public T get_a<T>()
         {
             return (T) registrations.First(x => x.is_for<T>()).build();
         }
+
+        public IEnumerable<T> get_all<T>()
+        {
+            return registrations.Where(x => x.is_for<T>()).Select(x => (T) x.build());
+        }
     }
 }
\ No newline at end of file
product/application.console/application.console/infrastructure/SimpleContainerBuilder.cs
@@ -6,9 +6,9 @@ namespace gorilla.migrations.console.infrastructure
 {
     public class SimpleContainerBuilder
     {
-        IList<Reg> registered_items = new List<Reg>();
+        IList<ComponentFactory> registered_items = new List<ComponentFactory>();
 
-        public Registration register<T>(Expression<Func<T>> factory)
+        public ComponentRegistration register<T>(Expression<Func<T>> factory)
         {
             var registration = new GenericRegistration<T>(factory);
             registered_items.Add(registration);
product/application.console/application.console/application.console.csproj
@@ -45,10 +45,11 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="infrastructure\ComponentFactory.cs" />
     <Compile Include="infrastructure\Container.cs" />
     <Compile Include="infrastructure\FactoryScope.cs" />
     <Compile Include="infrastructure\GenericRegistration.cs" />
-    <Compile Include="infrastructure\Registration.cs" />
+    <Compile Include="infrastructure\ComponentRegistration.cs" />
     <Compile Include="infrastructure\Scope.cs" />
     <Compile Include="infrastructure\SimpleContainer.cs" />
     <Compile Include="infrastructure\SimpleContainerBuilder.cs" />
product/application.tests/console/SimpleContainerSpecs.cs
@@ -1,4 +1,4 @@
-using System;
+using System.Collections.Generic;
 using developwithpassion.bdd.contexts;
 using developwithpassion.bdd.harnesses.mbunit;
 using developwithpassion.bdddoc.core;
@@ -8,23 +8,29 @@ namespace tests.console
 {
     public class SimpleContainerSpecs
     {
-        public abstract class concern : observations_for_a_sut_with_a_contract<Container, SimpleContainer> {}
-
-        [Concern(typeof (SimpleContainer))]
-        public class when_registering_an_item_with_the_container : concern
+        public abstract class concern : observations_for_a_sut_with_a_contract<Container, SimpleContainer>
         {
             context c = () =>
             {
                 builder = new SimpleContainerBuilder();
-                builder.register(() => new Thingy()).As<IThingy>().scope<FactoryScope>();
             };
 
-            [Obsolete("use context property to access testing controller")]
             public override Container create_sut()
             {
                 return builder.build();
             }
 
+            static protected SimpleContainerBuilder builder;
+        }
+
+        [Concern(typeof (SimpleContainer))]
+        public class when_registering_an_item_with_the_container : concern
+        {
+            context c = () =>
+            {
+                builder.register(() => new Thingy()).As<IThingy>().scope<FactoryScope>();
+            };
+
             because b = () =>
             {
                 result = controller.sut.get_a<IThingy>();
@@ -35,13 +41,37 @@ namespace tests.console
                 result.should_not_be_equal_to(controller.sut.get_a<IThingy>());
             };
 
-            static SimpleContainerBuilder builder;
             static IThingy result;
         }
+
+        [Concern(typeof (SimpleContainer))]
+        public class when_resolving_multiple_implementations_for_a_component : concern
+        {
+            context c = () =>
+            {
+                builder.register(() => new Thingy()).As<IThingy>();
+                builder.register(() => new AnotherThingy()).As<IThingy>();
+            };
+
+            because b = () =>
+            {
+                results = controller.sut.get_all<IThingy>();
+            };
+
+            it should_return_each_registered_implementation = () =>
+            {
+                results.should_contain_item_matching(x => x is Thingy);
+                results.should_contain_item_matching(x => x is AnotherThingy);
+            };
+
+            static IEnumerable<IThingy> results;
+        }
     }
 
 
+    interface IThingy {}
+
     class Thingy : IThingy {}
 
-    interface IThingy {}
+    class AnotherThingy : IThingy {}
 }
\ No newline at end of file