Typed entities

Version: 3.3

Typed entities allow easier access to commonly used properties or relations through declared properties that are mapped to the actual property value.

Out of the box the following typed entities are shipped by the SDK:

  • IAssetEntity
  • IFileEntity
  • IMailTemplateEntity
  • ISettingEntity
  • IUserEntity
  • IUserGroupEntity

Create a new typed entity type

All typed entity interfaces must inherit from the ITypedEntity interface. An implementation for the interface is not required as it will be generated and cached at runtime when first used.

Properties

Declared properties should use the types supported by Content Hub (string, bool, int, long, decimal, DateTime, DateTimeOffset, JToken, ...) to avoid exceptions when casting values.

The declared property name should match the property name that has been defined in the entity definition itself. If that is not possible due to the naming schema or naming conventions it is possible to override it using the TypedEntityPropertyAttribute.

If a property is supposed to be read-only it is legitimate to omit the setter.

Culture-sensitive properties can be declared using the generic CultureSensitive<T> type. The same rules about types and names apply to those properties as well.

RequestResponse
public interface ISampleEntity : ITypedEntity
{
    string StringProperty { get; set; }
    decimal DecimalProperty { get; set; }

    [TypedEntityProperty("Some.Different.Property.Name")]
    string StringPropertyNameOverridden { get; set; }

    string ReadonlyStringProperty { get; }

    string ReadonlyStringProperty { get; }

    CultureSensitive<string> CultureSensitiveString { get; }
}

Relations

Relations can be declared using the generic ToOneRelation<T> and ToManyRelation<T> types.

As for the properties, the declared property name for the relation should be equivalent to the name specified in the entity definition. If that is not possible the TypedEntityRelationAttribute allows to override the name here as well.

RequestResponse
public interface ISampleEntity : ITypedEntity
{
    ToOneRelation<IChildToOneParentRelation> TestToTest { get; }

    ToManyRelation<IChildToManyParentsRelation> TestToTests { get; }
}

Extend existing typed entity

In the event that you would like to extend an existing typed entity type, you can do so by simply inheriting from it and adding your own properties and relations to it as specified above.

RequestResponse
public interface ICustomAssetEntity : IAssetEntity
{
    string CustomProperty { get;set; }
}

Create an instance of a typed entity

Typed entities can be resolved using the TypedEntityFactory on the IMClient instance:

RequestResponse
var culture = new CultureInfo("en-US");

// Load asset from Content Hub instance 
var asset = await MClient.Entities.GetAsync(id);

// Turn received IEntity into typed IAssetEntity
var typedAsset = MClient.TypedEntityFactory.FromEntity<IAssetEntity>(asset);
typedAsset.Title = "New title";
typedAsset.Description[culture] = "English description";

await MClient.Entities.SaveAsync(typedAsset);

Do you have some feedback for us?

If you have suggestions for improving this article,