diff options
| author | mo <mo@mokhan.ca> | 2009-10-10 19:34:48 -0600 |
|---|---|---|
| committer | mo <mo@mokhan.ca> | 2009-10-10 19:34:48 -0600 |
| commit | 328f67dfee816ef78de9e489032f1fc5cc35498b (patch) | |
| tree | 4acdcbabf6ffe411df3207a7dd85e18d8ad3206a | |
| parent | 46c1342e6ba3686c64ceb83e58db6fcff364c8d8 (diff) | |
added a get_all behavior to the simple container
9 files changed, 73 insertions, 32 deletions
diff --git a/product/application.console/application.console/application.console.csproj b/product/application.console/application.console/application.console.csproj index c386d3e..523e959 100644 --- a/product/application.console/application.console/application.console.csproj +++ b/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" /> diff --git a/product/application.console/application.console/infrastructure/ComponentFactory.cs b/product/application.console/application.console/infrastructure/ComponentFactory.cs new file mode 100644 index 0000000..838db56 --- /dev/null +++ b/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 diff --git a/product/application.console/application.console/infrastructure/ComponentRegistration.cs b/product/application.console/application.console/infrastructure/ComponentRegistration.cs new file mode 100644 index 0000000..c817c50 --- /dev/null +++ b/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 diff --git a/product/application.console/application.console/infrastructure/Container.cs b/product/application.console/application.console/infrastructure/Container.cs index 48eb2e8..903ae44 100644 --- a/product/application.console/application.console/infrastructure/Container.cs +++ b/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 diff --git a/product/application.console/application.console/infrastructure/GenericRegistration.cs b/product/application.console/application.console/infrastructure/GenericRegistration.cs index 5541aa6..1efa510 100644 --- a/product/application.console/application.console/infrastructure/GenericRegistration.cs +++ b/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; diff --git a/product/application.console/application.console/infrastructure/Registration.cs b/product/application.console/application.console/infrastructure/Registration.cs deleted file mode 100644 index f12703d..0000000 --- a/product/application.console/application.console/infrastructure/Registration.cs +++ /dev/null @@ -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 diff --git a/product/application.console/application.console/infrastructure/SimpleContainer.cs b/product/application.console/application.console/infrastructure/SimpleContainer.cs index de22fe6..b5d2ad3 100644 --- a/product/application.console/application.console/infrastructure/SimpleContainer.cs +++ b/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 diff --git a/product/application.console/application.console/infrastructure/SimpleContainerBuilder.cs b/product/application.console/application.console/infrastructure/SimpleContainerBuilder.cs index 7e41a63..ce3a406 100644 --- a/product/application.console/application.console/infrastructure/SimpleContainerBuilder.cs +++ b/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); diff --git a/product/application.tests/console/SimpleContainerSpecs.cs b/product/application.tests/console/SimpleContainerSpecs.cs index f09863e..5442c96 100644 --- a/product/application.tests/console/SimpleContainerSpecs.cs +++ b/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 |
