Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
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".
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.
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
// Get MetaModel
MetaClassManager metaModel = DataContext.Current.MetaModel;
// Step 3. Enum Meta Classes
foreach (MetaClass mc in metaModel.MetaClasses)
{
Trace.WriteLine(mc.Name);
}
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
// 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 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.
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.
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.
Example: CurrencyFieldValidator
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;
}
}
}
Last updated: Oct 21, 2014