-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathTestDependencyInjection.cxx
67 lines (52 loc) · 2.02 KB
/
TestDependencyInjection.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* TestDependencyInjection.cxx
*
* Created on: Oct 23, 2021
* Author: <a href="mailto:damirlj@yahoo.com">Damir Ljubic</a>
*/
#include "DIContainer.hxx"
#include "TestCases.hxx"
#include "ClientWithVariant.hxx"
#include "TestDependencyInjection.hxx"
namespace test::di
{
int testDI()
{
using namespace std;
using namespace utils::di;
auto diContainer = DIContainer::make_di_container();
if (!diContainer) return 1;
try
{
diContainer->add<Service1, ConsoleService>();
diContainer->add<Service2, LocatorService>(std::string("GNSS"));
}
catch(const exception& e)
{
cerr << e.what() << '\n'; // unit test
return 2;
}
// Client is only aware of the interfaces on which it depends - not the implementation behind
// 1. Inject dependency at client side: through the <b>constructor</b>
//const auto service = diContainer->get<Service1>(true); // shared instance
const auto service1 = diContainer->get<Service1>(false); // new instance
Client1 client1(service1.has_value() ? *service1 : nullptr);
client1("Alex");
// 2. Inject dependency at client side: through the <b>setter method</b>
const auto service2 = diContainer->get<Service2>(false);
Client2 client2;
client2.setService(service2.has_value() ? *service2 : nullptr);
client2(283.37f, 112.11f);
/*
* Example of injected the dependency into client
* that stores them into typed-safe union(std::variant) and can
* use the visitor pattern along with overloaded function object, or
* can specify for each DO matching handler
*/
test::di::visitor::Client<std::shared_ptr<Service1>, std::shared_ptr<Service2>> client3 ({*service1, *service2});
client3.call<std::shared_ptr<Service2>>([](std::shared_ptr<Service2>& service){
service->coordinate(106.34f, 89.21f);
});
return 0;
}
}