Arena’s ApplicationContext is an implementation of Service Locator architectural pattern.
You have a Singleton object (like a Repository / Home), or a configuration object and you want to use different implementations for testing and running an application.
An Application Context has the following services:
At the beginning of an application, we must create repository objects and let application context reference them:
// Xtend override protected Window<?> createMainWindow() { ApplicationContext.instance.configureSingleton(typeof(Whatever), RepoWhatever.instance) return new WhateverWindow(this) } // Java @Override protected Window<?> createMainWindow() { ApplicationContext.getInstance() .configureSingleton(Whatever.class, RepoWhatever.getInstance()); return new BuscarCelularesWindow(this); }
// Xtend class CelularesBootstrap extends CollectionBasedBootstrap { new() { ApplicationContext.instance => [ configureSingleton(typeof(Whatever), RepoWhatever.instance) ] }
// Java >>TestWhatever @Before @Override public void init() { ... ApplicationContext.getInstance().configureSingleton(Whatever.class, new MockRepoWhatever()); // Xtend >>TestWhatever @Before override init() { ... ApplicationContext.instance.configureSingleton(typeof(Whatever), new MockRepoWhatever)
Application model uses the properly configured repo:
def getRepoCustomers() { ApplicationContext.instance.getSingleton(typeof(Customer)) } def void search() { results = repoCustomers().search(name) ...
So application model doesn’t know if it is using a real or a mocked repository, as shown in this picture: