Skip to content

Commit

Permalink
fix listings for di and bootstrap chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
hjwp committed Jul 15, 2019
1 parent aa9d8cf commit 08354e7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
13 changes: 8 additions & 5 deletions appendix_bootstrap.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ Here's what a bootstrap script could look like:
def bootstrap(
start_orm=orm.start_mappers,
session_factory=DEFAULT_SESSION_FACTORY,
send_mail=email.send,
notifications=None,
publish=redis_pubsub.publish,
):
start_orm()
uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory=session_factory)
bus = messagebus.MessageBus(uow=uow, send_mail=send_mail, publish=publish)
if notifications is None:
notifications = EmailNotifications(smtp_host=EMAIL_HOST, port=EMAIL_PORT)
bus = messagebus.MessageBus(uow=uow, notifications=notifications, publish=publish)
return bus
----
====
Expand Down Expand Up @@ -132,7 +134,7 @@ to get a custom messagebus:
def sqlite_bus(in_memory_sqlite_db):
yield bootstrap.bootstrap(
session_factory=sessionmaker(bind=in_memory_sqlite_db),
send_mail=mock.Mock(),
notifications=mock.Mock(),
publish=mock.Mock(),
)
clear_mappers()
Expand Down Expand Up @@ -180,7 +182,8 @@ One fix is to split the "pub" from the "sub":

image::images/appendix_bootstrap_dependency_graph_2.png["Dependency graph with bootstrap script and no circular deps"]

Now we have what our esteemed tech reviewer David Seddon would call a "rocky road architecture": all the dependencies
flow in one direction.
Now we have what our esteemed tech reviewer David Seddon would call a "rocky
road architecture": all the dependencies flow in one direction.

TODO: alternative fix by making an abstract redis thingie?

23 changes: 15 additions & 8 deletions chapter_10_dependency_injection.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ we have an (explicit) dependency on an abstraction:
.The explicit dependency is more abstract (src/allocation/handlers.py)
====
[source,python]
[role="non-head"]
----
def send_out_of_stock_notification(
event: events.OutOfStock, send_mail: Callable,
Expand Down Expand Up @@ -185,6 +186,7 @@ Here's one way to do it:
.MessageBus as a class (src/allocation/messagebus.py)
====
[source,python]
[role="non-head"]
----
class MessageBus: #<1>
Expand Down Expand Up @@ -258,7 +260,7 @@ What else changes in the bus?
Here's the core of our dependency injection approach then. As you'll see
there's not much to it:

[[messagebus_does_DI]]
[[messagebus_does_DI0]]
.Dependency injection in 3 lines of code (src/allocation/messagebus.py)
====
[source,python]
Expand Down Expand Up @@ -326,6 +328,7 @@ dependencies we want to use:
.Flask initialises a bus with the production dependencies (src/allocation/flask_app.py)
====
[source,python]
[role="non-head"]
----
from allocation import (
commands, email, exceptions, messagebus, orm, redis_pubsub, unit_of_work,
Expand All @@ -348,6 +351,7 @@ bus = messagebus.MessageBus(
.So does redis (src/allocation/redis_pubsub.py)
====
[source,python]
[role="non-head"]
----
def get_bus(): #<1>
return messagebus.MessageBus(
Expand Down Expand Up @@ -378,10 +382,11 @@ def handle_change_batch_quantity(m, bus: messagebus.MessageBus):
=== Initialising DI In Our Tests


[[totally_reimplement_bootstrap]]
[[fakebus]]
.Handler tests just do their own bootstrap (tests/unit/test_handlers.py)
====
[source,python]
[role="non-head"]
----
class FakeBus(messagebus.MessageBus):
def __init__(self):
Expand Down Expand Up @@ -409,14 +414,15 @@ class TestAddBatch:

We've got two types of dependency:

[[two_types_of_dependency]]
.Two types of dependency
[[messagebus_does_DI]]
.Two types of dependency (src/allocation/messagebus.py)
====
[source,python]
[role="non-head"]
----
uow: unit_of_work.AbstractUnitOfWork, #<1>
send_mail: Callable, #<2>
publish: Callable, #<2>
uow: unit_of_work.AbstractUnitOfWork, #<1>
send_mail: Callable, #<2>
publish: Callable, #<2>
----
====

Expand Down Expand Up @@ -457,6 +463,7 @@ class Notifications(abc.ABC):
def send(self, destination, message):
raise NotImplementedError
...
class EmailNotifications(Notifications):
Expand Down Expand Up @@ -526,7 +533,7 @@ class FakeBus(messagebus.MessageBus):
we can use it in our tests:

[[test_with_fake_notifs]]
.Tests change slightly (test/unit/tests_handlers.py)
.Tests change slightly (tests/unit/test_handlers.py)
====
[source,python]
----
Expand Down
2 changes: 1 addition & 1 deletion code
Submodule code updated from 61a3cd to c70df4

0 comments on commit 08354e7

Please sign in to comment.