Skip to content

Commit

Permalink
Container registration scope can be updated (that was an unwanted bre…
Browse files Browse the repository at this point in the history
…aking change from v1)
  • Loading branch information
ybainier committed Aug 15, 2016
1 parent 78e3bcb commit 1ff2004
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Hypodermic.Tests/ContainerBuilderTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ namespace Testing
BOOST_CHECK_EQUAL(instance->i, expectedNumber);
}

BOOST_AUTO_TEST_CASE(should_update_registrations)
{
// Arrange
ContainerBuilder builder;
builder.registerType< TypeWithOneDependency >();

auto container = builder.build();

// Act
ContainerBuilder newBuilder;
newBuilder.registerType< ProvidedDependency >().as< ProvidedDependencyBase >();

newBuilder.updateContainer(*container);

// Assert
std::shared_ptr< TypeWithOneDependency > instance;

BOOST_REQUIRE_NO_THROW(instance = container->resolve< TypeWithOneDependency >());

BOOST_REQUIRE(instance != nullptr);
BOOST_CHECK(instance->dependency != nullptr);
}

BOOST_AUTO_TEST_SUITE_END()

} // namespace Testing
Expand Down
10 changes: 10 additions & 0 deletions Hypodermic/Container.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ namespace Hypodermic
return std::make_shared< NestedRegistrationScope >(m_registrationScope);
}

/// <summary>
/// Update the current registration scope
/// </summary>
/// <param name="scopeUpdater">The function responsible for updating the registration scope</param>
void updateRegistrationScope(const std::function< void(IRegistrationScope&) >& scopeUpdater) const
{
if (scopeUpdater)
scopeUpdater(*m_registrationScope);
}

/// <summary>
/// Resolve an instance of type T
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions Hypodermic/ContainerBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ namespace Hypodermic
return createAndRegisterContainerInstance(scope);
}

/// <summary>
/// Update a container
/// </summary>
/// <param name="container">The container to update</param>
void updateContainer(Container& container)
{
HYPODERMIC_LOG_INFO("Updating container");

for (auto&& x : m_registrationDescriptors)
container.updateRegistrationScope(m_buildActions[x]);
}

/// <summary>
/// Build a new and nested container from a passed Container
/// </summary>
Expand Down

0 comments on commit 1ff2004

Please sign in to comment.