This content is archived. See latest version here

Last updated: Mar 31 2014

MetaClass represents a class containing acollection of meta fields. Fields represent information that an object contains. Fields are like variables because they can be read or set directly. For example, if you have an object named "Car" you could store its color in a field named "Color".

MetaClass has an optional TitleFieldName property which specifies the name of title field. Be careful to define TitleFieldName with a correct string field name if you want to create a reference to this meta class.

You can use meta class to create an entity object, and meta class fields (add, remove) can be modified at runtime.

When creating and deleting meta classes, remember that a meta model can only be modified in Design mode. Refer to the MetaClassManager Class section for more information.

Getting a MetaClass collection

The table collection is available from the MetaClassManager.MetaClasses property, and it will return a MetaClassCollection object.

Example: The following example writes to trace only user tables

C#
// Get MetaModel
MetaClassManager metaModel = DataContext.Current.MetaModel;
// Step 3. Enum Meta Classes
foreach (MetaClass mc in metaModel.MetaClasses)
{
Trace.WriteLine(mc.Name);
}

Deleting a meta class

Call the DeleteMetaClass method of the MetaClassManager class, passing the meta class object to delete a meta class. The method removes a meta class definition and all data, references and permission specifications for that meta class.

Example: Find meta class by name and delete

C#
// Open Meta model edit scope
using (MetaClassManagerEditScope scope =
DataContext.Current.MetaModel.BeginEdit())
{
// Find table
MetaClass mc = DataContext.Current.MetaModel.MetaClasses ["Class_1"];
// Drop table
DataContext.Current.MetaModel.DeleteMetaClass (table);
// Save Changes
scope.SaveChanges();
}

MetaClass Card extension

MetaClass can be extended with Cards. To do this, you create a Card field in the meta class, which is used to save the reference to the current card. Call the CreateCardField method of the MetaClass to create a card field. Each card can have its own field set, but only one card can be active in the entity object.

If a meta class has a card field, you can create a Card meta class. The Card meta class will be visible in a collection of meta classes like the original meta class, but it will have an IsCard property with the value "true". You can add meta fields or delete card meta classes, as well as common meta classes.

  • Call the CardOwner property of MetaClass to get the card owner for the current card.
  • Call the SupportsCards property of the MetaClass class to specify whether the meta class supports cards.
  • Call the GetMetaClassSupportedCard of the MetaClassManager class to get a collection of meta classes supporting cards.

To start work with the card, create an entity object passing the current card meta class name to the card field, and all card fields will be available in the current entity object. When you save the object it will save the original common fields, the current card type as well as the current card field. Card field are loaded automatically and will be available in the filter expressions.

As an example, we created a new meta class "Client", marked that meta class supports cards, and created three cards "Regional Client", "Foreign Client" and "Partner" with their own field sets. If we create a new client object without cards, only meta class fields are available but if you set the card property, the fields from the card will be available in the entity object automatically.

Card Meta Class
Validators

Meta class supports validation. The Validator is used to ensure that a meta class is configured properly. Validators are instantiated during runtime when you save the entity object. If a meta class is not properly configured, the validator will throw an exception for the incorrect values.

By default, meta fields will add validators to meta classes. For example, the e-mail field will add an e-mail validator to check that e-mail property has the correct e-mail, text field and add maximum length validators.

Call the Validators property of the MetaClass class to get a collection of validators for the current class.

You can create custom validators. To do this, create a class that implements the IValidator, and implement ErrorMessage, the IsValid property and the Validate method. Then you can add a validator to the collection of validators for the current class.

If you develop a custom meta type, you should add your validators in AssignValidators and remove them in the RemoveValidators methods of the meta field installer.

Example: CurrencyFieldValidator

C#
            using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Globalization;

namespace Mediachase.BusinessFoundation.Data
{
/// <summary>
/// Represents a currency field validator.
/// </summary>

public class CurrencyFieldValidator : BaseFieldValidator
{
private bool _allowNull = true;
public CurrencyFieldValidator()
{
}
public bool AllowNull
{
get { return _allowNull; }
set { _allowNull = value; }
}

protected override bool EvaluateIsValid()
{
object fieldValue = base.GetValue();
// Check Null
if (fieldValue == null && !this.AllowNull)
{

base.ErrorMessage = string.Format(CultureInfo.InvariantCulture, "The '{0}' field doesn't allow null.", this.FieldName);
return false;
}

if (fieldValue == null)
return true;


// Check Type
if (fieldValue.GetType() != typeof(Decimal))
{
base.ErrorMessage = string.Format(CultureInfo.InvariantCulture, "Wrong type '{1}'. '{0}' field expects decimal.", this.FieldName, fieldValue.GetType().GetType());
return false;
}
return true;
}
}
}

Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/