Loading...
Area: Optimizely Commerce
Applies to versions: 14 and higher

Object configuration

Recommended reading 
Note: This documentation is for the preview version of the upcoming release of CMS 12/Commerce 14/Search & Navigation 14. Features included here might not be complete, and might be changed before becoming available in the public release. This documentation is provided for evaluation purposes only.

This topic explains options for configuring business foundation objects. These provide a lightweight object relational mapping (ORM) where you can add custom fields based on your business requirements. The Customer subsystem uses business foundation objects for their storage mechanism.

Classes in this topic are in the following namespaces: 

  • Mediachase.BusinessFoundation.Data
  • Mediachase.BusinessFoundation.Data.Meta.Management
  • Mediachase.BusinessFoundation.Data.Modules
Note: You should not modify the default business objects unless you have a full understanding of how it works.

Working with metaclass objects

Creating objects

DataContext.Current = new DataContext(ServiceLocator.Current.GetInstance<IConnectionStringHandler>().Commerce.ConnectionString);
ModuleManager.InitializeActiveModules();
using (var scope = DataContext.Current.MetaModel.BeginEdit(MetaClassManagerEditScope.SystemOwner, AccessLevel.System))
{
    var manager = DataContext.Current.MetaModel;
    var changeTrackingManifest = ChangeTrackingManager.CreateModuleManifest();
    var recentReferenceManifest = RecentReferenceManager.CreateModuleManifest();
    var budgetClass = manager.CreateMetaClass("Budget", "Budget", "Budgets", "cls_Budget", PrimaryKeyIdValueType.Integer);
    ModuleManager.Activate(budgetClass, changeTrackingManifest);
    using (var builder = new MetaFieldBuilder(budgetClass))
    {
        builder.CreateDateTime("StartDate", "StartDate", true, false);
        builder.CreateDateTime("EndDate", "EndDate", true, false);
        builder.CreateCurrency("Amount", "Amount", true, 0, true);
        builder.CreateText("Currency", "Currency", true, 50, false);
        builder.CreateText("Status", "Status", true, 50, false);
        builder.CreateCurrency("SpentBudget", "SpentBudget", true, 0, true);
        builder.CreateText("PurchaserName", "PurchaserName", true, 50, false);
        builder.CreateCurrency("LockOrganizationAmount", "LockOrganizationAmount", true, 0, true);
        var contactReference = builder.CreateReference("Contact", "CreditCard_mf_Contact", true, "Contact", false);
        var orgReference = builder.CreateReference("Organization", "CreditCard_mf_Organization", true, "Organization", false);
        builder.SaveChanges();
    }

    budgetClass.AddPermissions();
    scope.SaveChanges();
}

Editing objects

DataContext.Current = new DataContext(ServiceLocator.Current.GetInstance<IConnectionStringHandler>().Commerce.ConnectionString);
ModuleManager.InitializeActiveModules();
using (var scope = DataContext.Current.MetaModel.BeginEdit(MetaClassManagerEditScope.SystemOwner, AccessLevel.System))
{
    var manager = DataContext.Current.MetaModel;
    var contactMetaClass = manager.MetaClasses[ContactEntity.ClassName];
    var changeTrackingManifest = ChangeTrackingManager.CreateModuleManifest();
    var recentReferenceManifest = RecentReferenceManager.CreateModuleManifest();
    using (var builder = new MetaFieldBuilder(contactMetaClass))
    {
        builder.CreateText("UserRole", "{Customer:UserRole}", true, 50, false);
        builder.CreateText("UserLocation", "{Customer:UserLocation}", true, 50, false);
        builder.CreateInteger("Points", "{Customer:Points}", true, 0);
        builder.CreateInteger("NumberOfOrders", "{Customer:NumberOfOrders}", true, 0);
        builder.CreateInteger("NumberOfReviews", "{Customer:NumberOfReviews}", true, 0);
        builder.CreateText("Tier", "{Customer:Tier}", true, 100, false);
        builder.CreateText("ElevatedRole", "{Customer:ElevatedRole}", true, 100, false);
        builder.CreateHtml("Bookmarks", "{Customer:Bookmarks}", true);
        builder.SaveChanges();
    }
    contactMetaClass.DeleteMetaField("MiddleName");
    scope.SaveChanges();
}

Deleting objects

DataContext.Current = new DataContext(ServiceLocator.Current.GetInstance<IConnectionStringHandler>().Commerce.ConnectionString);
ModuleManager.InitializeActiveModules();
using (var scope = DataContext.Current.MetaModel.BeginEdit(MetaClassManagerEditScope.SystemOwner, AccessLevel.System))
{
    var manager = DataContext.Current.MetaModel;
    manager.DeleteMetaClass("CreditCard");
    scope.SaveChanges();
}

Related topics

Do you find this information helpful? Please log in to provide feedback.

Last updated: Jul 02, 2021

Recommended reading