Knight Data Access Layer
Composition VB.NET
Basic Tasks VB.NET > Mapping > Composition VB.NET

Composition Overview

Composition allows you to embed a class within another class and have it save to the save to the same table as a flattened object.  In the example below, the ClaimDetail will be stored in the Claims table as part of the Claim object.  The Detail property is simply decorated with the composition attribute.

 

Mapping with Attributes

Public Class Claim

Public Property ClaimId() As Integer

Public Property ClaimDate() As Date

<Composition>

Public Property Detail() As ClaimDetail

End Class

 

Public Class ClaimDetail

Public Property ClaimType() As String

End Class

 

Mapping with Code

Dim db As IDataHelper = DataHelper.SessionFactory()

Dim table As New Table(GetType(Claim))

table.TableName = "Claims"

Dim field As New Field(table,"ClaimType","ClaimType","Detail")

table.Fields.Add(field)

db.Mapper.ObjectMap.Add(GetType(Claim),table)

 

Mapping with XML

  <Table>
    <MappingFromDatabase>false</MappingFromDatabase>
    <Schema />
    <ClassTypeString>KellermanSoftware.NetDataAccessLayerTests.TestClasses.Claim, NET-Data-Access-Layer-Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</ClassTypeString>
    <ClassName>Claim</ClassName>
    <TableName>Claims</TableName>
    <Fields>
      <Field>
        <ColumnName>ClaimType</ColumnName>
        <PropertyInfoString>ClaimType</PropertyInfoString>
        <FieldTypeString>System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</FieldTypeString>
        <DatabaseType>AnsiString</DatabaseType>
        <Unique>false</Unique>
        <AllowDbNull>false</AllowDbNull>
        <IsPrimaryKey>false</IsPrimaryKey>
        <KeyType xsi:nil="true" />
        <IsReadOnly>false</IsReadOnly>
        <Length xsi:nil="true" />
        <IsCompositeKey>false</IsCompositeKey>
        <IsMaintenanceField>false</IsMaintenanceField>
        <CompositionPropertyString>Detail</CompositionPropertyString>
      </Field>
    </Fields>
    <CompositeKeys />
  </Table>