Working with Code and Persistence Mapping

This article only considers Persistence in a SQL database. As such it does not apply to systems that use XmlPersistenceMappers or MDrivenServer (MDrivenServer has the settings described below as default).

In MDriven Framework a lot is done by just using default values. But if you want to tune the default values you can.

Consider this model

image

We state that the “TheDefaultSuperClass” should be the Superclass for all other classes in our package by setting the Package

image

We mark the TheDefaultSuperClass as being Abstract (indicated by Italic style on the class name in diagrams).

The default is that the DefaultSuperclass will get its own table in the database. But that is actually often not what we want to do.

Instead we want to have the fields from DefaultSuperClass in all inheriting classes tables.

This is how; Add a DefaultORMappingBuilder component to your PMP designer. Set the NewMappingProvider (the mapping that is calculated from your current model) to the DefaultORMappingBuilder1. Also set the RunTimeMappingProvider (the mapping that will be used in runtime).

image

 

In the DefaultORMappingBuilder we can now influence the Mapping in certain ways:

image

Above I said that the DefaultSuperclass for the package (here called RootClass) should be ChildMapped – ie its attributes not stored in a table of its own but rather in each child´s table.

I also choose the change how foreign key fields should be generated – the default is <Name>, but here I append an extra “ID”.

I also set the Id Column name for the  each table; the default is “Eco_Id” – but I would rather have it to be based on the tables name so I set <TableName>ID.

These two changes from default behavior makes the database easier to read for someone just accessing the database – as all Identities that are joinable end with ID.

 

For control of an ordinary class’s OR mapping (ie the non DefaultSuperClass) you simply set this on the class:

image

If you do not see this property on your classes – it is most likely that you need to set OrMappingConfigMode setting on your package:

image

Having the settings above done we can now; generate code, build all, switch to PMP view, Press Validate Model, Press Generate default mapping XML:

image

Once you have generated the default mapping XML you get an XML file appended to the project.

It looks like this:

 

