Ninja Database Pro
Triggers Property
Example 



KellermanSoftware.NinjaDatabasePro Namespace > NinjaDbPro Class : Triggers Property
A list of actions to perform for a type when inserting, updating, or deleting. To create a trigger, create a class that implements the ITrigger interface.
Syntax
Public ReadOnly Property Triggers As List(Of ITrigger)
Dim instance As NinjaDbPro
Dim value As List(Of ITrigger)
 
value = instance.Triggers
public List<ITrigger> Triggers {get;}
public:
property List<ITrigger^>^ Triggers {
   List<ITrigger^>^ get();
}
Example
public class BeforeUpdateTrigger : ITrigger
{
    public TriggerAction TriggerAction
    {
        get { return TriggerAction.BeforeUpdate; }
    }
 
    public Type TriggerType
    {
        get { return typeof(Person); }
    }
 
    public void ExecuteTrigger(Type type, object instance)
    {
        Person person = (Person)instance;
 
        if (person.Name == "Secret Agent Charlie")
        {
            person.Name = "Charlie Smith";
            Console.WriteLine("Name Replaced");
        }
    }
}
 
public class AfterInsertTrigger : ITrigger
{
    public TriggerAction TriggerAction
    {
        get { return TriggerAction.AfterInsert; }
    }
 
    public Type TriggerType
    {
        get { return typeof(Person); }
    }
 
    public void ExecuteTrigger(Type type, object instance)
    {
        Person person = (Person) instance;
        Console.WriteLine("Person was inserted with an ID of {0}",person.PersonId);
    }
}
 
[Test]
public void TriggerUsageTest()
{
    NinjaDbPro db = new NinjaDbPro("MyDatabaseDirectory", "MyDatabaseName");
    db.OpenDatabase();
 
    db.Triggers.Add(new AfterInsertTrigger());
    db.Triggers.Add(new BeforeUpdateTrigger());
 
    Person person = new Person();
    person.Name = "John Smith";
 
    //The Insert Trigger Will Be Fired Here
    db.Save(person);
 
    //The Before Update Trigger Will Be Fired Here
    person.Name = "Secret Agent Charlie";
    db.Save(person);
 
    Console.WriteLine("Person Name is now: {0}", person.Name);
    db.CloseDatabase();
}
Public Class BeforeUpdateTrigger
    Implements ITrigger
    Public ReadOnly Property TriggerAction() As TriggerAction
        Get
            Return TriggerAction.BeforeUpdate
        End Get
    End Property
 
    Public ReadOnly Property TriggerType() As Type
        Get
            Return GetType(Person)
        End Get
    End Property
 
    Public Sub ExecuteTrigger(ByVal type As Type, ByVal instance As Object)
        Dim person As Person = CType(instance, Person)
 
        If person.Name = "Secret Agent Charlie" Then
            person.Name = "Charlie Smith"
            Console.WriteLine("Name Replaced")
        End If
    End Sub
End Class
 
Public Class AfterInsertTrigger
    Implements ITrigger
    Public ReadOnly Property TriggerAction() As TriggerAction
        Get
            Return TriggerAction.AfterInsert
        End Get
    End Property
 
    Public ReadOnly Property TriggerType() As Type
        Get
            Return GetType(Person)
        End Get
    End Property
 
    Public Sub ExecuteTrigger(ByVal type As Type, ByVal instance As Object)
        Dim person As Person = CType(instance, Person)
        Console.WriteLine("Person was inserted with an ID of {0}",person.PersonId)
    End Sub
End Class
 
<Test>
Public Sub TriggerUsageTest()
    Dim db As New NinjaDbPro("MyDatabaseDirectory", "MyDatabaseName")
    db.OpenDatabase()
 
    db.Triggers.Add(New AfterInsertTrigger())
    db.Triggers.Add(New BeforeUpdateTrigger())
 
    Dim person As New Person()
    person.Name = "John Smith"
 
    'The Insert Trigger Will Be Fired Here
    db.Save(person)
 
    'The Before Update Trigger Will Be Fired Here
    person.Name = "Secret Agent Charlie"
    db.Save(person)
 
    Console.WriteLine("Person Name is now: {0}", person.Name)
    db.CloseDatabase()
End Sub
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

NinjaDbPro Class
NinjaDbPro Members