You are here: Advanced Features > Enhancement Tools > Required Depedencies

Transparent Persistence Enhancement Example

You can inject transparent persistence awareness in your persisted classes without modifying their original code. This is done by enhancing the assemblies at build time.

Required assemblies

For transparent activation/persistence you need following dependencies at compile time. (see also the dependency overview)

Enhance Persistent Classes

The first step is to enhance the persisted classes. One possibility is to introduce an Attribute to mark your persisted classes.

By the way, their are alternative way to select the enhanced classes. See here .

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public  class TransparentPersistedAttribute : Attribute
{
}
TransparentPersistedAttribute.cs: Annotation to mark persisted classes
<AttributeUsage(AttributeTargets.Class, AllowMultiple:=False, Inherited:=True)> _
Public  Class TransparentPersistedAttribute
    Inherits Attribute
End  Class
TransparentPersistedAttribute.vb: Annotation to mark persisted classes

This Attributeis then used to mark all persisted classes.

[TransparentPersisted]
public  class Person
{
Person.cs: Mark your domain model with the annotations
<TransparentPersisted()> _
Public  Class Person
Person.vb: Mark your domain model with the annotations

Execute Enhancement on Build

The next step is to run the enhancement step during the build. This is done by running the db4otool during the build:

For C#-projects you find the post build events in the project properties. Right click on the project, choose properties. On the project properties dialog choose 'Build Events'. Add the command there.

For VB.NET you find the post build events in the project properties. Right click on the project, choose properties. There navigate to the 'Compile'-tab and click the 'Build Events...'-button there.

On the post build event add this command:

$(ProjectDir)\lib\Db4oTool.exe -tp -debug
				-by-attribute:YourNamespace.TransparentPersistedAttribute $(TargetPath)
			

Alternative: MSBuild task

As alternative you also can use an MSBuild task:

<UsingTask AssemblyFile=".\lib\Db4oTool.MSBuild.dll" TaskName="Db4oTool.MSBuild.Db4oEnhancerMSBuildTask" />
<ItemGroup>
  <Db4oEnhance Include="$(TargetPath)" />
</ItemGroup>
simple-enhance-example.csproj: Define a task for the enhancement

And the execute the task after the compilation.

<Target Name="AfterBuild">
  <Db4oEnhancerMSBuildTask Assemblies="@(Db4oEnhance)" />
</Target>
simple-enhance-example.csproj: Define a target which runs the task

Check Enhancement

You can check if the enhancement worked correctly by checking for the activation interface. Such a check should be part of your test-suite to ensure that everything works correctly.

if (!typeof (IActivatable).IsAssignableFrom(typeof (Person)))
{
    throw  new InvalidOperationException(string.Format("Expect that the {0} implements {1}",
                                                      typeof (Person), typeof (IActivatable)));
}
TransparentPersistence.cs: Check for enhancement
If  Not  GetType(IActivatable).IsAssignableFrom(GetType(Person)) Then
    Throw  New InvalidOperationException(String.Format("Expect that the {0} implements {1}", GetType(Person), GetType(IActivatable)))
End  If
TransparentPersistence.vb: Check for enhancement

Using Transparent Activation

Configure the transparent activation in order to use it.

IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
configuration.Common.Add(new TransparentActivationSupport());
IObjectContainer container = Db4oEmbedded.OpenFile(configuration, DatabaseFileName);
TransparentPersistence.cs: Add transparent activation
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
configuration.Common.Add(New TransparentActivationSupport())
Dim container As IObjectContainer = Db4oEmbedded.OpenFile(configuration, DatabaseFileName)
TransparentPersistence.vb: Add transparent activation

After that transparent activation is working properly you can transverse along the object graph and don’t have to worry about not activated objects:

Person person = QueryByName(container, "Joanna the 10");
Person beginOfDynasty = person.Mother;

// With transparent activation enabled, you can navigate deeply
// nested object graphs. db4o will ensure that the objects
// are loaded from the database.
while (null != beginOfDynasty.Mother)
{
    beginOfDynasty = beginOfDynasty.Mother;
}
Console.WriteLine(beginOfDynasty.Name);
TransparentPersistence.cs: Activation just works
Dim person As Person = QueryByName(container, "Joanna the 10")
Dim beginOfDynasty As Person = person.Mother

' With transparent activation enabled, you can navigate deeply
' nested object graphs. db4o will ensure that the objects
' are loaded from the database.
While beginOfDynasty.Mother IsNot  Nothing
    beginOfDynasty = beginOfDynasty.Mother
End  While
Console.WriteLine(beginOfDynasty.Name)
TransparentPersistence.vb: Activation just works

Using Transparent Persistence

Transparent persistence not only manages the activation, but also manages updating the objects. Configure transparent persistence in order to use it:

IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
configuration.Common.Add(new TransparentPersistenceSupport(new DeactivatingRollbackStrategy()));
IObjectContainer container = Db4oEmbedded.OpenFile(configuration, DatabaseFileName);
TransparentPersistence.cs: Add transparent persistence
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
configuration.Common.Add(New TransparentPersistenceSupport(New DeactivatingRollbackStrategy()))
Dim container As IObjectContainer = Db4oEmbedded.OpenFile(configuration, DatabaseFileName)
TransparentPersistence.vb: Add transparent persistence

After that updated objects are automatically stored every time you commit.

Person person = QueryByName(container, "Joanna the 10");
Person mother = person.Mother;
mother.Name = "New Name";
// Just commit the transaction. All modified objects are stored
container.Commit();
TransparentPersistence.cs: Just update and commit. Transparent persistence manages all updates
Dim person As Person = QueryByName(container, "Joanna the 10")
Dim mother As Person = person.Mother
mother.Name = "New Name"
' Just commit the transaction. All modified objects are stored
container.Commit()
TransparentPersistence.vb: Just update and commit. Transparent persistence manages all updates