ASP.NET

This topic applies to .NET version only 

You can use db4o as a persistent layer of your ASP.NET application. The actual integration of db4o into your web application is quite simple: you just need to add reference to Db4objects.Db4o.dll and use it in a client/server mode.

Security Requirements 

However, as it is characteristic to web-applications in general, there are some security permissions involved, which can in fact forbid db4o functionality in your ASP.NET application. So, before developing/deploying you should make sure that the required permissions would be granted to your application at the hosting server:

  1. ASPNET user should have read/write permissions to the directory containing database file. Obviously this is necessary to work with the database.
  2. Trust Level of your site should be set to "Full".

  3. All the necessary permissions should be granted to db4o assembly. The easiest way to ensure this is to add full trust to db4o:

    • For .NET 1.1 you can set this using caspol utility:
    • caspol -af Db4objects.Db4o.dll
    • For .NET versions >= 2.0 Db4objects.Db4o.dll should be installed in GAC

    If full trust is not a suitable solution for, you can check the minimum security permissions that db4o dll needs to operate using permcalc.exe tool from your Visual Studio installation.

    PermCalc.exe -Sandbox Db4objects.Db4o.dll

    sandbox.permcalc.xml
    01<?xml version="1.0"?> 02<Sandbox> 03 <PermissionSet version="1" class="System.Security.PermissionSet"> 04 <IPermission version="1" class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Read="*AllFiles*" PathDiscovery="*AllFiles*" /> 05 <IPermission version="1" class="System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Flags="MemberAccess" /> 06 <IPermission version="1" class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Flags="UnmanagedCode, Execution, ControlEvidence" /> 07 <IPermission Window="SafeSubWindows" Clipboard="OwnClipboard" version="1" class="System.Security.Permissions.UIPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 08 <IPermission version="1" class="System.Security.Permissions.KeyContainerPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Unrestricted="true" /> 09 </PermissionSet> 10</Sandbox>

    Consult with your web-server administrator to grant these permissions.

Sample Application

Let's look at an example implementation: we will create a simple ASP.NET application, which will use db4o to store, retrieve and delete objects

The basic requirements for seamless db4o integration are:

In order to keep page-specific code clean we will implement db4o functionality in a separate Db4oHttpModule implementing IhttpModule interface.

Database file path can be saved in Web.Config:

<appSettings>
    <add key="db4oFileName" value="~/Data/Test.yap"/>
</appSettings>

Make sure that you have Data folder inside you web-site directory. ASPNET user should have enough rights to create and modify files inside this folder. You should also make sure that Data folder is not accessible to anonymous user; otherwise web-server visitors will be able to download your database.

We will open db4o connection only with the first client request:

The following code will ensure that the database is closed upon termination:

This is basically all the functionality that is required from db4o module. In order to make use of it we need to register it in Web.Config:

<httpModules>
      <add type="Db4objects.Db4odoc.Web.Data.Db4oHttpModule" name="Db4oHttpModule" />
</httpModules>

With the help of the created module we can access db4o database fairly easy:

To test the whole application you can use the following simple form:

Persisting Objects in ASP.NET2 

If you are creating your application in ASP.NET2 you should take into consideration the fact that the assembly names are generated automatically on each build by default. Db4o distinguish persisted classes by name, namespace and assembly, so after the assembly name change, you won't be able to access classes saved with the previous version of the assembly.

There are several workarounds: