Commit 46c1342

mo <mo@mokhan.ca>
2009-10-11 01:21:52
added a simple container.
1 parent 7792f27
product/application.console/application.console/infrastructure/Container.cs
@@ -0,0 +1,7 @@
+namespace gorilla.migrations.console.infrastructure
+{
+    public interface Container
+    {
+        T get_a<T>();
+    }
+}
\ No newline at end of file
product/application.console/application.console/infrastructure/FactoryScope.cs
@@ -0,0 +1,12 @@
+using System;
+
+namespace gorilla.migrations.console.infrastructure
+{
+    public class FactoryScope : Scope
+    {
+        public object apply_to(Func<object> factory)
+        {
+            return factory();
+        }
+    }
+}
\ No newline at end of file
product/application.console/application.console/infrastructure/GenericRegistration.cs
@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+
+namespace gorilla.migrations.console.infrastructure
+{
+    public class GenericRegistration<Implementation> : Reg
+    {
+        Func<Implementation> factory;
+        ICollection<Type> contracts = new HashSet<Type>();
+        Scope contract_scope;
+
+        public GenericRegistration(Expression<Func<Implementation>> factory)
+        {
+            this.factory = factory.Compile();
+        }
+
+        void scope(Scope scope)
+        {
+            contract_scope = scope;
+        }
+
+        public void scope<Scope>() where Scope : infrastructure.Scope, new()
+        {
+            scope(new Scope());
+        }
+
+        public Registration As<Contract>()
+        {
+            contracts.Add(typeof (Contract));
+            return this;
+        }
+
+        public bool is_for<Contract>()
+        {
+            var type = typeof (Contract);
+            return type.Equals(typeof (Implementation)) || contracts.Any(x => x.Equals(type));
+        }
+
+        public object build()
+        {
+            return contract_scope.apply_to(() => factory());
+        }
+    }
+}
\ No newline at end of file
product/application.console/application.console/infrastructure/Registration.cs
@@ -0,0 +1,14 @@
+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/Scope.cs
@@ -0,0 +1,9 @@
+using System;
+
+namespace gorilla.migrations.console.infrastructure
+{
+    public interface Scope
+    {
+        object apply_to(Func<object> factory);
+    }
+}
\ No newline at end of file
product/application.console/application.console/infrastructure/SimpleContainer.cs
@@ -0,0 +1,20 @@
+using System.Collections.Generic;
+using System.Linq;
+
+namespace gorilla.migrations.console.infrastructure
+{
+    public class SimpleContainer : Container
+    {
+        readonly IList<Reg> registrations;
+
+        public SimpleContainer(IList<Reg> registrations)
+        {
+            this.registrations = registrations;
+        }
+
+        public T get_a<T>()
+        {
+            return (T) registrations.First(x => x.is_for<T>()).build();
+        }
+    }
+}
\ No newline at end of file
product/application.console/application.console/infrastructure/SimpleContainerBuilder.cs
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq.Expressions;
+
+namespace gorilla.migrations.console.infrastructure
+{
+    public class SimpleContainerBuilder
+    {
+        IList<Reg> registered_items = new List<Reg>();
+
+        public Registration register<T>(Expression<Func<T>> factory)
+        {
+            var registration = new GenericRegistration<T>(factory);
+            registered_items.Add(registration);
+            return registration;
+        }
+
+        public Container build()
+        {
+            return new SimpleContainer(registered_items);
+        }
+    }
+}
\ No newline at end of file
product/application.console/application.console/application.console.csproj
@@ -45,6 +45,13 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="infrastructure\Container.cs" />
+    <Compile Include="infrastructure\FactoryScope.cs" />
+    <Compile Include="infrastructure\GenericRegistration.cs" />
+    <Compile Include="infrastructure\Registration.cs" />
+    <Compile Include="infrastructure\Scope.cs" />
+    <Compile Include="infrastructure\SimpleContainer.cs" />
+    <Compile Include="infrastructure\SimpleContainerBuilder.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
product/application.tests/console/SimpleContainerSpecs.cs
@@ -0,0 +1,47 @@
+using System;
+using developwithpassion.bdd.contexts;
+using developwithpassion.bdd.harnesses.mbunit;
+using developwithpassion.bdddoc.core;
+using gorilla.migrations.console.infrastructure;
+
+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
+        {
+            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();
+            }
+
+            because b = () =>
+            {
+                result = controller.sut.get_a<IThingy>();
+            };
+
+            it should_return_a_new_instance_each_time_it_is_told_to_create_one = () =>
+            {
+                result.should_not_be_equal_to(controller.sut.get_a<IThingy>());
+            };
+
+            static SimpleContainerBuilder builder;
+            static IThingy result;
+        }
+    }
+
+
+    class Thingy : IThingy {}
+
+    interface IThingy {}
+}
\ No newline at end of file
product/application.tests/data/SqlDatabaseGatewayFactorySpecs.cs → product/application.tests/core/data/SqlDatabaseGatewayFactorySpecs.cs
@@ -3,7 +3,7 @@ using developwithpassion.bdd.harnesses.mbunit;
 using developwithpassion.bdddoc.core;
 using gorilla.migrations.data;
 
