Understanding IMetadataFactory
TheIMetadataFactory interface (source:UiMetadataFramework.Core/Binding/Component/IMetadataFactory.cs:8) defines a single method:
DefaultMetadataFactory
The framework providesDefaultMetadataFactory (source:UiMetadataFramework.Core/Binding/Component/DefaultMetadataFactory.cs:11) which handles most scenarios. It:
- Iterates over all configuration attributes
- Finds properties marked with
[ConfigurationProperty] - Builds a dictionary of configuration values
- Validates mandatory configurations
- Returns the dictionary as JSON-serializable metadata
How DefaultMetadataFactory Works
Creating Custom Factories
Custom factories are useful when you need to:- Transform configuration data before sending to client
- Generate metadata from external sources
- Add computed properties to metadata
- Validate complex configuration rules
Example: DropdownMetadataFactory
Here’s a real example from UiMetadataFramework.Basic (source:UiMetadataFramework.Basic/Inputs/Dropdown/DropdownMetadataFactory.cs:16):- Extends
DefaultMetadataFactoryto leverage existing functionality - Overrides
AugmentConfigurationto add custom logic - Determines the dropdown’s data source (enum, inline, or remote)
- Generates appropriate metadata for each source type
- Uses the DI container to instantiate inline sources
Field Metadata Factories
For customizing how individual fields are processed, implementIFieldMetadataFactory (source:UiMetadataFramework.Core/Binding/Field/IFieldMetadataFactory.cs:8):
DefaultFieldMetadataFactory
The default implementation (source:UiMetadataFramework.Core/Binding/Field/DefaultFieldMetadataFactory.cs:7):When to Use Custom Factories
Use DefaultMetadataFactory when:
- Configuration is simple key-value pairs
- No data transformation is needed
- Standard validation is sufficient
Create a custom factory when:
- You need to compute metadata from multiple sources
- Configuration requires transformation (e.g., enum to dropdown items)
- You need to validate complex business rules
- You want to integrate with external services via DI
- You need to generate metadata dynamically based on types
Override AugmentConfiguration when:
- You want default behavior plus custom additions
- You need to modify the result dictionary
- You want to add computed properties
Implement IMetadataFactory from scratch when:
- You need completely different metadata structure
- Default behavior doesn’t fit your use case
- You want full control over the metadata generation
Best Practices
- Extend DefaultMetadataFactory: Start with the default and override
AugmentConfigurationrather than implementingIMetadataFactoryfrom scratch - Use the DI container: Access services through
binder.Container.GetService()for testability - Validate early: Throw
BindingExceptionwith clear messages when configuration is invalid - Keep it pure: Avoid side effects in metadata generation
- Cache expensive operations: Use static caches for reflection-heavy operations
- Document metadata structure: Clearly document what metadata your factory produces for frontend developers
Next Steps
- Learn about dependency injection in UIMF
- See how to create custom components
- Understand client integration to consume the metadata