Sans Pareil Technologies, Inc.

Key To Your Business

ODM Framework



The uma::bson API added a simple function pointer based Object-Document Mapping (ODM) framework in version 2.5. 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::ODMObject class. This extends the previous simple ODM made available through uma::bson::Object (ODMObject inherits from Object)

The primary requirements for using the ODM framework are the following:

  • Inherit from the uma::bson::ODMObject class and specify the class as the template parameter. The actual type is used for the MetaField implementation that is used to store the function pointers to the accessor and mutator methods for the fields (similar to the model used for POD or POJO type objects).
  • Declare all serialisable fields as instances of uma::bson::Value.
  • Register the serialisable fields by creating a MetaFieldImpl instance with the value type as template parameter, field name (may be aliased differently for bson I/O), function pointer to the accessor method for the field (needs to be non-const), and function pointer to the mutator method for the field and invoking the registerField() method.

The registration process should ideally be done in each constructor implemented (or designated constructor if using C++11), and performed only if registered() returns false.

Header File

The class contains two string fields which store the name and model identifier. Standard accessor and mutator methods are defined for these fields. The common method used to register the fields are implemented in a private registerFields method which is invoked from the constructors.

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



Implementation File

The implementation file provides the implementation for the constructors and the private registerFields method. Note that the registration is wrapped within a check to see if the current class has already been registered. This is the recommended pattern to follow, since there is no reason to re-register the class for the various instances that may be created during application runtime. We had some issues with static registration using a macro, hence this round-about way for performing the registration (we have generally had no luck with getting static registration to work with MSVC).

    1: #include "ManufacturerODM.h"
    2:
    3: using namespace sample::odm;
    4:    
    5: ManufacturerODM::ManufacturerODM()
    6: {
    7:   registerFields();
    8: }
    9:
   10:
   11: ManufacturerODM::ManufacturerODM( const std::string& nm, const std::string& mo ) :
   12:   name( nm ), model( mo )
   13: {
   14:   registerFields();
   15: }
   16:
   17:
   18: void ManufacturerODM::registerFields()
   19: {
   20:   if ( ! registered( this ) )
   21:   {
   22:     registerField(
   23:         new MetaFieldImpl<uma::bson::String>( "name", &ManufacturerODM::getName, &ManufacturerODM::setName ) );
   24:     registerField(
   25:         new MetaFieldImpl<uma::bson::String>( "model", &ManufacturerODM::getModel, &ManufacturerODM::setModel ) );
   26:   }
   27: }



Client Code

Client code is nearly identical to that using the simple callback based ODM framework.

sample::odm::ManufacturerODM 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::odm::ManufacturerODM mobj;
mobj.populate( ss ); // de-serialise data from BSON stream

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