Search code examples
db4oduplication

db4o Impedance Mismatch


I've built a nice repository layer around a db4o database to store Product objects, which relate to Manufacturer objects in a one-to-many relationship, i.e.:

public class Manufacturer
{
    public string Name { get; set; }
}

public class Product // simplified
{
    public string Name { get; set; }
    Manufacturer Manufacturer { get; set; }
}

So far I really like db4o. The problem I have is preventing data duplication without resorting to IDs.

When implementing references like manufacturers using SQL Server, my data models would contain a unique ID field, and in turn my Product class would be dirtied up with a ManufacturerID. I imagined that using an object database like db4o would reduce the impedance mismatch between a relational DB and objects, but without IDs, there is no way to tell one object from another when editing it.

Is there an elegant way to share a manufacturer between products without duplicating data? Or should I just use a relational DB?


Solution

  • You can add an unique index to db4o in your config.

    configuration.Common.ObjectClass(typeof (Manufacturer)).ObjectField("<Name>k__BackingField").Indexed(true);
    configuration.Add(new UniqueFieldValueConstraint(typeof(Manufacturer), "<Name>k__BackingField"));
    

    This way it is not possible to have two different Manufacturer object with the same name in your db. The field name have to be "k__BackingField"because you are using auto properties. Of course you could add an integer ID, and index it the same way.