Object Event Scripts

MineMarket object event scripts are used to handle custom actions specific to a customer that are not handled in core MineMarket. For example, a requirement might be to stop data being saved to the database if a certain value has not been set on an object.

It is essential to understand CDAL and the MineMarket object model for object event scripts. See CDAL Data Access Layer.

Formal Definition

Each MineMarket object type has a separate object event script. The following example is for analyte definitions:

Copy
using System;
using Mincom.MineMarket.DAL;
using Mincom.MineMarket;

/// <summary>
/// Exposes events for business objects
/// </summary>

public class AnalyteDefEvents : IObjectEvents
{
    public AnalyteDefEvents() { }

    public void ObjectPropertyChange(IEventsObject obj, 
        ObjectPropertyChangeEventArgs e)
    {
    }

    public void ObjectBeforeSave(IEventsObject obj, 
        ObjectBeforeSaveEventArgs e)
    {
    }

    public void ObjectBeforeDelete(IEventsObject obj, 
        ObjectBeforeDeleteEventArgs e)
    {
    }
}

Coding Standards

The events exposed by the interface provide an opportunity to perform custom actions:

  • When any properties of an object are changed
  • Before saving any changes to objects, including newly created objects
  • Before saving deletions

Important: In these events, do not add code like the following that would cause another event to fire by making an object Dirty. See MineMarket Object States.

Copy
despatchOrder.PropertyChanged("XYZ", "XYZ");

If you have to make the object Dirty, perform the required operations on that object within the current object event script. Otherwise you can cause an "An object has been edited by another user" error and an undesired ‘cascading’ of scripts that execute unwanted code.

Method Descriptions

ObjectPropertyChange

This event fires when an object property of the event type has been changed in MineMarket. The obj parameter is the object that has changed and must be cast to that object type. For example:

Copy
AnalyteDef analyteDef = (AnalyteDef)obj;

The e parameter provides the following details:

Copy
public sealed class ObjectPropertyChangeEventArgs
{
    public PropertyChangeHint Hint;
    public object NewValue;
    public object OldValue;
    public string PropertyName;
    public ObjectPropertyChangeEventArgs(string propertyName, object oldValue, 
        object newValue, PropertyChangeHint hint);
}

ObjectBeforeSave

This event fires before an object of the event type is about to be saved. Saving of data can be cancelled in this event if required. For example:

Copy
AnalyteDef analyteDef = (AnalyteDef)obj;
if (analyteDef.Alias1.Equals("H2O", StringComparison.OrdinalIgnoreCase))
{
    e.Info.AddError(analyteDef, "Cannot make changes to Moisture");
}
else
{
    //...
}

ObjectBeforeDelete

This event fires before an object of the event type is about to be deleted from the database. Saving of data can be cancelled in this event if required. The state of the object will be Deleting and collections on the object can be cleared, making this event potentially more difficult to code. For example:

Copy
AnalyteDef analyteDef = (AnalyteDef)obj;
if (analyteDef.Alias1.Equals("Cu", StringComparison.OrdinalIgnoreCase))
{
    e.Info.AddError(analyteDef, "Copper cannot be deleted");
}

Tips

The MineMarket ‘Intellisense’ script editor (Object Events (script editor)) does not have the features of Visual Studio. Copying the script into a Visual Studio project that references BulkTrak.Component.dll and cdal.dll will make developing the script easier.

The Object Events screen includes the InvoiceDataXMLScript object. Do not use this object event because it is ignored. To modify the invoice data XML, see Invoice Data XML Scripts.

64-bit Compatibility

Since the switch to 64-bit operating systems, some DLLs are not in the location that MineMarket expects and therefore are not available in the list of references. For example, System.Windows.Forms contains code to display message boxes, which is quite common in object events; however, on a 64-bit machine this DLL may not display in the list and the object event cannot use this assembly.

The following SQL script can be used to add the DLL to the list but caution must be exercised when doing so because it is a ‘backdoor’ fix and is not supported by R&D. In MineMarket the object event script must be set to ‘Use Script’ and be saved to the database before this SQL script is run, or else the update script will have no effect. Replace NAME_OF_OBJECT_EVENT_SCRIPT with the name of the object event script.

Copy
update CustomScript
set BTReferences = convert(ntext, (convert(nvarchar(200), BTReferences)  +  '|System.Windows.Forms.dll'))
where Name like 'NAME_OF_OBJECT_EVENT_SCRIPT %'