Artificial fields, what are they good for?

OpenAccess requires a persistent class model to work. This means that data not represented by a field in a persistent class cannot be stored or retrieved.  Sometimes you need to add new data to your application during runtime. With many other ORMs you have to add a field to your persistent class, recompile everything and update the database schema.  That is a little unhandy and looks like you always need a developer to do so.

OpenAccess Artificial Fields can help you to add such new data definitions on the fly. You can specify the field definition with the complete mapping in an xml format and OpenAccess can use it to add the new field to the internal class definition and to the database schema.

Here is an example of the XML mapping that adds an ‘age’ field to the ‘Person’ class definition.

<artificial> 
  <mapping id="artificialMapping1"
    <namespace name="AddressDataModel"
      <class name="Person"
        <field name="age" clr="System.Int32" /> 
      </class> 
    </namespace> 
  </mapping> 
</artificial> 

It is necessary that the class AddressDataModel already exists and is defined as persistent class. The code adds a field named age with type integer to the existing persistent class AddressDataModel. But what about the database schema?  The trick is now that the OpenAccess generated schema can be updated by the schema API, it is possible because of the forward mapping capabilities of OpenAccess.

string ddlscript = database.GetSchemaHandler().CreateUpdateDDLScript(null); 
if (!string.IsNullOrEmpty(ddlscript)) 
    database.GetSchemaHandler().ExecuteDDLScript(ddlscript); 
 

The only open question now is; how can we access the new field? It is not showing up in the class itself because we did not change any code. The access has to be done via the OpenAccess generic access API described in this blog post.

A complete example can be found in the OpenAccess 2009 Q2 release. Start the QuickStartFramwork from the start menu and choose the GenericAccessAndArtificialFields example.



Comments

Comments are disabled in preview mode.