This project is read-only.

NDatabase Triggers


To add a trigger, the first step is to build a class that implements one of the trigger interface.

A simple base class for all triggers

public abstract class Trigger
{
    public virtual IOdb Odb { get; set; }
}

Insert Trigger


public abstract class InsertTrigger : Trigger
{
    public abstract bool BeforeInsert(object @object);
    public abstract void AfterInsert(object @object, OID oid);
}

Update Trigger


public abstract class UpdateTrigger : Trigger
{
    public abstract bool BeforeUpdate(IObjectRepresentation oldObjectRepresentation,
                                        object newObject, OID oid);
    public abstract void AfterUpdate(IObjectRepresentation oldObjectRepresentation,
                                        object newObject, OID oid);
}
Update trigger contains IObjectRepresentation item which is used to give the user an instance of an object representation, level2. The Object Representation encapsulates the NonNativeObjectInfo which is the internal object representation.

public interface IObjectRepresentation
{
    /// <summary>
    ///   Retrieves the oid of the object
    /// </summary>
    OID GetOid();

    /// <summary>
    ///   Retrieves the full object class name
    /// </summary>
    string GetObjectClassName();

    /// <summary>
    ///   Return the value of a specific attribute
    /// </summary>
    object GetValueOf(string attributeName);

    /// <summary>
    ///   Sets the value of a specific attribute
    /// </summary>
    void SetValueOf(string attributeName, object value);
}

Delete Trigger


public abstract class DeleteTrigger : Trigger
{
    public abstract bool BeforeDelete(object @object, OID oid);
    public abstract void AfterDelete(object @object, OID oid);
}

Select Trigger


public abstract class SelectTrigger : Trigger
{
    public abstract void AfterSelect(object @object, OID oid);
}

Insert Trigger Example


public class MyTrigger : InsertTrigger
{
    public override bool BeforeInsert(object obj)
    {
        Console.WriteLine("before inserting {0}", obj);
        ((Mage) obj).Attack += 1.1;
        Console.WriteLine("before inserting (after change) {0}", obj);

        return true;
    }

    public override void AfterInsert(object obj, OID oid)
    {
        Console.WriteLine("after insert object with id {0}({1})", oid,
                obj.GetType().Name);
    }
}
Then you need to register the trigger by NDatabase API:

var mage = new Mage("Merlin", 3.3, 3.4);
var myTrigger = new MyTrigger();

using (var odb = OdbFactory.Open("inserting_trigger.db"))
{
    odb.TriggerManagerFor<Mage>().AddInsertTrigger(myTrigger);
    odb.Store(mage);
}

using (var odb = OdbFactory.Open("inserting_trigger.db"))
{
    var merlin = odb.Query<Mage>().Execute<Mage>().GetFirst();

    // code which is using merlin Mage instance
}
The output from example:

before inserting [Mage] Name: Merlin, Attack: 3,3, Defense: 3,4
before inserting (after change) [Mage] Name: Merlin, Attack: 4,4, Defense: 3,4
after insert object with id 2(Mage)

Last edited Dec 28, 2012 at 1:21 PM by jacek, version 10

Comments

No comments yet.