packages/graphrag-common/example_notebooks/factory_module_example.ipynb
# Copyright (c) 2026 Microsoft Corporation.
# Licensed under the MIT License.
The Factory class provides a flexible dependency injection pattern that can register and create instances of classes implementing a common interface using string-based strategies. It supports both transient scope (creates new instances on each request) and singleton scope (returns the same instance after first creation).
from abc import ABC, abstractmethod
from graphrag_common.factory import Factory
class SampleABC(ABC):
"""Abstract base class for sample implementations."""
@abstractmethod
def get_value(self) -> str:
"""Return the value of the sample implementation."""
msg = "Subclasses must implement the get_value method."
raise NotImplementedError(msg)
class ConcreteClass(SampleABC):
"""A concrete implementation of SampleABC."""
def __init__(self, value: str):
print(f"Creating ConcreteClass with value: {value}")
self._value = value
def get_value(self) -> str:
"""Return the value of the sample implementation."""
return self._value
class SampleFactory(Factory[SampleABC]):
"""A Factory for SampleABC classes."""
factory = SampleFactory()
# Registering transient services
# A new one is created for every request
factory.register("some_strategy", ConcreteClass)
trans1 = factory.create("some_strategy", {"value": "test1"})
trans2 = factory.create("some_strategy", {"value": "test2"})
print(f"Value from trans1: {trans1.get_value()}") # Output: test1
print(f"Value from trans2: {trans2.get_value()}") # Output: test2
assert trans1 is not trans2
assert trans1.get_value() == "test1"
assert trans2.get_value() == "test2"
# Registering singleton services
# After first creation, the same one is returned every time
factory.register("some_other_strategy", ConcreteClass, scope="singleton")
single1 = factory.create("some_other_strategy", {"value": "singleton"})
single2 = factory.create("some_other_strategy", {"value": "ignored"})
print(f"Value from single1: {single1.get_value()}") # Output: singleton
print(f"Value from single2: {single2.get_value()}") # Output: singleton
assert single1 is not single2
assert single1.get_value() == "singleton"
assert single2.get_value() == "ignored"