<?xml version="1.0" encoding="utf-8"?>
<ORMapping>
  <ClassDef Name="Class1" SuperClass="TheDefaultSuperClass">
    <AliasDef Name="Class1_A" Table="Class1" ExtentRequiresDiscriminator="False">
      <KeyImpl Name="EcoKey">
        <KeyColumn Name="Class1ID" />
      </KeyImpl>
      <DiscriminatorImpl Name="EcoType" Column="ECO_TYPE" />
    </AliasDef>
    <DiscriminatorValue Name="EcoType" Value="3" />
    <AttributeDef Name="ChangeTime" Alias="Class1_A" Columns="ChangeTime" AttributeMapper="&lt;Default&gt;" />
    <AttributeDef Name="CreateTime" Alias="Class1_A" Columns="CreateTime" AttributeMapper="&lt;Default&gt;" />
    <AttributeDef Name="Attribute1" Alias="Class1_A" Columns="Attribute1" AttributeMapper="&lt;Default&gt;" />
  </ClassDef>
  <ClassDef Name="Class4" SuperClass="Class3">
    <AliasDef Name="Class4_A" Table="Class4" ExtentRequiresDiscriminator="False">
      <KeyImpl Name="EcoKey">
        <KeyColumn Name="Class4ID" />
      </KeyImpl>
      <DiscriminatorImpl Name="EcoType" Column="ECO_TYPE" />
    </AliasDef>
    <DiscriminatorValue Name="EcoType" Value="7" />
    <AttributeDef Name="ChangeTime" Alias="Class4_A" Columns="ChangeTime" AttributeMapper="&lt;Default&gt;" />
    <AttributeDef Name="CreateTime" Alias="Class4_A" Columns="CreateTime" AttributeMapper="&lt;Default&gt;" />
    <AttributeDef Name="Attribute2" Alias="Class4_A" Columns="Attribute2" AttributeMapper="&lt;Default&gt;" />
    <AttributeDef Name="Attribute1" Alias="Class4_A" Columns="Attribute1" AttributeMapper="&lt;Default&gt;" />
  </ClassDef>
  <ClassDef Name="Class3" SuperClass="TheDefaultSuperClass" /> <!-- NOTICE - EMPTY DUE to being abstract-->
  <ClassDef Name="TheDefaultSuperClass">
    <KeyDef Name="EcoKey" Signature="System.Int32" IsId="True" KeyMapper="DefaultEcoIdMapper" />
    <DiscriminatorDef Name="EcoType" Signature="System.Int16" />
  </ClassDef>
  <ClassDef Name="Class2" SuperClass="Class1">
    <AliasDef Name="Class2_A" Table="Class2" ExtentRequiresDiscriminator="False">
      <KeyImpl Name="EcoKey">
        <KeyColumn Name="Class2ID" />
      </KeyImpl>
      <DiscriminatorImpl Name="EcoType" Column="ECO_TYPE" />
    </AliasDef>
    <DiscriminatorValue Name="EcoType" Value="13" />
    <AttributeDef Name="Attribute2" Alias="Class2_A" Columns="Attribute2" AttributeMapper="&lt;Default&gt;" />
  </ClassDef>
  
  <Database Name="">   <!-- NOTICE – Missing both Class3 and TheDefaultSuperClass since they are both child mapped and abstract--> 
    <Table Name="Class1">
      <Column Name="Class1ID" AllowNULL="False" Type="INT" Length="0" Scale="0" Precision="0" DefaultValue="" />   <!-- NOTICE - TableName+ID -->
      <Column Name="ECO_TYPE" AllowNULL="False" Type="SMALLINT" Length="0" Scale="0" Precision="0" DefaultValue="" />
      <Column Name="ChangeTime" AllowNULL="False" Type="DATETIME" Length="255" Scale="-1" Precision="-1" DefaultValue="'19000101'" /> <!-- NOTICE - Inherited attributes child mapped -->
      <Column Name="CreateTime" AllowNULL="False" Type="DATETIME" Length="255" Scale="-1" Precision="-1" DefaultValue="'19000101'" />
      <Column Name="Attribute1" AllowNULL="True" Type="VARCHAR(255)" Length="255" Scale="-1" Precision="-1" DefaultValue="''" />
      <Index Name="IX_PK_Class1" Columns="Class1ID" IsUnique="False" IsCaseSensitive="False" IsPrimary="True" IsDescending="False" IsFilteredOnNotNull="False" />
    </Table>   
    <Table Name="Class2">
      <Column Name="Class2ID" AllowNULL="False" Type="INT" Length="0" Scale="0" Precision="0" DefaultValue="" />
      <Column Name="ECO_TYPE" AllowNULL="False" Type="SMALLINT" Length="0" Scale="0" Precision="0" DefaultValue="" />
      <Column Name="Attribute2" AllowNULL="True" Type="VARCHAR(255)" Length="255" Scale="-1" Precision="-1" DefaultValue="''" />
      <Index Name="IX_PK_Class2" Columns="Class2ID" IsUnique="False" IsCaseSensitive="False" IsPrimary="True" IsDescending="False" IsFilteredOnNotNull="False" />
    </Table>
    <Table Name="Class4"> <!-- NOTICE – attributes from both TheDefaultSuperclass And from Class3--> 
      <Column Name="Class4ID" AllowNULL="False" Type="INT" Length="0" Scale="0" Precision="0" DefaultValue="" />
      <Column Name="ECO_TYPE" AllowNULL="False" Type="SMALLINT" Length="0" Scale="0" Precision="0" DefaultValue="" />
      <Column Name="Attribute1" AllowNULL="True" Type="VARCHAR(255)" Length="255" Scale="-1" Precision="-1" DefaultValue="''" />
      <Column Name="ChangeTime" AllowNULL="False" Type="DATETIME" Length="255" Scale="-1" Precision="-1" DefaultValue="'19000101'" />
      <Column Name="CreateTime" AllowNULL="False" Type="DATETIME" Length="255" Scale="-1" Precision="-1" DefaultValue="'19000101'" />
      <Column Name="Attribute2" AllowNULL="True" Type="VARCHAR(255)" Length="255" Scale="-1" Precision="-1" DefaultValue="''" />
      <Index Name="IX_PK_Class4" Columns="Class4ID" IsUnique="False" IsCaseSensitive="False" IsPrimary="True" IsDescending="False" IsFilteredOnNotNull="False" />
    </Table>
  </Database>
</ORMapping>

 

Normally you will not bother with generating this file. You would just let MDriven generate it when evolving the database – then MDriven stores this in the database so that it can retrieve it on the next evolve. MDriven use the old mapping and compares it to a fresh generated mapping in order to deduce all the fine print your model changes inflict on the db-mapping.

image

In order to completely de-mystify the Mapping provision:

NewMappingProvider to get information on how the mapping should look after evolve db.

OldMappingProvider to get information on how the mapping looks in your current db.

RuntimeMappingProvider is the mapping your system use in runtime when accessing the database.

This entry was posted in ORMapping and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*