E
E
Evgeny Nikitin2015-11-17 21:50:26
C++ / C#
Evgeny Nikitin, 2015-11-17 21:50:26

What is Autowiring in DI Containers?

I began to study the structure of DI containers in general and in particular for C ++. After reviewing a number of implementations, I chose Hypodermic for myself. In general, using Unit tests, I figured out how to work with the library, but I didn’t quite understand about Autowiring. What it is?
PS I am attaching the text of two Unit-tests with the problem in question.

BOOST_AUTO_TEST_CASE(autowired_registration_follows_the_usual_registration_rules)
{
    // Arrange
    ContainerBuilder c;

    // Act
    c.autowireType< ServiceA >().as< IServiceA >();
    c.autowireType< ServiceB >().singleInstance();

    // Assert
    auto container = c.build();

    auto serviceB = container->resolve< ServiceB >();
    auto nullServiceB = container->resolve< IServiceB >();

    BOOST_CHECK(serviceB != nullptr);
    BOOST_CHECK(nullServiceB == nullptr);
    BOOST_CHECK(serviceB == container->resolve< ServiceB >());
}

BOOST_AUTO_TEST_CASE(autowired_registration_can_resolve_all_services)
{
    // Arrange
    ContainerBuilder c;
    c.autowireType< ServiceA >().as< IServiceA >().as< IRunWithScissors >();

    c.autowireType< ServiceB >().as< IServiceB >();
    c.autowireType< ServiceRunningWithScissors >().as< IServiceB >();

    c.autowireType< ServiceBController >();

    auto container = c.build();

    // Act
    auto serviceBController = container->resolve< ServiceBController >();

    // Assert
    BOOST_CHECK(serviceBController != nullptr);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-11-18
@eunikitin

Autowiring - autowiring. That is, the container itself, based on types, tries to guess who depends on who. You don't need to explicitly list all dependencies.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question