IriStrategy.namedMapper constructor
Creates a reference to a named mapper for this IRI strategy.
Use this constructor when you want to provide a custom IriTermMapper
implementation via dependency injection. With this approach, you must:
- Implement the mapper yourself that works with a record type
- Instantiate the mapper (outside of the generated code)
- Provide the mapper instance as a named parameter to
initRdfMapper
Note that - unlike similar constructors like RdfIri.namedMapper - the
named mapper will not be registered globally, it will only be used
for the class annotated with @RdfGlobalResource.
Unlike the default constructor which generates a mapper, this requires you to
implement a mapper that works with a record of the property values from fields
marked with @RdfIriPart.
Important: When using custom mappers with multiple IRI part properties,
use @RdfIriPart.position(index) to specify the order of fields in the record:
@RdfGlobalResource(Product.classIri, IriStrategy.namedMapper('productIdMapper'))
class Product {
@RdfIriPart.position(1) // First field in the record
final String category;
@RdfIriPart.position(2) // Second field in the record
final String id;
// ...
}
// Implement mapper for the (String, String) record:
class ProductIdMapper implements IriTermMapper<(String, String)> {
@override
IriTerm toRdfTerm((String, String) record, SerializationContext context) {
final (category, id) = record;
return context.createIriTerm('https://example.org/products/$category/$id');
}
@override
(String, String) fromRdfTerm(IriTerm term, DeserializationContext context) {
final parts = term.value.split('/').takeLast(2).toList();
return (parts[0], parts[1]);
}
}
// In initialization code:
final productMapper = ProductIdMapper();
final rdfMapper = initRdfMapper(productIdMapper: productMapper);
The resource mapper will:
- During serialization: Extract the properties into a record to pass to your mapper
- During deserialization: Take the record your mapper produces and set the properties
The optional providedAs parameter allows this resource's IRI to be provided to dependent
mappers under the specified name, enabling hierarchical IRI patterns.
Implementation
const IriStrategy.namedMapper(String name, {this.providedAs})
: template = null,
fragmentTemplate = null,
super.namedMapper(name);