|
- <%
- '=======================================================================================================================
- ' Controller Factory
- '
- ' Centralizes controller instantiation by name so the MVC dispatcher (a WSC,
- ' with its own isolated script engine) never has to Execute/Eval a string
- ' built from a route-supplied controller name to resolve a class. Each
- ' controller factory function (e.g. HomeController(), which already exists as
- ' each controller's own singleton accessor) is registered once, by reference,
- ' via GetRef - not by re-deriving it from a string at dispatch time.
- '=======================================================================================================================
-
- Class ControllerFactory_Class
- Private m_factories
-
- Private Sub Class_Initialize()
- Set m_factories = Server.CreateObject("Scripting.Dictionary")
- m_factories.CompareMode = 1 ' vbTextCompare, case-insensitive
- End Sub
-
- Private Sub Class_Terminate()
- Set m_factories = Nothing
- End Sub
-
- '---------------------------------------------------------------------------------------------------------------------
- ' Register a controller's factory function (its own singleton accessor,
- ' e.g. GetRef("HomeController")) under its route name.
- '---------------------------------------------------------------------------------------------------------------------
- Public Sub Register(controllerName, factoryFunctionRef)
- Dim key : key = LCase(Trim(controllerName))
- If Not m_factories.Exists(key) Then
- m_factories.Add key, factoryFunctionRef
- End If
- End Sub
-
- '---------------------------------------------------------------------------------------------------------------------
- ' Create(controllerName) -> the controller singleton instance
- '---------------------------------------------------------------------------------------------------------------------
- Public Function Create(controllerName)
- Dim key, fn
- key = LCase(Trim(controllerName))
-
- If Not m_factories.Exists(key) Then
- Err.Raise vbObjectError + 1100, "ControllerFactory.Create", "No factory registered for controller: " & controllerName
- End If
-
- Set fn = m_factories(key)
- Set Create = fn()
- End Function
-
- '---------------------------------------------------------------------------------------------------------------------
- Public Function IsRegistered(controllerName)
- IsRegistered = m_factories.Exists(LCase(Trim(controllerName)))
- End Function
- End Class
-
- ' Singleton instance
- Dim ControllerFactory_Class__Singleton
- Function ControllerFactory()
- If IsEmpty(ControllerFactory_Class__Singleton) Then
- Set ControllerFactory_Class__Singleton = New ControllerFactory_Class
- End If
- Set ControllerFactory = ControllerFactory_Class__Singleton
- End Function
- %>
|