db4o runs with some limitations on Microsoft's Silverlight. You can find the Silverlight assemblies in the db4o distribution, in the folder 'bin/silverlight'.
Currently only the core functionality is supported on Silverlight. The db4o core and the db4o LINQ provider run on Silverlight. Other features like the client-server mode are not supported.
To run db4o on Silverlight you need to use the Silverlight assemblies which are in the db4o distribution. Additionally you need to configure the Silverlight support in the db4o configuration. This also configured the isolated storage.
IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration(); configuration.AddConfigurationItem(new SilverlightSupport()); IObjectContainer container = Db4oEmbedded.OpenFile(configuration, "database.db4o");
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration() configuration.AddConfigurationItem(New SilverlightSupport()) Dim container As IObjectContainer = Db4oEmbedded.OpenFile(configuration, "database.db4o")
Due to the restrictions of the Silverlight sandbox, it's impossible to inspect the private fields of an object. This means that the field of persistent objects need to be public!
Also auto-properties cannot be stored. Auto properties use a regular, compiler generated private field to hold the data. Since db4o cannot access private fields, it also cannot access the data of auto-properties.
public class Person { public string FirstName; public string SirName; }
Public Class Person Public FirstName As String Public SirName As String End Class
You can use LINQ to query your objects in Silverlight. Note that the Silverlight version uses the Mono.Cecil.dll and the Cecil.FlowAnalysis.dll assembly for the LINQ-implementation.
var persons = from Person p in container where p.FirstName.Contains("Roman") select p; foreach (Person person in persons) { // do something with the person }
Dim persons = From p As Person In container _ Where p.FirstName.Contains("Roman") _ Select p For Each p As Person In persons ' do something with the person Next
By default you cannot access the file system in Silverlight, but only a special isolated storage. Therefore you need to use the isolated storage when you're configuring the IO system. By default this storage is limited to 1 MByte of data. You can increase it with the users consent.
When the Silverlight application runs outside the browser with enough privileges you can use the regular storage. However by default the isolated storage should be used.
IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
configuration.File.Storage
= new IsolatedStorageStorage();
IObjectContainer container = Db4oEmbedded.OpenFile(configuration, "database.db4o");
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration() configuration.File.Storage = New IsolatedStorageStorage() Dim container As IObjectContainer = Db4oEmbedded.OpenFile(configuration, "database.db4o")