ClassFactory is used to register and create instances of classes. It is a singleton class that can be used to register a sub-class for a given base class and key. Do NOT directly attempt to instantiate this class, instead use the static Instance property of the MJGlobal class to get the instance of the ClassFactory for your application.

Constructors

Properties

_registrations: ClassRegistration[] = []

Methods

  • Creates an instance of the class registered for the given base class and key. If no registration is found, will return an instance of the base class.

    Type Parameters

    • T

    Parameters

    • baseClass: any
    • key: string = null
    • Rest ...params: any[]

    Returns T

  • Returns all registrations for a given base class and key. If key is not provided, will return all registrations for the base class.

    Parameters

    • baseClass: any
    • key: string = undefined

    Returns ClassRegistration[]

  • Returns the registration with the highest priority for a given base class and key. If key is not provided, will return the registration with the highest priority for the base class.

    Parameters

    • baseClass: any
    • key: string = undefined

    Returns ClassRegistration

  • Returns all registrations that have the specified root class, regardless of what base class was used in the registration. This is useful for finding all registrations in a class hierarchy.

    Parameters

    • rootClass: any

      The root class to search for

    • key: string = undefined

      Optional key to filter results

    Returns ClassRegistration[]

    Array of matching registrations

  • Use this method or the

    Parameters

    • baseClass: any

      A reference to the base class you are registering a sub-class for

    • subClass: any

      A reference to the sub-class you are registering

    • key: string = null

      A key can be used to differentiate registrations for the same base class/sub-class combination. For example, in the case of BaseEntity and Entity object subclasses we'll have a LOT of entries and we want to get the highest priority registered sub-class for a specific key. In that case, the key is the entity name, but the key can be any value you want to use to differentiate registrations.

    • priority: number = 0

      Higher priority registrations will be used over lower priority registrations. If there are multiple registrations for a given base class/sub-class/key combination, the one with the highest priority will be used. If there are multiple registrations with the same priority, the last one registered will be used. Finally, if you do NOT provide this setting, the order of registrations will increment the priority automatically so dependency injection will typically care care of this. That is, in order for Class B, a subclass of Class A, to be registered properly, Class A code has to already have been loaded and therefore Class A's RegisterClass decorator was run. In that scenario, if neither Class A or B has a priority setting, Class A would be 1 and Class B would be 2 automatically. For this reason, you only need to explicitly set priority if you want to do something atypical as this mechanism normally will solve for setting the priority correctly based on the furthest descendant class that is registered.

    • skipNullKeyWarning: boolean = false

      If true, will not print a warning if the key is null or undefined. This is useful for cases where you know that the key is not needed and you don't want to see the warning in the console.

    • autoRegisterWithRootClass: boolean = true

      If true (default), will automatically register the subclass with the root class of the baseClass hierarchy. This ensures proper priority ordering when multiple subclasses are registered in a hierarchy.

    Returns void

    Register Class

    decorator to register a sub-class for a given base class.