liveBook · Manning

Dependency injection

As explained in Build Python Web Apps with Streamlit

Dependency injection is a software design pattern in which an object receives the components it depends on from outside rather than constructing them internally.

Dependency injection keeps core logic focused by giving an object the components it needs instead of making those components internally. section 10.5.2

Passing services into an agent graph

Create the language model and vector store in Bot, then pass them into SupportAgentGraph. The graph stores them as self.llm and self.vector_store, leaving setup outside the graph’s core logic and making automated testing easier. section 10.5.2

A practical dependency-injection arrangement separates responsibilities:

  • Bot creates configuration-dependent services such as VectorStore. section 10.5.2
  • SupportAgentGraph receives those services through its constructor. section 10.5.2
  • Graph nodes use the injected vector store for retrieval without constructing it themselves. section 10.5.2

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines the pattern and demonstrates constructor injection, but does not develop testing techniques beyond stating the benefit.
Examples
Uses Bot, SupportAgentGraph, an LLM, and a VectorStore as the construction-and-injection example.
What it assumes
Assumes familiarity with classes, constructors, objects, and application components such as LLMs and vector stores.
What you can do after
Build applications that pass configured services into focused components instead of constructing dependencies inside them.
How it connects
Connects dependency injection with object construction, automated testing, vector stores, language models, and graph logic.

Also explained in

Compare how 21 books explain dependency injection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.