How Alchemic works
The following are the main architectural items that you will come across when working with Alchemic.
The context
Every interaction with Alchemic takes place through the Alchemic context. Implementing the ALCContext
protocol, the context is essentially a self starting singleton instance that contains all the methods needed to tell Alchemic about your app. The context is also responsible for starting Alchemic, resolving dependencies, performing injections and a bunch of other things.
The data model
Behind the context, Alchemic maintains an in memory data model. It’s where Alchemic stores all the your app’s setup. When starting up, Alchemic scans your project and executes any setup methods it finds to create this model. This is known as the registration phase of Alchemic’s startup process.
Object factories
The data model contains a collection of Object factories. These describe what sort of object you want them to manage and the dependencies they have. Object factories can manage singleton instances, factories (AKA Templates) and references to externally created objects such as UIViewControllers
. Object factories are usually created automatically during registration.
Alchemic’s boot sequence
Alchemic will automatically start itself when your application loads. It follows this logic:
- Self starts on a background thread so that your application’s startup is not impacted.
- Registration phase: Scans all classes in your app for dependency injection declarations and executes them to setup the model.
- Resolving phase: Resolves all references and configures the internal model based on the found declarations.
- Singletons phase: Instantiates any classes declared as Singletons and wires up their dependencies.
- Post the “AlchemicFinishedLoading” notification.