Let's look at an example of committing
event usage. In this example committing
callback will be used to
ensure that only unique objects can be saved to the database. Duplicate objects
- objects, which have the same fields - will cause an exception at the commit
time.
We will use a simple Item class for an example:
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02
namespace Db4objects.Db4odoc.CommitCallbacks 03
{ 04
public class Item 05
{ 06
private int _number; 07
private string _word; 08
09
public Item(int number, string word) 10
{ 11
_number = number; 12
_word = word; 13
} 14
15
public string Word 16
{ 17
get 18
{ 19
return _word; 20
} 21
} 22
23
public int Number 24
{ 25
get 26
{ 27
return _number; 28
} 29
} 30
31
public override string ToString() 32
{ 33
return _number + "/" + _word; 34
} 35
} 36
}
01' Copyright (C) 2007 db4objects Inc. http://www.db4o.com 02
Namespace Db4objects.Db4odoc.CommitCallbacks 03
04
Public Class Item 05
Private _number As Integer 06
Private _word As String 07
08
Public Sub New(ByVal number As Integer, ByVal word As String) 09
_number = number 10
_word = word 11
End Sub 12
13
Public ReadOnly Property Word() As String 14
Get 15
Return _word 16
End Get 17
End Property 18
19
Public ReadOnly Property Number() As Integer 20
Get 21
Return _number 22
End Get 23
End Property 24
25
Public Overloads Overrides Function ToString() As String 26
Return _number.ToString() + "/" + _word 27
End Function 28
End Class 29
End Namespace
The following methods will configure a commit-time callback method for uniqueness check:
01private static void Configure() 02
{ 03
IEventRegistry registry = EventRegistryFactory.ForObjectContainer(Container()); 04
// register an event handler, which will check object uniqueness on commit 05
registry.Committing += new CommitEventHandler(delegate(object sender, CommitEventArgs args) 06
{ 07
// uniqueness should be checked for both added and updated objects 08
CheckUniqueness(args.Added); 09
CheckUniqueness(args.Updated); 10
}); 11
}
01private static void CheckUniqueness(IObjectInfoCollection collection) 02
{ 03
foreach (IObjectInfo info in collection) 04
{ 05
// only check for Item objects 06
Item item = info.GetObject() as Item; 07
if (item == null) continue; 08
09
// search for objects with the same fields in the database 10
IObjectSet found = Container().Get(new Item(item.Number, item.Word)); 11
if (found.Count > 1) 12
{ 13
throw new Db4oException("Object is not unique: " + item); 14
} 15
} 16
}
1Private Shared Sub Configure() 2
Dim registry As IEventRegistry = EventRegistryFactory.ForObjectContainer(Container) 3
' register an event handler, which will check object uniqueness on commit 4
AddHandler registry.Committing, AddressOf OnCommitting 5
End Sub
1Private Shared Sub OnCommitting(ByVal sender As Object, ByVal args As CommitEventArgs) 2
' uniqueness should be checked for both added and updated objects 3
CheckUniqueness(args.Added) 4
CheckUniqueness(args.Updated) 5
End Sub
01Private Shared Sub CheckUniqueness(ByVal collection As IObjectInfoCollection) 02
For Each info As IObjectInfo In collection 03
' only check for Item objects 04
If (TypeOf info.GetObject() Is Item) Then 05
Dim item As Item = CType(info.GetObject, Item) 06
If item Is Nothing Then 07
Continue For 08
End If 09
' search for objects with the same fields in the database 10
Dim found As IObjectSet = Container.Get(New Item(item.Number, item.Word)) 11
If found.Count > 1 Then 12
Throw New Db4oException("Object is not unique: " + item.ToString()) 13
End If 14
End If 15
Next 16
End Sub
let's
save one initial object Item(1,"one")
01private static void StoreFirstObject() 02
{ 03
IObjectContainer container = Container(); 04
try 05
{ 06
// creating and storing item1 to the database 07
Item item = new Item(1, "one"); 08
container.Set(item); 09
// no problems here 10
container.Commit(); 11
} 12
catch (Db4oException ex) 13
{ 14
System.Console.WriteLine(ex.Message); 15
container.Rollback(); 16
} 17
}
01Private Shared Sub StoreFirstObject() 02
Dim container As IObjectContainer = CommitCallbackExample.Container() 03
Try 04
' end creating and storing item1 to the database 05
Dim item As Item = New Item(1, "one") 06
container.Set(item) 07
' no problems here 08
container.Commit() 09
Catch ex As Db4oException 10
System.Console.WriteLine(ex.Message) 11
container.Rollback() 12
End Try 13
End Sub
Now
we can check the functionality of the committing
callback using
the following code:
01private static void StoreOtherObjects() 02
{ 03
IObjectContainer container = Container(); 04
// creating and storing similar items to the database 05
Item item = new Item(2, "one"); 06
container.Set(item); 07
item = new Item(1, "two"); 08
container.Set(item); 09
try 10
{ 11
// commit should work as there were no duplicate objects 12
container.Commit(); 13
} 14
catch (Db4oException ex) 15
{ 16
System.Console.WriteLine(ex.Message); 17
container.Rollback(); 18
} 19
System.Console.WriteLine("Commit successful"); 20
21
// trying to save a duplicate object to the database 22
item = new Item(1, "one"); 23
container.Set(item); 24
try 25
{ 26
// Commit should fail as duplicates are not allowed 27
container.Commit(); 28
} 29
catch (Db4oException ex) 30
{ 31
System.Console.WriteLine(ex.Message); 32
container.Rollback(); 33
} 34
}
01Private Shared Sub StoreOtherObjects() 02
Dim container As IObjectContainer = CommitCallbackExample.Container() 03
Dim item As Item = New Item(2, "one") 04
container.Set(item) 05
item = New Item(1, "two") 06
container.Set(item) 07
Try 08
container.Commit() 09
Catch ex As Db4oException 10
System.Console.WriteLine(ex.Message) 11
container.Rollback() 12
End Try 13
System.Console.WriteLine("Commit successful") 14
item = New Item(1, "one") 15
container.Set(item) 16
Try 17
container.Commit() 18
Catch ex As Db4oException 19
System.Console.WriteLine(ex.Message) 20
container.Rollback() 21
End Try 22
End Sub