Question
Removal of Singleton Pattern from Cairngorm
Removal of the Singleton design pattern has many benefits for Cairngorm with regards to Unit Testing and Modules.
So far the list of Singletons used in Cairngorm are ServiceLocator, ModelLocator and CairngormEventDispacther.
I've looked into solutions to replace all Singletons with Dependency Injection.
So far here are a summary my conclusions.
The ModelLocator.
Singleton Purpose: A single point of reference for View or Commands to reference the Model.
Solution:
1, Pass the model object via the event to the command.
2, Pass the model object to the view via reference (DI).
All model instances are instantiated in a factory with all required dependencies passed via reference through the model object's constructor. The Model hierarchy represents the Display Object structure, therefore each presentation model should contain a reference to the next view's presentation model.
ServiceLocator:
Singleton Purpose: A single point of reference for Delegates to reference Services.
Solution: Similar to Prana.
Create Delegate instances in FrontController.
Have a reference of ServiceLocator within FrontController.
Set services on Delegates when Delegate gets instantiated.
Inject delegate into command.
This should remove the need for ServiceLocator to be a singleton.
CairngormEventDispatcher.
Signleton Purpose: Gives Model Objects and Commands the ability to dispatch Cairngorm Events.
Solution: Model and Commands do not talk to FrontController.
The Proposal here is that if we are using the Presentation Model pattern commands are only ever used as Responders.
Command sequencing is never initiated within a command.
Model events are routed through the view.
View events are bubbled events that are listen to by the frontController.
These are broad statements, with the intention of creating discussion rather then stating "facts".
I have created a version of the framework that uses each of these points that i'll post if need be.
So far the list of Singletons used in Cairngorm are ServiceLocator, ModelLocator and CairngormEventDispacther.
I've looked into solutions to replace all Singletons with Dependency Injection.
So far here are a summary my conclusions.
The ModelLocator.
Singleton Purpose: A single point of reference for View or Commands to reference the Model.
Solution:
1, Pass the model object via the event to the command.
2, Pass the model object to the view via reference (DI).
All model instances are instantiated in a factory with all required dependencies passed via reference through the model object's constructor. The Model hierarchy represents the Display Object structure, therefore each presentation model should contain a reference to the next view's presentation model.
ServiceLocator:
Singleton Purpose: A single point of reference for Delegates to reference Services.
Solution: Similar to Prana.
Create Delegate instances in FrontController.
Have a reference of ServiceLocator within FrontController.
Set services on Delegates when Delegate gets instantiated.
Inject delegate into command.
This should remove the need for ServiceLocator to be a singleton.
CairngormEventDispatcher.
Signleton Purpose: Gives Model Objects and Commands the ability to dispatch Cairngorm Events.
Solution: Model and Commands do not talk to FrontController.
The Proposal here is that if we are using the Presentation Model pattern commands are only ever used as Responders.
Command sequencing is never initiated within a command.
Model events are routed through the view.
View events are bubbled events that are listen to by the frontController.
These are broad statements, with the intention of creating discussion rather then stating "facts".
I have created a version of the framework that uses each of these points that i'll post if need be.
