Member-only story

Factory: Multiple Module Registration

How to initialize everything before anything is initialized.

Michael Long
Better Programming
Photo by Susan Holt Simpson on Unsplash

When you want to use a dependency injection system like Factory with multiple modules you often run into a “Who’s on first” dilemma.

Let’s say that ModuleP specifies an abstractAccountLoading protocol.

// ModuleP
public protocol AccountLoading {
func load() -> [Account]
}

Moving on, we have an accounting module, ModuleA, that displays our accounts, but needs one of those loaders to load them.

Finally, we have one more module, let’s call this one ModuleB, that knows how to build loaders of any type that we need.

Note that ModuleA and ModuleB are independent. Neither one knows about the other one, but both have a direct dependency on ModuleP, our master of models and protocols.

This is a classic modular contractural pattern.

But we have an application to build. So how does ModuleA get an instance of an account loader, when it knows nothing about ModuleB?

Well, this is an article about dependency injection, so let’s roll with that.

Resolver

In my earlier dependency injection system, Resolver, that wouldn’t really be a…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Responses (5)

What are your thoughts?