-namespace tests.data
+namespace tests.core.data
 {
     public class SqlDatabaseGatewayFactorySpecs
     {
product/application.tests/data/SqlDatabaseGatewaySpecs.cs → product/application.tests/core/data/SqlDatabaseGatewaySpecs.cs
@@ -6,7 +6,7 @@ using developwithpassion.bdddoc.core;
 using gorilla.migrations.data;
 using tests.helpers;
 
-namespace tests.data
+namespace tests.core.data
 {
     public class SqlDatabaseGatewaySpecs
     {
product/application.tests/data/SqlFileSpecs.cs → product/application.tests/core/data/SqlFileSpecs.cs
@@ -3,7 +3,7 @@ using developwithpassion.bdd.harnesses.mbunit;
 using developwithpassion.bdddoc.core;
 using gorilla.migrations.data;
 
-namespace tests.data
+namespace tests.core.data
 {
     public class SqlFileSpecs
     {
product/application.tests/io/WindowsFileSystemSpecs.cs → product/application.tests/core/io/WindowsFileSystemSpecs.cs
@@ -9,7 +9,7 @@ using gorilla.migrations.data;
 using gorilla.migrations.io;
 using MbUnit.Framework;
 
-namespace tests.io
+namespace tests.core.io
 {
     public class WindowsFileSystemSpecs
     {
@@ -21,7 +21,6 @@ namespace tests.io
         {
             context c = () =>
             {
-                //directory = Path.Combine(Environment.CurrentDirectory, "sample_files");
                 directory = AppDomain.CurrentDomain.BaseDirectory;
                 first_sql_file = Path.Combine(directory, "0001_first_test_file.sql");
                 second_sql_file = Path.Combine(directory, "0002_second_sql_file.sql");
product/application.tests/CommandRegistrySpecs.cs → product/application.tests/core/CommandRegistrySpecs.cs
@@ -3,7 +3,7 @@ using developwithpassion.bdd.harnesses.mbunit;
 using gorilla.migrations;
 using Rhino.Mocks;
 
-namespace tests
+namespace tests.core
 {
     public class CommandRegistrySpecs
     {
product/application.tests/ConsoleArgumentsSpecs.cs → product/application.tests/core/ConsoleArgumentsSpecs.cs
@@ -2,7 +2,7 @@ using developwithpassion.bdd.contexts;
 using developwithpassion.bdd.harnesses.mbunit;
 using gorilla.migrations;
 
-namespace tests
+namespace tests.core
 {
     public class ConsoleArgumentsSpecs
     {
product/application.tests/ConsoleSpecs.cs → product/application.tests/core/ConsoleSpecs.cs
@@ -4,7 +4,7 @@ using developwithpassion.bdd.mocking.rhino;
 using gorilla.migrations;
 using Rhino.Mocks;
 
-namespace tests
+namespace tests.core
 {
     public class ConsoleSpecs
     {
product/application.tests/RunMigrationsCommandSpecs.cs → product/application.tests/core/RunMigrationsCommandSpecs.cs
@@ -7,7 +7,7 @@ using gorilla.migrations.data;
 using gorilla.migrations.io;
 using tests.helpers;
 
-namespace tests
+namespace tests.core
 {
     public class RunMigrationsCommandSpecs
     {
product/application.tests/application.tests.csproj
@@ -69,19 +69,24 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="CommandRegistrySpecs.cs" />
-    <Compile Include="ConsoleArgumentsSpecs.cs" />
-    <Compile Include="ConsoleSpecs.cs" />
-    <Compile Include="data\SqlDatabaseGatewayFactorySpecs.cs" />
-    <Compile Include="data\SqlFileSpecs.cs" />
-    <Compile Include="data\SqlDatabaseGatewaySpecs.cs" />
+    <Compile Include="console\SimpleContainerSpecs.cs" />
+    <Compile Include="core\CommandRegistrySpecs.cs" />
+    <Compile Include="core\ConsoleArgumentsSpecs.cs" />
+    <Compile Include="core\ConsoleSpecs.cs" />
+    <Compile Include="core\data\SqlDatabaseGatewayFactorySpecs.cs" />
+    <Compile Include="core\data\SqlFileSpecs.cs" />
+    <Compile Include="core\data\SqlDatabaseGatewaySpecs.cs" />
     <Compile Include="EmptyTestFixture.cs" />
     <Compile Include="helpers\RhinoStubbingExtensions.cs" />
-    <Compile Include="io\WindowsFileSystemSpecs.cs" />
+    <Compile Include="core\io\WindowsFileSystemSpecs.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="RunMigrationsCommandSpecs.cs" />
+    <Compile Include="core\RunMigrationsCommandSpecs.cs" />
   </ItemGroup>
   <ItemGroup>
+    <ProjectReference Include="..\application.console\application.console\application.console.csproj">
+      <Project>{138AFA6A-D88C-4156-82EC-65AB62B19830}</Project>
+      <Name>application.console</Name>
+    </ProjectReference>
     <ProjectReference Include="..\application\application.csproj">
       <Project>{5106142C-D444-4D6A-A73F-CA823CE38101}</Project>
       <Name>application</Name>