Remote Execution Through Evaluation API

One of the ways to do that is using evaluation API. Evaluation/candidate classes are serialized and sent to the server. That means that if we will put the selection criteria and update code inside evaluation class, we will have that code on the server and executing a query using evaluation on the client side will run the update code on the server side.

For easier query execution we can use database singleton - a class that have only one instance saved in the database. That actually can be the class calling the query itself.

Let's fill up our server database:

RemoteExample.cs: SetObjects
01private static void SetObjects() 02 { 03 File.Delete(Db4oFileName); 04 IObjectContainer db = Db4oFactory.OpenFile(Db4oFileName); 05 try 06 { 07 for (int i = 0; i < 5; i++) 08 { 09 Car car = new Car("car"+i); 10 db.Set(car); 11 } 12 db.Set(new RemoteExample()); 13 } 14 finally 15 { 16 db.Close(); 17 } 18 CheckCars(); 19 }

RemoteExample.vb: SetObjects
01Private Shared Sub SetObjects() 02 File.Delete(Db4oFileName) 03 Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 04 Try 05 Dim i As Integer 06 For i = 0 To 5 - 1 Step i + 1 07 Dim car As Car = New Car("car" + i.ToString()) 08 db.Set(car) 09 Next 10 db.Set(New RemoteExample()) 11 Finally 12 db.Close() 13 End Try 14 CheckCars() 15 End Sub

Now we can update the cars using specially designed singleton:

RemoteExample.cs: UpdateCars
01private static void UpdateCars() 02 { 03 // triggering mass updates with a singleton 04 // complete server-side execution 05 IObjectServer server=Db4oFactory.OpenServer(Db4oFileName,0); 06 try 07 { 08 IObjectContainer client=server.OpenClient(); 09 IQuery q = client.Query(); 10 q.Constrain(typeof(RemoteExample)); 11 q.Constrain(new UpdateEvaluation()); 12 q.Execute(); 13 client.Close(); 14 } 15 finally 16 { 17 server.Close(); 18 } 19 CheckCars(); 20 }

RemoteExample.vb: UpdateCars
01Private Shared Sub UpdateCars() 02 ' triggering mass updates with a singleton 03 ' complete server-side execution 04 Dim server As IObjectServer = Db4oFactory.OpenServer(Db4oFileName, 0) 05 Try 06 Dim client As IObjectContainer = server.OpenClient() 07 Dim q As IQuery = client.Query() 08 q.Constrain(GetType(RemoteExample)) 09 q.Constrain(New UpdateEvaluation()) 10 q.Execute() 11 client.Close() 12 Finally 13 server.Close() 14 End Try 15 CheckCars() 16 End Sub

This method has its pros and cons.

Pros:

  • any arbitrary code can be executed;
  • the code is executed on the server side;

Cons:

  • the code will have to be serialized and sent over a network connection;
  • changing the code will require update of all clients