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.
For transparent activation/persistence you need following dependencies at compile time. (see also the dependency overview)
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 { }
<AttributeUsage(AttributeTargets.Class, AllowMultiple:=False, Inherited:=True)> _ Public Class TransparentPersistedAttribute Inherits Attribute End Class
This Attributeis then used to mark all persisted classes.
[TransparentPersisted] public class Person {
<TransparentPersisted()> _ Public Class Person
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)
As alternative you also can use an MSBuild task:
<UsingTask AssemblyFile=".\lib\Db4oTool.MSBuild.dll" TaskName="Db4oTool.MSBuild.Db4oEnhancerMSBuildTask" /> <ItemGroup> <Db4oEnhance Include="$(TargetPath)" /> </ItemGroup>
And the execute the task after the compilation.
<Target Name="AfterBuild"> <Db4oEnhancerMSBuildTask Assemblies="@(Db4oEnhance)" /> </Target>
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))); }
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
Configure the transparent activation in order to use it.
IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
configuration.Common.Add(new TransparentActivationSupport());
IObjectContainer container = Db4oEmbedded.OpenFile(configuration, DatabaseFileName);
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration() configuration.Common.Add(New TransparentActivationSupport()) Dim container As IObjectContainer = Db4oEmbedded.OpenFile(configuration, DatabaseFileName)
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);
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)
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);
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration() configuration.Common.Add(New TransparentPersistenceSupport(New DeactivatingRollbackStrategy())) Dim container As IObjectContainer = Db4oEmbedded.OpenFile(configuration, DatabaseFileName)
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();
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()