Interface IContentTypeModelAssigner

Assign values to a ContentTypeModel.

Namespace: EPiServer.DataAbstraction.RuntimeModel
Assembly: EPiServer.dll
Version: 9.12.2
Syntax
public interface IContentTypeModelAssigner
Examples

The following code example demonstrate how to use ContentDataAttributeScanningAssigner to Assign values from attributes.

using System.Reflection;
using EPiServer.DataAbstraction.RuntimeModel;
using EPiServer.DataAnnotations;

/// <summary>
/// Custom content type model assigner, which sets properties using attributes.
/// </summary>
public class CustomContentTypeModelAssignerSample : IContentTypeModelAssigner
{
/// <summary>
/// Check if the <paramref name="contentTypeModel"/> has a model type with a <see cref="EPiServer.DataAnnotations.ContentTypeAttribute"/>.
/// If so, some properties on <paramref name="contentTypeModel"/> will be set from the attribute.
/// </summary>
/// <param name="contentTypeModel">The content type model, which will be populated with values.</param>
public void AssignValues(ContentTypeModel contentTypeModel)
{
    object[] attributes = contentTypeModel.ModelType.GetCustomAttributes(typeof(ContentTypeAttribute), false);
    if (attributes.Length == 0)
    {
        return;
    }

    ContentTypeAttribute contentTypeAttribute = attributes[0] as ContentTypeAttribute;
    if (contentTypeAttribute != null)
    {
        contentTypeModel.DisplayName = contentTypeAttribute.DisplayName;
        contentTypeModel.Description = contentTypeAttribute.Description;
        contentTypeModel.Order = contentTypeAttribute.GetOrder();
        contentTypeModel.Guid = contentTypeAttribute.GetGUID();
    }
}

/// <summary>
/// Check if the <paramref name="property"/> uses the <see cref="EPiServer.DataAnnotations.CultureSpecificAttribute"/>.
/// If so, a property on <paramref name="propertyDefinitionModel"/> will be set from the attribute.
/// </summary>
/// <param name="propertyDefinitionModel">The property definition model, which will be populated with values.</param>
/// <param name="property">The property to use when scanning for attributes.</param>
/// <param name="parentModel">The content type model, in which the property contains.</param>
public void AssignValuesToPropertyDefinition(PropertyDefinitionModel propertyDefinitionModel, PropertyInfo property, ContentTypeModel parentModel)
{
    object[] attributes = property.GetCustomAttributes(typeof(CultureSpecificAttribute), true);
    if (attributes.Length == 0)
    {
        return;
    }

    CultureSpecificAttribute cultureSpecificAttribute = attributes[0] as CultureSpecificAttribute;
    if (cultureSpecificAttribute != null)
    {
        propertyDefinitionModel.CultureSpecific = cultureSpecificAttribute.IsCultureSpecific;
    }
}
}

Methods

AssignValues(ContentTypeModel)

Assigns values to contentTypeModel.

Declaration
void AssignValues(ContentTypeModel contentTypeModel)
Parameters
Type Name Description
ContentTypeModel contentTypeModel

The content type model, which will be populated with values.

Examples

The following code example demonstrate how to use ContentDataAttributeScanningAssigner to Assign values from attributes.

using System.Reflection;
using EPiServer.DataAbstraction.RuntimeModel;
using EPiServer.DataAnnotations;

