/Orc.Metadata.Model

Orc.Metadata abstraction layer for models meta-description.

Primary LanguageC#MIT LicenseMIT

Orc.Metadata.Model

Orc.Metadata abstraction layer for models meta-description.
This project only defines the interfaces and convenience base (abstract) classes, and giving account to its purpose is thus left to their implementation.

Usage examples

For purpose of illustration, we will assume the existence of a fictive Orc.Metadata.Model.Reflection implementation (feel free to write the actual implementation).

This implementation uses reflection to provide metadata on model objects, and offers several parameters to customize the result.

Using an implementation

The reflection implementation offers the following instantiation pathway and configurations :

var modelMetadataProvider = new ReflectionModelMetadataProvider();

// Tailor resulting metadatas to our need
modelMetadataProvider.ConfigureWith(modelConfig => modelConfig
    .IncludePublicProperties(true)
    .IncludePrivateProperties(true)
    .IncludePublicFields(false)
    .IncludePrivateFields(false)
    .AddMetadataCollection(myCustomModelMetadatas) // #1
    .ConfigurePropertiesWith(propertiesConfig => propertiesConfig
        .AddMetadataCollection(myCustomModelPropertiesMetadatas)) // #2
);

// Get a bundle of both our model instance, and metadatas which can be applied to it.
var modelWithMetadata = await provider.GetModelMetadataAsync(modelInstance);

#1, #2: Optionally, you can aggregate other metadatas, which will be merged depending on the main provider implementation.

Now, metadatas may be used as such :

// Get a list of available properties (public & private properties here)
ICollection<IModelPropertyDescriptor> propDescriptors =
    modelWithMetadata.GetMetadataValue(ModelMetadataTypes.PropertyDescriptors);

// Or get a property with its metadatas, directly by name
var propWithMetadatas =
    modelWithMetadata.GetPropertyObjectWithMetadataByName("MyPublicProperty");

// Get its value
var propValue = propWithMetadatas.GetMetadataValue(ModelPropertyMetadataTypes.Value);

// Assuming 'myCustomModelPropertiesMetadatas' contains a metadata named 'Operators'
var propAvailableOperators = propWithMetadatas.GetMetadataValue("Operators");

ModelMetadataTypes and ModelPropertyMetadataTypes provide common metadatas name.
Implementation of these metadatas is dependant on the provider.

Building an implementation

A simple sample may be found in Orc.Metadata.Model.Catel.Shared. Steps are as follow:

Using an unknown implementation (e.g. in a library)

Example using Catel ServiceLocator

Defining constraints

Tech talk - interfaces & types explained

IModelMetadataCollection

IModelPropertyMetadataCollection

Solution diagram

Solution diagram