Sans Pareil Technologies, Inc.

Key To Your Business

Simple ODM Framework



The uma::bson API added a simple callback based Object-Document Mapping (ODM) framework in version 2.2. This makes it possible for client applications/libraries to create domain models that use the various uma::bson::Value instances to store data and inherit from the abstract uma::bson::Object class. Serialisation to/from BSON is provided by the API through the callback methods.

Serialisation to BSON involves retrieving a list of field names that are to be serialised. The framework iterates over the list of field names and invokes the getValue method to retrieve the value to be serialised. The de-serialisation process invokes the setValue method on the instance to populate the fields with data from the BSON data stream.

Callback Methods



The following three pure virtual methods must be implemented by the model object to enable automatic BSON serialisation support. An optional virtual method must be implemented by model objects that store arrays of other objects.

const Object::FieldNames getFieldNames() const



Return a std::vector<std::string> that lists the fields in the instance that are to be serialised. Note that the order of fields specified is important when comparing different BSON data streams. This method is used primarily during the BSON serialisation process. The equality operator for uma::bson::Object also uses this method.

Value& getValue( const std::string& name )



This method must return the value for the field with the specified name in the object. Note that the API at present requires a reference to the Value, thus forcing clients to model internal fields as uma::bson::Value instances. This is purely for performance reasons, and may be modified in a future version to return an object, which would enable modelling fields as raw datatypes. This method is used by the BSON serialisation process as it iterates over the list of field names, and retrieves the value that is to be serialised.

Note that this method is also used during the de-serialisation process when populating Object data types. The de-serialiser fetches the reference to the current (usually default constructed) Object instance from the destination model instance and then populates it with the data from the BSON stream. This is done to ensure that the Object instance being populated is of the same type as the type used in the destination model class.

void setValue( const std::string& name, const uma::bson::Value& value )



This method is used to set the value of a field in the model object by name. This method is the primary driver for the BSON de-serialisation process. The BSON elements parsed from the data stream trigger invocation of this method with appropriate name and value pairs.

As mentioned in the notes about the getValue method, for Object type fields, the references to the existing instances are fetched from the destination model object and then populated. This method is still invoked with the updated object, so the implementation ideally would check for same memory location before assigning.

Note

Starting with version 2.3 of the API, this method is no longer pure virtual. A default implementation is provided which should handle most needs. The internal BSON de-serialiser fetches the reference to the current object/array fields using the getValue method and populates them. Hence, there should be no need to update the already updated field instance. The method is still virtual to allow any specific customisations, or for cases where client code implements its its own serialisation. The unit test suite has been updated to test the proper behaviour of the default implementation.

Object* getObjectForArray( const std::string& name )



This optional method should be implemented by all model objects that store other objects in an uma::bson::Array. Note that this is not required if the array stores simple types (Integer, Date, String), and is only required when the values stored in the array are other objects. The de-serialisation process will invoke this method, populate and set in the array instance that is being constructed with the BSON data. Note that the Object instance returned must ideally be a freshly created instance on the heap. The uma::bson::Element instance that stores this object instance takes ownership of the memory and hence client code must not delete these instances.

Note that the default implementation for this method throws an exception. Classes that include array fields that store other objects must implement this method, or an exception will be raised at run time. Proper unit testing should catch this issue, and lead to this method being implemented where appropriate.

Sample Model Class



The following sample model class (taken from the unit test suite) illustrates the concepts behind the ODM framework. The ManufacturingObject class contains two fields - name and model. The public interface for the class encapsulates the uma::bson::String fields and presents clients a std::string based interface.

Header File

The class contains two string fields which store the name and model identifier. The public interface for the class allows users to get/set the stored value as std::string instances. The callback methods are made private to ensure that the serialisation implementation is hidden from callers. This in general is the recommended pattern for modelling your domain objects using the ODM framework.

    1: #ifndef SAMPLE_VALUE_MANUFACTUREROBJECT_H
    2: #define SAMPLE_VALUE_MANUFACTUREROBJECT_H
    3:
    4: #include <uma/bson/Object.h>
    5: #include <uma/bson/String.h>
    6:
    7: namespace sample
    8: {
    9:   namespace value
   10:   {
   11:
   12:     class ManufacturerObject : public uma::bson::Object
   13:     {
   14:     public:
   15:       ManufacturerObject() {}
   16:
   17:       const std::string& getName() const { return name.getValue(); }
   18:       void setName( const std::string& n ) { name.setValue( n ); }
   19:
   20:       const std::string& getModel() const { return model.getValue(); }
   21:       void setModel( const std::string& m ) { model.setValue( m ); }
   22:
   23:
   24:     private:
   25:       const FieldNames getFieldNames() const;
   26:       uma::bson::Value& getValue( const std::string& name );
   27:       void setValue( const std::string& name, const uma::bson::Value& value );
   28:
   29:
   30:     private:
   31:       uma::bson::String name;
   32:       uma::bson::String model;
   33:     };
   34:
   35:   } // namespace value
   36: } // namespace sample
   37:
   38: #endif // SAMPLE_VALUE_MANUFACTUREROBJECT_H



Implementation File

The getFieldNames method creates a new vector and populates it with the field names. Since this information is unlikely to change at runtime, we may modify the method declaration to make it return a reference to a vector. This will allow a static vector to be maintained and returned to avoid duplicating this information all the time.

Both the getValue and setValue methods throw an exception if an invalid element name is specified. This is generally a good practice and will help find any issues while unit testing the serialisation process.

    1: #include "ManufacturerObject.h"
    2:
    3: using namespace sample::value;
    4: using namespace uma::bson;
    5: using std::string;
    6:
    7:
    8: const Object::FieldNames ManufacturerObject::getFieldNames() const
    9: {
   10:   FieldNames names;
   11:   names.push_back( "name" );
   12:   names.push_back( "model" );
   13:
   14:   return names;
   15: }
   16:
   17:
   18: uma::bson::Value& ManufacturerObject::getValue( const string& field )
   19: {
   20:   if ( field == "name" ) return name;
   21:   if ( field == "model" ) return model;
   22:
   23:   throw Poco::InvalidArgumentException( "No field with name: " + field + " in sample::value::ManufacturerObject" );
   24: }
   25:
   26:
   27: void ManufacturerObject::setValue( const string& field, const uma::bson::Value& value )
   28: {
   29:   const uma::bson::String& str = dynamic_cast<const uma::bson::String&>( value );
   30:
   31:   if ( field == "name" ) { name = str; return; }
   32:   if ( field == "model" ) { model = str; return; }
   33:
   34:   throw Poco::InvalidArgumentException( "No field with name: " + field + " in sample::value::ManufacturerObject" );
   35: }



Client Code

sample::value::ManufacturerObject manufacturer;
manufacturer.setName( “BlackBerry” );
manufacturer.setModel( “Q10” );

// a little later in your code
std::stringstream ss;
manufacturer.toBson( ss ); // serialise as BSON data stream.

// Another part of your code
sample::value::ManufacturerObject mobj;
mobj.populate( ss ); // de-serialise data from BSON stream

if ( manufacturer != mobj ) throw “Serialisation failed”;