Member Junction
    Preparing search index...

    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.

    Index

    Constructors

    Methods

    • Creates an instance of the class registered for the given base class and key.

      If no registration is found, falls back to instantiating the base class itself — a long-standing, deliberate behavior that legitimate consumers (notably BaseEntity) rely on. This method therefore does NOT return null for an unregistered key, so if (instance) is not a valid resolution-failure test. Use TryCreateInstance when you need to know whether the key actually resolved.

      Type Parameters

      • T

      Parameters

      • baseClass: unknown
      • key: string | null = null
      • ...params: unknown[]

      Returns T | null

      when the key does not resolve AND the anchor base class declares @RequiresSubclass() (i.e. it cannot function standalone). Bases without that marker keep the historical fallback behavior and only emit a structured warning.

    • Async version of CreateInstance that supports lazy loading. If no registration is found synchronously and lazy loaders are registered, attempts to load the missing module before retrying and creating the instance.

      Falls back to instantiating the base class directly if no registration is found even after lazy loading (same behavior as the sync CreateInstance) — including throwing when the anchor base is marked @RequiresSubclass().

      Type Parameters

      • T

      Parameters

      • baseClass: unknown
      • key: string | null = null
      • ...params: unknown[]

      Returns Promise<T | null>

    • 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: unknown
      • Optionalkey: string | null

      Returns ClassRegistration[]

    • Returns all registrations for a given base class whose Key matches the provided regex (tested against the trimmed-but-original-case key). Use for more nuanced discovery patterns than the prefix helper handles.

      Parameters

      • baseClass: unknown
      • pattern: RegExp

      Returns ClassRegistration[]

    • Returns all registrations for a given base class whose Key STARTS WITH the provided prefix (case-insensitive, trimmed). Useful when registrations follow a naming convention with a structured prefix (e.g. "<EntityName>:...").

      Prefer GetAllRegistrationsByMetadata when the discriminating data is structured — putting tuples in the key string is fragile.

      Parameters

      • baseClass: unknown
      • keyPrefix: string

      Returns ClassRegistration[]

    • Returns all registrations for a given base class whose attached Metadata bag satisfies the predicate. Registrations with no metadata are passed undefined to the predicate.

      This is the recommended discovery path for structured per-registration data (e.g. form-panel slots that filter by { entity, slot }). It avoids the brittleness of encoding tuples into the Key string.

      Parameters

      • baseClass: unknown
      • predicate: (
            metadata: Record<string, unknown> | undefined,
            registration: ClassRegistration,
        ) => boolean

      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: unknown
      • Optionalkey: string | null

      Returns ClassRegistration | null

    • Async version of GetRegistration that supports lazy loading. If no registration is found synchronously and lazy loaders are registered, attempts to load the missing module before retrying the lookup.

      Parameters

      • baseClass: unknown

        The base class to look up

      • Optionalkey: string | null

        Optional key to differentiate registrations

      Returns Promise<ClassRegistration | null>

      The matching ClassRegistration, or null if not found even after lazy loading

    • 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: unknown

        The root class to search for

      • Optionalkey: string | null

        Optional key to filter results

      Returns ClassRegistration[]

      Array of matching registrations

    • Use this method or the

      Parameters

      • baseClass: unknown

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

      • subClass: unknown

        A reference to the sub-class you are registering

      • key: string | null = 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 = false

        If true, 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. Defaults to false to preserve the original registration contract where classes are stored under the baseClass you specify.

      • Optionalmetadata: Record<string, unknown>

      Returns void

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

    • Registers a lazy loader callback that will be called when a class registration cannot be found synchronously. Multiple loaders can be registered and will be called in order until one succeeds.

      Parameters

      • loader: (baseClassName: string, key: string) => Promise<boolean>

        A function that receives (baseClassName, key) and returns a Promise indicating whether it successfully loaded the module containing the registration.

      Returns void

    • Explicit-result sibling of CreateInstance. Never throws for an unresolved key — returns a ClassResolutionResult so the caller can branch on Resolved.

      const res = MJGlobal.Instance.ClassFactory.TryCreateInstance<MyProvider>(MyProviderBase, key);
      if (!res.Resolved || !res.Instance) {
      LogError(`provider '${key}' did not resolve: ${res.Reason}`);
      return; // skip — do NOT install a hollow base instance
      }
      use(res.Instance);

      Type Parameters

      • T

      Parameters

      • baseClass: unknown
      • key: string | null = null
      • ...params: unknown[]

      Returns ClassResolutionResult<T>

    • Explicit-result, lazy-loading-aware sibling of TryCreateInstance. Never throws for an unresolved key.

      Type Parameters

      • T

      Parameters

      • baseClass: unknown
      • key: string | null = null
      • ...params: unknown[]

      Returns Promise<ClassResolutionResult<T>>