You are here: Platform Specific Issues > Silverlight

Silverlight

db4o runs with some limitations on Microsoft's Silverlight. You can find the Silverlight assemblies in the db4o distribution, in the folder 'bin/silverlight'.

Feature Set

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.

Add Silverlight Support

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");
IOExamples.cs: Add Silverlight support
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
configuration.AddConfigurationItem(New SilverlightSupport())

Dim container As IObjectContainer = Db4oEmbedded.OpenFile(configuration, "database.db4o")
IOExamples.vb: Add Silverlight support

Persistent Object Limitations

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;
}
Person.cs: fields need to be public to be persisted
Public Class Person
    Public FirstName As String
    Public SirName As String
End Class
Person.vb: fields need to be public to be persisted

Queries

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
}
QueriesInSilverlight.cs: Queries in Silverlight
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
QueriesInSilverlight.vb: Queries in Silverlight

Isolated Storage

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");
IOExamples.cs: use the isolated storage on silverlight
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
configuration.File.Storage = New IsolatedStorageStorage()

Dim container As IObjectContainer = Db4oEmbedded.OpenFile(configuration, "database.db4o")
IOExamples.vb: use the isolated storage on silverlight