IriMapping.namedMapper constructor
const
IriMapping.namedMapper(
- String name
Creates a reference to a named mapper for this IRI term.
Use this constructor when you want to provide a custom IriTermMapper
implementation via dependency injection. When using this approach, you must:
- Implement the mapper yourself
- Instantiate the mapper (outside of the generated code)
- Provide the mapper instance as a named parameter to
initRdfMapper
The name will correspond to a parameter in the generated initRdfMapper function,
but the mapper will not be registered globally in the RdfMapper instance
but only used for the Resource Mapper whose property is annotated with this mapping.
This approach is particularly useful for IRIs that require complex logic or external context (like base URLs) that might vary between deployments.
Example:
class Book {
// Using a custom mapper for a UserReference object
@RdfProperty(
SchemaPerson.identifier,
iri: IriMapping.namedMapper('userReferenceMapper')
)
final UserReference userRef;
}
// You must implement the mapper:
class UserReferenceMapper implements IriTermMapper<UserReference> {
@override
IriTerm toRdfTerm(UserReference value, SerializationContext context) {
return context.createIriTerm('https://example.org/users/${value.username}');
}
@override
UserReference fromRdfTerm(IriTerm term, DeserializationContext context) {
final segments = Uri.parse(term.value).pathSegments;
return UserReference(segments.last);
}
}
// In initialization code:
final userRefMapper = UserReferenceMapper();
final rdfMapper = initRdfMapper(userReferenceMapper: userRefMapper);
Implementation
const IriMapping.namedMapper(String name)
: template = null,
fragmentTemplate = null,
super.namedMapper(name);