Knight Data Access Layer
Composition
Basic Tasks C# > Mapping > Composition

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 int ClaimId { get; set; }

   public DateTime ClaimDate { get; set; }

   [Composition]

   public ClaimDetail Detail { get; set; }

}

 

public class ClaimDetail

{

   public string ClaimType { get; set; }

}

 

Mapping with Code

IDataHelper db = DataHelper.SessionFactory();

Table table = new Table(typeof(Claim));

table.TableName = "Claims";

Field field = new Field(table,"ClaimType","ClaimType","Detail");

table.Fields.Add(field);

db.Mapper.ObjectMap.Add(typeof(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>