Class BlockFactory

Default implementation of IBlockFactory that maps block type strings to concrete block instances using a normalized key lookup.

Inheritance
BlockFactory
Implements
Namespace: MOSAIC.Components.Factory
Assembly: MOSAIC.dll
Syntax
public sealed class BlockFactory : IBlockFactory
Remarks

The factory accepts a JsonModel whose Type property is normalized (trimmed and lowercased) and matched against a registry of known block types. Each match delegates to the corresponding static ConfigureInput method on the target model class.

Multiple aliases (short name, fully-qualified name, legacy name) are supported for backward compatibility.

Constructors

BlockFactory(IServiceProvider)

Default implementation of IBlockFactory that maps block type strings to concrete block instances using a normalized key lookup.

Declaration
public BlockFactory(IServiceProvider sp)
Parameters
Type Name Description
IServiceProvider sp

The application's IServiceProvider passed to every block's ConfigureInput method for dependency resolution.

Remarks

The factory accepts a JsonModel whose Type property is normalized (trimmed and lowercased) and matched against a registry of known block types. Each match delegates to the corresponding static ConfigureInput method on the target model class.

Multiple aliases (short name, fully-qualified name, legacy name) are supported for backward compatibility.

Methods

CanCreate(string)

Whether type names a block this factory can build, decided without building one. The check resolves the same switch Create(JsonModel) uses, so it cannot drift from it.

Declaration
public bool CanCreate(string type)
Parameters
Type Name Description
string type

A Type value, as written by a saved graph.

Returns
Type Description
bool

true when a saved block of this type would load.

Create(JsonModel)

Creates a block instance from the supplied JsonModel definition.

Declaration
public object Create(JsonModel m)
Parameters
Type Name Description
JsonModel m

The JSON model containing the block type and configuration parameters.

Returns
Type Description
object

The newly created block instance.

Exceptions
Type Condition
KeyNotFoundException

Thrown when Type does not match any known block type.

CreateCore(JsonModel)

Declaration
private Func<object> CreateCore(JsonModel m)
Parameters
Type Name Description
JsonModel m
Returns
Type Description
Func<object>

Create<T>(JsonModel)

Creates a block instance and casts it to the requested type T.

Declaration
public T Create<T>(JsonModel m) where T : class
Parameters
Type Name Description
JsonModel m

The JSON model containing the block type and configuration parameters.

Returns
Type Description
T

The created block instance cast to T.

Type Parameters
Name Description
T

The expected type of the block. Must be a reference type.

Exceptions
Type Condition
KeyNotFoundException

Thrown when Type does not match any known block type.

InvalidCastException

Thrown when the created block is not assignable to T.

GetBlockType(string)

Maps a normalized type key to the constructor call for that block, without running it. See Create(JsonModel) for the wrapper that runs it, and CanCreate(string) for the probe that only needs to know the key is known.

Declaration
public Type? GetBlockType(string type)
Parameters
Type Name Description
string type
Returns
Type Description
Type

NormalizeKey(string)

Normalizes a block type key by trimming whitespace and converting to lowercase.

Declaration
private static string NormalizeKey(string key)
Parameters
Type Name Description
string key

The raw block type string.

Returns
Type Description
string

The normalized, lowercase key.

NotAssignable(JsonModel, Type)

Reports a block that was created but is of the wrong type, then produces the exception.

Declaration
private static InvalidCastException NotAssignable(JsonModel m, Type requested)
Parameters
Type Name Description
JsonModel m

The model that was created.

Type requested

The type the caller asked for.

Returns
Type Description
InvalidCastException

The exception the caller throws.

ResolveRegistration(JsonModel)

Declaration
private (Type Type, Func<object> Create) ResolveRegistration(JsonModel m)
Parameters
Type Name Description
JsonModel m
Returns
Type Description
(Type Type, Func<object> Create)

UnknownBlockType(JsonModel)

Reports an unrecognised block type, then produces the exception for it.

Declaration
private static KeyNotFoundException UnknownBlockType(JsonModel m)
Parameters
Type Name Description
JsonModel m

The model whose Type matched no case.

Returns
Type Description
KeyNotFoundException

The exception the caller throws.

Implements

IBlockFactory

Search documentation

Start typing to search the documentation.