Try our conversational search powered by Generative AI!

Loading...
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

This section describes how to work with data through entity objects from Business Foundation (BF).

This is the recommended way when working with BF since it is a highly extensible and easy to use framework relying on the SQL API. See also Working with SQLRecords for other options on how to work with data.

All classes referred to here are available in the Mediachase.BusinessFoundation.Data.Business and Mediachase.BusinessFoundation.Data namespaces.

Business Foundation includes a request-response system to process entity object methods. You can create a request passing the target entity object and request parameters, and call the Execute method. BF will find the handler to process the request, call the handler and return a response with the execution result.

Entity objects

DataContext initalization

The DataContext class has the User and UserTimeZone properties. Entity objects and modules can use them to provide programmatic access to the current user ID and time zone. You should initialize the User and UserTimeZone properties before working with entity objects.

When you create DateTime or Date meta fields, you can pass the user time zone and DateTime values, which will be automatically converted to user time zone.

In an ASP.NET application, we recommend to initialize DataContext in the Global. Application_BeginRequest method.

EntityObject class

The EntityObject class represents a Meta class object. Meta class is an abstract representation of something, whereas an entity object is a usable example of the item the meta class represents.

An entity object can be typed or untyped. Use the EntityObject object and its properties to retrieve and update the entity object in the persistent storage. You cannot create a new EntityObject directly. Instead, use the BusinessManager class and request-response system for calling the Entity object methods.

BusinessManager class

The BusinessManager class represents methods for processing the request-response for entity objects. Call the BusinessManager static methods to initialize an entity object, and to create, update, delete, list and execute custom method for entity objects.

Example: Creation of a new entity object

C#
EntityObject page = BusinessManager.InitializeEntity("Class_1");
        page["Title"] = "Title Text";
        page["Description"] = "Description Text";
        PrimaryKeyId pageId = BusinessManager.Create(page);

Request-response system

Use the request-response system to process entity object methods.

Example: Load entity object

C#
LoadRequest request = new LoadRequest(new EntityObject(metaClassName, primaryKeyId));
        LoadResponse response = (LoadResponse)BusinessManager.Execute(request);
        EntityObject entityObject = response.EntityObject;

You can override the default method handler, add custom plug-ins or define a custom method.

Pipeline events

When an instance of a request is created and you call the BusinessManager.Execute method, BusinessManager creates a pipeline and the following events are executed by the BusinessManager class while the request is processed:

  1. Call the PreExecute method of the appropriate IRequestHandler class for the request.
  2. Call the Execute method of the appropriate IPlugin classes that are subscribed to from the PreMainOperation event.
  3. Open Transaction.
  4. Call the PreExecuteInsideTransaction method of the appropriate IRequestHandler class for the request.
  5. Call the Execute method of the appropriate IPlugin classes that are subscribed to from the PreMainOperation (In-Tranasaction) event.
  6. Call the Execute method of the appropriate IRequestHandler class for the request.
  7. Call the PostExecuteInsideTransaction method of the appropriate IRequestHandler class for the request.
  8. Call the Execute method of the appropriate IPlugin classes that are subscribed to from the PostMainOperation (In-Tranasaction) event.
  9. Commit the Transaction.
  10. Call the PostExecute method of the appropriate IRequestHandler class for the request.
  11. Call the Execute method of the appropriate IPlugin classes that are subscribed to from the PostMainOperation event.

Entity objects

Handlers

A request handler is the process (frequently referred to as the "endpoint") that runs in response to a request made to a business manager. The most common handler is a default entity object handler that processes default methods. When users execute methods, the request is processed by the business manager through the handler. You can create your own handler that executes custom methods.

Creating handlers

To create a custom handler, you create a class that implements the IRequestHandler interface. You should also implement the PreExecute, PreExecuteInsideTransaction, Execute, PostExecuteInsideTransaction, PostExecute methods.

BF has BaseRequestHandler helper class that you can use as a base class for a custom handler, and which implements the IRequestHandler together with virtual methods.

Registering handlers

After you have created a custom handler class, it must be registered in the Application config file, enabling the application to call the handler. In the config file of the application, create a mediachase.businessFoundation.data/businessManager section.

The following example shows how to register a handler that responds to requests for the Test method for Class_1 meta class. The handler is defined as the class SampleHandler in the assembly SampleHandlerAssembly.

Example: Register a plugin

XML
<businessManager>
            <plugins>
            <add metaClass="*" method="Update;Create" eventStage="PreMainOperationInsideTranasaction" type="SampleHandler, SampleHandlerAssembly" />
            </plugins>
            </businessManager>
Method attributes supports a search mask with * and ? chars and multiple methods separated by semicolons. eventStage supports several event stage items separated by |.

Default methods

Business Foundation implements several methods:

  • Initialize creates a new entity object
  • Load loads entity object by primary key
  • Create creates new entity object
  • Update updates entity object
  • Delete deletes entity object
  • List load collection of entity objects by filter

These methods can all be called and used to simplify method execution.

Initialize

Example: Call the Initialize method to create a new instance of entity object.

C#
InitializeEntityResponse response = (InitializeEntityResponse)BusinessManager.Execute(new InitializeEntityRequest(metaClassName));
            EntityObject obj = response.EntityObject;

Load

Example: Call the Load method to load an instance of entity object by primary key.

C#
LoadResponse response = (LoadResponse)BusinessManager.Execute(new LoadRequest(new EntityObject(metaClassName, primaryKeyId)));
            EntityObject obj = response.EntityObject;

Create

