This project is read-only.
Transparent persistance
TransparentPersistedAttribute ([TransparentPersisted]) is defined in DonNicky.Db.Db4o.Config.Attributes. Finally to enable transparent persistance you should:
1. Set container configuration accordingly:
cfg.Common.Add(new TransparentPersistenceSupport(new DeactivatingRollbackStrategy()));
2. Use [TransparentPersisted] attribute for your class:
[TransparentPersisted]
public class Employee 
{
    ...
}
3. Use db4otool.exe in post-build project event:
$(ProjectDir)\..\tools\Db4oTool.exe -collections -tp -debug -by-attribute:DonNicky.Db.Db4o.Config.Attributes.TransparentPersistedAttribute $(TargetPath)

Composite keys
To use composite keys you should:
1. Use UniqueGroup group attribute as in the following code:
public class Employee : Db4oTPEntity
{
    // composite primary key (_company, _id)
    [UniqueGroup(1)]
    private Company _company;
    [UniqueGroup(1)]
    private int _id;
    // alternative key (_firstName, _middleName, _lastNamem, _dateOfBirth)
    [UniqueGroup(2)]
    private string _firstName, _middleName, _lastName;
    [UniqueGroup(2)]
    private DateTime _dateOfBirth;
}
2. Call defined in DonNicky.Db.Db4o namespace UseExtensions() extension method (registers necessary handler for commiting event) for your IObjectContainer:
IObjectContainer db = Db4oEmbedded.OpenFile(cfg, databaseFileName);
db.UseExtensions();
[Unique] attribute
To ensure uniqueness of a field defined with a according attribute you should call IEmbeddedConfiguration.UseExtensions() extension method (DonNicky.Db.Db4o namespace):
IEmbeddedConfiguration cfg = Db4oEmbedded.NewConfiguration();
cfg.UseExtensions(Assembly.GetAssembly(typeof(Company)));
.NET data annotations
1. Define field attributes in your model:
public class Company : Db4oTPEntity
{
    private int _id;
    private string _name;
    // only positive values allowed
    [Range(1, int.MaxValue)]
    public int Id { get { return _id; } set { _id = value; } }
    // required property
    [Required]
    public string Name { get { return _name; } set { _name = value; } }
}
2. Call defined in DonNicky.Db.Db4o namespace UseExtensions() extension method (registers necessary handler for updating and creating events) for your IObjectContainer:
IObjectContainer db = Db4oEmbedded.OpenFile(cfg, databaseFileName);
db.UseExtensions();
Object consistency checking
1. Define in your class CheckConsistency() method:
public class Employee
{
   public bool CheckConsistency() { return _position.Company == _department.Company; }
}
2. Call defined in DonNicky.Db.Db4o namespace UseExtensions() extension method (registers necessary handler for updating and creating events) for your IObjectContainer:
IObjectContainer db = Db4oEmbedded.OpenFile(cfg, databaseFileName);
db.UseExtensions();
Deferred deletion
This a two-phase deletion: firstly you mark objects for deletion, secondly you delete them from database really. To use this scheme:
1. You should emplement DonNicky.Db.IDeletable interface in your class or derive your class from DonNicky.Db.Db4o.Db4oTPEntity (in this case your derived class must have [TransparentPersisted] attribute) which implements IDeletable interface.
2. Define deletion behavior for dependent classes (you can omit this and default restrict behavior will be applied):
[TransparentPersisted]
public class Company : Db4oTPEntity
{
}

public class Department : Db4oTPEntity
{
    [DeleteStrategy(DeleteBehavior.Cascade)]
    private Company _company;
}
3. Mark object for deletion:
Company company = (select Company c from db select c).Single();
company.Delete();
4. Mark dependent object for cascade deletion (if they have according behavior defined via attribute):
db.DeleteCascadeAll();
5. Make real deletion of marked objects:
db.Purge();
Example

Last edited May 27, 2012 at 4:06 PM by donnicky, version 12

Comments

No comments yet.