Esempio n. 1
0
from pyioc3 import StaticContainerBuilder, StaticContainer

ioc_builder = StaticContainerBuilder()


def my_factory_wrapper(ctx: StaticContainer):

    def my_factory(name: str):
        greeting = ctx.get("greeting")
        return "{}, {}!".format(greeting, name)

    return my_factory


ioc_builder.bind_factory(
    annotation="my_factory",
    factory=my_factory_wrapper)
ioc_builder.bind_constant(
    annotation="greeting",
    value="Hello")

ioc = ioc_builder.build()

fact = ioc.get("my_factory")
value: str = fact("Factory")
print(value)
Esempio n. 2
0
        # You could also return a default here.
        raise Exception("One of the many issues with the service locator anti-pattern...")

    return create_provider


ioc_builder = StaticContainerBuilder()

# Bind each provider under it's own annotation
ioc_builder.bind(
    annotation=Provider1,
    implementation=Provider1)
ioc_builder.bind(
    annotation=Provider2,
    implementation=Provider2)

# Bind the factory
ioc_builder.bind_factory(
    annotation="create_provider",
    factory=create_provider_factory)

# Application Entry
ioc_builder.bind(
    annotation=Main,
    implementation=Main)

ioc = ioc_builder.build()

main = ioc.get(Main)
main(provider="p2")