Example: Call the Create method to store an entity object in the persistent storage.

C#
CreateResponse response = (CreateResponse)BusinessManager.Execute(new CreateRequest(target));
            PrimaryKeyId newPk = response.PrimaryKeyId;

Update

Example: Call the Update method to update an entity object in the persistent storage.

C#
BusinessManager.Execute(new UpdateRequest(target));

Delete

Example: Call the Delete method to delete an entity object from the persistent storage.

C#
BusinessManager.Execute(new DeleteRequest(target));

List

Example: Call the List method to select a collection of entity objects from the persistent storage.

C#
ListResponse response = (ListResponse)BusinessManager.Execute(new ListRequest(metaClassName, filters, sorting));
            EntityObject[] objs = response.EntityObjects;

Custom methods

To create a custom method, start by creating and registering a custom handler and define a custom request and response.

All requests should be inherited from the Request class, and all responses should be inherited from the Response class.

Filtration and sorting

Call the List method to return an array of entity objects from the meta class. Additional information about FilterElement and SortingElement can be found in the Filtering and Sorting section.

Typed entity objects

Entity objects that are used in your applications can be either typed or untyped. However, BF has other tools to support typed entity objects, with which programming is easier and less error-prone.

Do the following to use typed entity objects:

  1. Run McCodeGen to create typed entity object C# class.
  2. Register a new typed object as primary type for meta class.

McCodeGen.exe

The McCodeGen application reads the mcgen file, extracts the meta model, and generates a template and an output text file.

McCodeGen.exe-mcgen:mcgenFile-out:outputFile

ParameterDescription

mcgenFile

The meta model file

outputFile

The output file

Using mcgen file to create a typed entity object

The McCodeGen application can generate C# classes from the mcgen file. A typed entity object class is very similar to the untyped class EntityObject, but includes properties mapped to column values and column name constant string.

The Mcgen file should include this:

XML
<?xml version="1.0" encoding="utf-8" ?>
            <mcgen xmlns="mediachase.codegen.config" extension="cs" template="Templates\BusinessFoundationEntityObject.aspx">
            <EntityObject>
            <ConnectionString> Data Source=(local);Initial Catalog=TestDB;User ID=sa;Password=; </ConnectionString>
            <MetaClass>Book</MetaClass>
            <Params>
            <add key="Namespace" value="Test.MetaClass"/>
            </Params>
            </EntityObject>
            </mcgen>
ParameterDescription

ConnectionString

The connection string

MetaClass

The name of meta class

Namespace

The typed row C# class namespace

Paste your connection string and meta class name, declare an output class namespace, then execute McCodeGen from the command line.

McCodeGen.exe -mcgen:BookEntity.mcgen -out:BookEntity.cs

Then you can add a typed entity object class to your .NET project.

Typed entity object registration

After you have created a typed entity object, it must be registered in the application. Create a new handler based on EntityObjectDefaultRequestHandler and override the CreateEntityObject method, which will create and return a typed entity object. After you have created a handler, you should register it.

BusinessFoundation allows for executing entity object methods through Web Service.

Visual Studio integration

McCodeGen should be installed in order for the VSIntegration to work.

The McCodeGen application can be integrated with Visual Studio by adding themcgen file to aVisual Studio Project.

SQL records

Check the properties for your mcgen file (in this case a CategoryRow.mcgen), the "Custom Tool" field should be blank. Enter the name "McCodeGenerator".

SQL records

The tool should then run automatically. Check to make sure that the output was created by opening the Solution Explorer.

SQL records

MetaObject class

The MetaObject class allows low-level access to entity object persistent storage. The default handler uses MetaObject to load and save entity objects to a persistent storage. You can use MetaObject to call, get, update or delete low-level methods.

MetaObject extensions

The MetaObject extension allows for capturing meta object events, and the MetaObject extension is registered in the meta class.

To create a custom MetaObject extension, you first create a class that implements the IMetaObjectExtension interface. You should also implement the Init, PreSave, Save, PreDelete, Delete and Clone methods. Use the BaseMetaObjectExtension class as a base class for your extension.

After you have created a custom extension class, you must register it in the MetaClass. You should add the MetaObjectExtensionInfo object to the MetaClass.Extensions collection, passing extension name, extension type and activation type. This enables the extension to receive metaobject events.

Example: Add metaobject extension

C#
// Add MetaObject Extension
            metaClass.Extensions.Add(new MetaObjectExtensionInfo(HistoryManager.ModuleName, AssemblyUtil.GetTypeString(typeof
            (HistoryExtension)), MetaObjectExtensionActivationType.OnSave));
Remember that a meta model can only be modified in Design mode. Refer to the MetaClassManager Class section for more information.

Extension supports several activation types:

  • ByRequest (default) - the extension will be loaded on any meta object event.
  • OnSave - the extension will be loaded on save event.
  • OnLoad - the extension will be loaded on load meta object event.

The use of activation type makes it possible to optimize application performance.

Triggers

A trigger is procedural code that is automatically executed in response to certain events on a particular meta object in a persistent storage. The trigger is mostly used for keeping the integrity of the information on the persistent storage. Lets say for example that we have an "employee" meta class. When a new meta object is added to the employees meta class, new meta object should be created also in the meta class of the taxes, vacations, and salaries.

  • Call AddTrigger of the TriggerManager class to add a custom trigger.
  • Call RemoveTrigger of theTriggerManager class to remove a custom trigger.

A trigger can be registered on create, update and delete events for the meta object.

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

Last updated: Mar 31, 2014

Recommended reading