Consolidated ASP Classic MVC framework from best components
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

66 řádky
2.9KB

  1. <%
  2. '=======================================================================================================================
  3. ' Controller Factory
  4. '
  5. ' Centralizes controller instantiation by name so the MVC dispatcher (a WSC,
  6. ' with its own isolated script engine) never has to Execute/Eval a string
  7. ' built from a route-supplied controller name to resolve a class. Each
  8. ' controller factory function (e.g. HomeController(), which already exists as
  9. ' each controller's own singleton accessor) is registered once, by reference,
  10. ' via GetRef - not by re-deriving it from a string at dispatch time.
  11. '=======================================================================================================================
  12. Class ControllerFactory_Class
  13. Private m_factories
  14. Private Sub Class_Initialize()
  15. Set m_factories = Server.CreateObject("Scripting.Dictionary")
  16. m_factories.CompareMode = 1 ' vbTextCompare, case-insensitive
  17. End Sub
  18. Private Sub Class_Terminate()
  19. Set m_factories = Nothing
  20. End Sub
  21. '---------------------------------------------------------------------------------------------------------------------
  22. ' Register a controller's factory function (its own singleton accessor,
  23. ' e.g. GetRef("HomeController")) under its route name.
  24. '---------------------------------------------------------------------------------------------------------------------
  25. Public Sub Register(controllerName, factoryFunctionRef)
  26. Dim key : key = LCase(Trim(controllerName))
  27. If Not m_factories.Exists(key) Then
  28. m_factories.Add key, factoryFunctionRef
  29. End If
  30. End Sub
  31. '---------------------------------------------------------------------------------------------------------------------
  32. ' Create(controllerName) -> the controller singleton instance
  33. '---------------------------------------------------------------------------------------------------------------------
  34. Public Function Create(controllerName)
  35. Dim key, fn
  36. key = LCase(Trim(controllerName))
  37. If Not m_factories.Exists(key) Then
  38. Err.Raise vbObjectError + 1100, "ControllerFactory.Create", "No factory registered for controller: " & controllerName
  39. End If
  40. Set fn = m_factories(key)
  41. Set Create = fn()
  42. End Function
  43. '---------------------------------------------------------------------------------------------------------------------
  44. Public Function IsRegistered(controllerName)
  45. IsRegistered = m_factories.Exists(LCase(Trim(controllerName)))
  46. End Function
  47. End Class
  48. ' Singleton instance
  49. Dim ControllerFactory_Class__Singleton
  50. Function ControllerFactory()
  51. If IsEmpty(ControllerFactory_Class__Singleton) Then
  52. Set ControllerFactory_Class__Singleton = New ControllerFactory_Class
  53. End If
  54. Set ControllerFactory = ControllerFactory_Class__Singleton
  55. End Function
  56. %>

Powered by TurnKey Linux.