Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a basic implementation of the DefaultStartupFacilities
- Loading branch information
Jean-Paul S. Boodhoo
committed
Oct 23, 2010
1 parent
6a3b118
commit 052dc60
Showing
5 changed files
with
126 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
product/nothinbutdotnetstore.specs/tasks/StartupFacilitiesSpecs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
product/nothinbutdotnetstore/tasks/startup/DefaultStartupFacilities.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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;} | ||
} | ||
} | ||
} |