Commit 3abbf40
Changed files (10)
product
application.console
application.tests
console
product/application.console/application.console/infrastructure/ComponentFactory.cs
@@ -1,8 +1,11 @@
+using System;
+
namespace gorilla.migrations.console.infrastructure
{
public interface ComponentFactory : ComponentRegistration
{
bool is_for<T>();
+ bool is_for(Type type);
object build();
}
}
\ No newline at end of file
product/application.console/application.console/infrastructure/ComponentResolutionException.cs
@@ -0,0 +1,6 @@
+using System;
+
+namespace gorilla.migrations.console.infrastructure
+{
+ public class ComponentResolutionException<Component> : Exception {}
+}
\ No newline at end of file
product/application.console/application.console/infrastructure/GenericRegistration.cs
@@ -14,6 +14,7 @@ namespace gorilla.migrations.console.infrastructure
public GenericRegistration(Expression<Func<Implementation>> factory)
{
this.factory = factory.Compile();
+ as_an<Implementation>();
}
public void scoped_as<Scope>() where Scope : infrastructure.Scope, new()
@@ -29,8 +30,12 @@ namespace gorilla.migrations.console.infrastructure
public bool is_for<Contract>()
{
- var type = typeof (Contract);
- return type.Equals(typeof (Implementation)) || contracts.Any(x => x.Equals(type));
+ return is_for(typeof (Contract));
+ }
+
+ public bool is_for(Type type)
+ {
+ return contracts.Any(x => x.Equals(type));
}
public object build()
product/application.console/application.console/infrastructure/ComponentRegistry.cs → product/application.console/application.console/infrastructure/Ioc.cs
@@ -1,6 +1,6 @@
namespace gorilla.migrations.console.infrastructure
{
- static public class ComponentRegistry
+ static public class Ioc
{
static Container underlying_container;
product/application.console/application.console/infrastructure/SimpleContainer.cs
@@ -14,7 +14,10 @@ namespace gorilla.migrations.console.infrastructure
public T get_a<T>()
{
- return (T) registrations.First(x => x.is_for<T>()).build();
+ var type = typeof (T);
+ if (!registrations.Any(x => x.is_for(type))) throw new ComponentResolutionException<T>();
+
+ return (T) registrations.First(x => x.is_for(type)).build();
}
public IEnumerable<T> get_all<T>()
product/application.console/application.console/application.console.csproj
@@ -50,7 +50,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="infrastructure\ComponentFactory.cs" />
- <Compile Include="infrastructure\ComponentRegistry.cs" />
+ <Compile Include="infrastructure\Ioc.cs" />
+ <Compile Include="infrastructure\ComponentResolutionException.cs" />
<Compile Include="infrastructure\Container.cs" />
<Compile Include="infrastructure\Factory.cs" />
<Compile Include="infrastructure\GenericRegistration.cs" />
product/application.console/application.console/Program.cs
@@ -11,7 +11,7 @@ namespace gorilla.migrations.console
.then(new RegisterConsoleCommands())
.run();
- ComponentRegistry.get_a<Console>().run_against(args);
+ Ioc.get_a<Console>().run_against(args);
}
}
}
\ No newline at end of file
product/application.console/application.console/RegisterConsoleCommands.cs
@@ -6,7 +6,7 @@ namespace gorilla.migrations.console
{
public void run()
{
- ComponentRegistry.get_a<CommandRegistry>().register(ComponentRegistry.get_a<RunMigrationsCommand>());
+ Ioc.get_a<CommandRegistry>().register(Ioc.get_a<RunMigrationsCommand>());
}
}
}
\ No newline at end of file
product/application.console/application.console/WireUpContainer.cs
@@ -1,4 +1,6 @@
using gorilla.migrations.console.infrastructure;
+using gorilla.migrations.data;
+using gorilla.migrations.io;
namespace gorilla.migrations.console
{
@@ -7,9 +9,12 @@ namespace gorilla.migrations.console
public void run()
{
var builder = new SimpleContainerBuilder();
- builder.register(() => new ConsoleApplication(ComponentRegistry.get_a<CommandRegistry>())).as_an<Console>();
+ builder.register(() => new ConsoleApplication(Ioc.get_a<CommandRegistry>())).as_an<Console>();
builder.register(() => new ConsoleArgumentsCommandRegistry()).as_an<CommandRegistry>().scoped_as<Singleton>();
- ComponentRegistry.initialize_with(builder.build());
+ builder.register(() => new RunMigrationsCommand(Ioc.get_a<FileSystem>(), Ioc.get_a<DatabaseGatewayFactory>() )).as_an<ConsoleCommand>();
+ builder.register<FileSystem>(() => new WindowsFileSystem());
+ builder.register<DatabaseGatewayFactory>(() => new SqlDatabaseGatewayFactory());
+ Ioc.initialize_with(builder.build());
}
}
}
\ No newline at end of file
product/application.tests/console/SimpleContainerSpecs.cs
@@ -3,6 +3,7 @@ using developwithpassion.bdd.contexts;
using developwithpassion.bdd.harnesses.mbunit;
using developwithpassion.bdddoc.core;
using gorilla.migrations.console.infrastructure;
+using MbUnit.Framework;
namespace tests.console
{
@@ -66,6 +67,44 @@ namespace tests.console
static IEnumerable<IThingy> results;
}
+
+ [Concern(typeof (SimpleContainer))]
+ public class when_attempting_to_resolve_an_item_from_the_container_that_has_not_been_registered : concern
+ {
+ because b = () =>
+ {
+ controller.doing(() => controller.sut.get_a<string>());
+ };
+
+ it should_throw_a_meaningful_exception = () =>
+ {
+ controller.exception_thrown_by_the_sut.should_be_an_instance_of<ComponentResolutionException<string>>();
+ };
+ }
+
+ [Concern(typeof (SimpleContainer))]
+ [Ignore]
+ public class when_resolving_an_ienumerable_of_anything : concern
+ {
+ context c = () =>
+ {
+ builder.register(() => new Thingy()).as_an<IThingy>();
+ builder.register(() => new AnotherThingy()).as_an<IThingy>();
+ };
+
+ because b = () =>
+ {
+ results = controller.sut.get_a<IEnumerable<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;
+ }
}