/// <summary>
/// Custom content type model assigner, which sets properties using attributes.
/// </summary>
public class CustomContentTypeModelAssignerSample : IContentTypeModelAssigner
{
/// <summary>
/// Check if the <paramref name="contentTypeModel"/> has a model type with a <see cref="EPiServer.DataAnnotations.ContentTypeAttribute"/>.
/// If so, some properties on <paramref name="contentTypeModel"/> will be set from the attribute.
/// </summary>
/// <param name="contentTypeModel">The content type model, which will be populated with values.</param>
public void AssignValues(ContentTypeModel contentTypeModel)
{
    object[] attributes = contentTypeModel.ModelType.GetCustomAttributes(typeof(ContentTypeAttribute), false);
    if (attributes.Length == 0)
    {
        return;
    }

    ContentTypeAttribute contentTypeAttribute = attributes[0] as ContentTypeAttribute;
    if (contentTypeAttribute != null)
    {
        contentTypeModel.DisplayName = contentTypeAttribute.DisplayName;
        contentTypeModel.Description = contentTypeAttribute.Description;
        contentTypeModel.Order = contentTypeAttribute.GetOrder();
        contentTypeModel.Guid = contentTypeAttribute.GetGUID();
    }
}

/// <summary>
/// Check if the <paramref name="property"/> uses the <see cref="EPiServer.DataAnnotations.CultureSpecificAttribute"/>.
/// If so, a property on <paramref name="propertyDefinitionModel"/> will be set from the attribute.
/// </summary>
/// <param name="propertyDefinitionModel">The property definition model, which will be populated with values.</param>
/// <param name="property">The property to use when scanning for attributes.</param>
/// <param name="parentModel">The content type model, in which the property contains.</param>
public void AssignValuesToPropertyDefinition(PropertyDefinitionModel propertyDefinitionModel, PropertyInfo property, ContentTypeModel parentModel)
{
    object[] attributes = property.GetCustomAttributes(typeof(CultureSpecificAttribute), true);
    if (attributes.Length == 0)
    {
        return;
    }

    CultureSpecificAttribute cultureSpecificAttribute = attributes[0] as CultureSpecificAttribute;
    if (cultureSpecificAttribute != null)
    {
        propertyDefinitionModel.CultureSpecific = cultureSpecificAttribute.IsCultureSpecific;
    }
}
}

AssignValuesToPropertyDefinition(PropertyDefinitionModel, PropertyInfo, ContentTypeModel)

Assigns values to propertyDefinitionModel from attributes defined on property

Declaration
void AssignValuesToPropertyDefinition(PropertyDefinitionModel propertyDefinitionModel, PropertyInfo property, ContentTypeModel parentModel)
Parameters
Type Name Description
PropertyDefinitionModel propertyDefinitionModel

The property definition model, which will be populated with values.

System.Reflection.PropertyInfo property

The property to use when scanning for attributes.

ContentTypeModel parentModel

The content type model, in which the property contains.

Examples

The following code example demonstrate how to use ContentDataAttributeScanningAssigner to Assign values from attributes.

using System.Reflection;
using EPiServer.DataAbstraction.RuntimeModel;
using EPiServer.DataAnnotations;

/// <summary>
/// Custom content type model assigner, which sets properties using attributes.
/// </summary>
public class CustomContentTypeModelAssignerSample : IContentTypeModelAssigner
{
/// <summary>
/// Check if the <paramref name="contentTypeModel"/> has a model type with a <see cref="EPiServer.DataAnnotations.ContentTypeAttribute"/>.
/// If so, some properties on <paramref name="contentTypeModel"/> will be set from the attribute.
/// </summary>
/// <param name="contentTypeModel">The content type model, which will be populated with values.</param>
public void AssignValues(ContentTypeModel contentTypeModel)
{
    object[] attributes = contentTypeModel.ModelType.GetCustomAttributes(typeof(ContentTypeAttribute), false);
    if (attributes.Length == 0)
    {
        return;
    }

    ContentTypeAttribute contentTypeAttribute = attributes[0] as ContentTypeAttribute;
    if (contentTypeAttribute != null)
    {
        contentTypeModel.DisplayName = contentTypeAttribute.DisplayName;
        contentTypeModel.Description = contentTypeAttribute.Description;
        contentTypeModel.Order = contentTypeAttribute.GetOrder();
        contentTypeModel.Guid = contentTypeAttribute.GetGUID();
    }
}

/// <summary>
/// Check if the <paramref name="property"/> uses the <see cref="EPiServer.DataAnnotations.CultureSpecificAttribute"/>.
/// If so, a property on <paramref name="propertyDefinitionModel"/> will be set from the attribute.
/// </summary>
/// <param name="propertyDefinitionModel">The property definition model, which will be populated with values.</param>
/// <param name="property">The property to use when scanning for attributes.</param>
/// <param name="parentModel">The content type model, in which the property contains.</param>
public void AssignValuesToPropertyDefinition(PropertyDefinitionModel propertyDefinitionModel, PropertyInfo property, ContentTypeModel parentModel)
{
    object[] attributes = property.GetCustomAttributes(typeof(CultureSpecificAttribute), true);
    if (attributes.Length == 0)
    {
        return;
    }

    CultureSpecificAttribute cultureSpecificAttribute = attributes[0] as CultureSpecificAttribute;
    if (cultureSpecificAttribute != null)
    {
        propertyDefinitionModel.CultureSpecific = cultureSpecificAttribute.IsCultureSpecific;
    }
}
}

Extension Methods

See Also