HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

MetaClass class

Represents a class containing a collection 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 can store its color in a field named Color.

📘

Note

MetaClass has an optional TitleFieldName property that 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 modify meta-class fields (add, remove) at runtime. When you create and delete meta-classes, remember that you can modify a meta-model only in Design mode. See the MetaClassManager class section.

Get a MetaClass collection

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

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);
      }

Delete 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 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 Card extension

You can extend MetaClass with Cards. To do this, you create a Card field in the meta-class, saving the current card's reference. Call the CreateCardField method of the MetaClass to create a Card field. Each card can have its own field set, but only one can be active in the entity object.

You can create a Card meta-class if a meta-class has a card field. The Card meta-class is visible in a collection of meta-classes like the original meta-class but will have an IsCard property with the value true. You can add meta-fields or delete card meta-classes and 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 are available in the current entity object. When you save the object, it saves the original common fields, the current card type, and the current card field. Card fields are loaded automatically and are available in the filter expressions.

The following example creates a Client meta-class, marks that meta-class supports cards, and creates three cards with field sets: Regional Client, Foreign Client, and Partner. If you 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 are available in the entity object automatically.

Validators

Meta-class supports validation. The Validator ensures 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 throws an exception for the incorrect values.

By default, meta-fields add validators to meta-classes. For example, the email field adds an email validator to check that email property has the correct email, 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 by creating a class that implements the IValidator, and implements 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

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;
              }
          }
      }