Skip to content

Commit

Permalink
Added a basic implementation of the DefaultStartupFacilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Paul S. Boodhoo committed Oct 23, 2010
1 parent 6a3b118 commit 052dc60
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 3 deletions.
Expand Up @@ -82,6 +82,7 @@
<Compile Include="infrastructure\TextWriterLoggerSpecs.cs" />
<Compile Include="MessageGeneratorSpecs.cs" />
<Compile Include="tasks\StartupCommandFactorySpecs.cs" />
<Compile Include="tasks\StartupFacilitiesSpecs.cs" />
<Compile Include="tasks\StartupPipelineBuilderSpecs.cs" />
<Compile Include="tasks\StartupSpecs.cs" />
<Compile Include="utility\CommonPipelineActions.cs" />
Expand Down
86 changes: 86 additions & 0 deletions product/nothinbutdotnetstore.specs/tasks/StartupFacilitiesSpecs.cs
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Machine.Specifications;
using Machine.Specifications.DevelopWithPassion.Rhino;
using nothinbutdotnetstore.infrastructure.containers;
using nothinbutdotnetstore.infrastructure.containers.basic;
using nothinbutdotnetstore.tasks.startup;

namespace nothinbutdotnetstore.specs.tasks
{
public class StartupFacilitiesSpecs
{
public abstract class concern : Observes<StartupFacilities,
DefaultStartupFacilities>
{
}

[Subject(typeof(DefaultStartupFacilities))]
public class when_registering_a_dependency_with_its_contract_and_implementation_specified : concern
{
Establish c = () =>
{
dependency_container = the_dependency<DependencyContainer>();
dependencies = new Dictionary<Type, DependencyFactory>();
provide_a_basic_sut_constructor_argument(dependencies);
};

Because b = () =>
sut.register<IDbConnection, SqlConnection>();

It should_register_an_autowiring_factory_that_will_be_used_to_create_the_dependency = () =>
{
var item = dependencies[typeof(IDbConnection)].ShouldBeAn<AutomaticDependencyFactory>();
item.dependency_container.ShouldEqual(dependency_container);
item.type.ShouldEqual(typeof(SqlConnection));
item.constructor_selection_strategy.ShouldBeAn<GreedyConstructorSelectionStrategy>();
};

static IDictionary<Type, DependencyFactory> dependencies;
static DependencyContainer dependency_container;
}

[Subject(typeof(DefaultStartupFacilities))]
public class when_registering_a_dependency_with_a_contract_and_provided_instance : concern
{
Establish c = () =>
{
the_connection = new SqlConnection();
dependencies = new Dictionary<Type, DependencyFactory>();
provide_a_basic_sut_constructor_argument(dependencies);
};

Because b = () =>
sut.register(the_connection);

It should_register_a_basic_dependency_factory_that_return_the_same_instance = () =>
{
var item = dependencies[typeof(IDbConnection)].ShouldBeAn<BasicDependencyFactory>();
item.create().ShouldEqual(the_connection);
};

static IDictionary<Type, DependencyFactory> dependencies;
static IDbConnection the_connection;
}

[Subject(typeof(DefaultStartupFacilities))]
public class when_requesting_the_dependencies_dictionary : concern
{
Establish c = () =>
{
dependencies = new Dictionary<Type, DependencyFactory>();
provide_a_basic_sut_constructor_argument(dependencies);
};

Because b = () =>
result = sut.original_factories;

It should_return_the_dictionary_it_is_created_with = () => { result.ShouldEqual(dependencies); };

static IDictionary<Type, DependencyFactory> dependencies;
static IDictionary<Type, DependencyFactory> result;
}
}
}
Expand Up @@ -5,9 +5,9 @@ namespace nothinbutdotnetstore.infrastructure.containers.basic
{
public class AutomaticDependencyFactory : DependencyFactory
{
DependencyContainer dependency_container;
ConstructorSelectionStrategy constructor_selection_strategy;
Type type;
public DependencyContainer dependency_container;
public ConstructorSelectionStrategy constructor_selection_strategy;
public Type type;

public AutomaticDependencyFactory(DependencyContainer dependency_container,
ConstructorSelectionStrategy constructor_selection_strategy, Type type)
Expand Down
1 change: 1 addition & 0 deletions product/nothinbutdotnetstore/nothinbutdotnetstore.csproj
Expand Up @@ -88,6 +88,7 @@
<Compile Include="tasks\startup\commands\ConfiguringApplicationRoutes.cs" />
<Compile Include="tasks\startup\commands\A_ConfiguringInfrastructure.cs" />
<Compile Include="tasks\startup\DefaultStartupCommandFactory.cs" />
<Compile Include="tasks\startup\DefaultStartupFacilities.cs" />
<Compile Include="tasks\startup\Start.cs" />
<Compile Include="tasks\startup\Startup.cs" />
<Compile Include="tasks\startup\StartupCommand.cs" />
Expand Down
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using nothinbutdotnetstore.infrastructure.containers;
using nothinbutdotnetstore.infrastructure.containers.basic;

namespace nothinbutdotnetstore.tasks.startup
{
public class DefaultStartupFacilities : StartupFacilities
{
IDictionary<Type, DependencyFactory> factories;
DependencyContainer dependency_container;

public DefaultStartupFacilities(IDictionary<Type, DependencyFactory> factories, DependencyContainer dependency_container)
{
this.factories = factories;
this.dependency_container = dependency_container;
}

public void register<Contract, Implementation>()
{
factories.Add(typeof(Contract),new AutomaticDependencyFactory(dependency_container,
new GreedyConstructorSelectionStrategy(),typeof(Implementation)));
}

public void register<Contract>(Contract instance)
{
factories.Add(typeof(Contract),new BasicDependencyFactory(() => instance));
}

public IDictionary<Type, DependencyFactory> original_factories
{
get {return factories;}
}
}
}

0 comments on commit 052dc60

Please sign in to comment.