-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain_in_memory_injector.py
56 lines (46 loc) · 1.7 KB
/
main_in_memory_injector.py
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
from injector import Injector, inject, Module, provider, singleton
from adapters.in_memory_person_repository import InMemoryPersonRepository
from adapters.in_memory_order_repository import InMemoryOrderRepository
from adapters.in_memory_connection import InMemoryConnection
from use_cases.unit_of_work import UnitOfWork
from domain.person import Person
from domain.order import Order
from use_cases.create_person_and_order_use_case import (
CreatePersonAndOrderUseCase)
class AppModule(Module):
@singleton
@provider
def provide_connection(self) -> InMemoryConnection:
return InMemoryConnection()
@singleton
@provider
def provide_person_repository(self) -> InMemoryPersonRepository:
return InMemoryPersonRepository()
@singleton
@provider
def provide_order_repository(self) -> InMemoryOrderRepository:
return InMemoryOrderRepository()
@inject
@singleton
@provider
def provide_unit_of_work(
self,
connection: InMemoryConnection,
person_repository: InMemoryPersonRepository,
order_repository: InMemoryOrderRepository
) -> UnitOfWork:
return UnitOfWork(connection, person_repository, order_repository)
@inject
@singleton
@provider
def provide_create_use_case(
self,
unit_of_work: UnitOfWork
) -> CreatePersonAndOrderUseCase:
return CreatePersonAndOrderUseCase(unit_of_work)
injector = Injector(AppModule())
create_use_case = injector.get(CreatePersonAndOrderUseCase)
new_person = Person(id=1, name="John Doe", age=30)
new_order = Order(id=1, order_date="2023-04-03", total_amount=100.0)
person, order = create_use_case.execute(new_person, new_order)
print(person, order)