TechTalk Genome v4.2

<TypeDiscriminator> Element

Declares that the dynamic type for instances of any classes in the type family is determined by the value of a persistent field.

<Type name=" type ">
<TypeDiscriminator memberName=" property " />
</Type>
type
A root class of a type family.
property
A persistent property of a scalar (including Enum) type.

Remarks

The <TypeDiscriminator> element stores dynamic type information for all types in the type family in the property. The value of the property (called discriminator value) determines the type for the stored instances of the types in the type family. The possible values of property have to be set on each individual type by using the <Discriminator> element . For types without concrete instances, it is unnecessary to provide discriminator values. The discriminator values must be unique within the type family.

The property has to be declared on the type and mapped using <PersistentField> element .

The developer must set the value of the property in the constructor for every type in the type family. The dynamic type of an instance is fixed for its entire lifetime. A type can be identified by one or more type discriminator values. Usually only one discriminator value is defined for a type, however, when mapping existing database structures, it can be necessary to have several possible discriminator values that identify the same type.

Examples

The type information of the type BusinessContact is stored in the TypeId field in the database. The type of the TypeId property is an Enum to make the code more readable.

          
BusinessContact.cs    
                    
public enum BusinessContactTypes { BusinessContact = 0, Supplier = 1 }

public abstract class BusinessContact : Persistent 
{
    ...
    public abstract BusinessContactTypes TypeId { get; set; }
    
    public BusinessContact(ctor params) 
    {
        ...
        TypeId = BusinessContactTypes.BusinessContact;
    }
    ...
}

public abstract class Supplier : BusinessContact 
}
    public Supplier(ctor params) 
    {
        ...
        TypeId = BusinessContactTypes.Supplier;
    }
}
                
          
NorthwindSchema.xml    
                    
<Type name="BusinessContact">
    <RootInheritance />
    
    <TypeDiscriminator memberName="TypeId" />
        <!-- the <TypeDiscriminator> element is specified only on the root class -->
        
    <Discriminator value="BusinessContact" />
        <!-- the BusinessContact type has concrete instances - the 
             BusinessContactTypes.BusinessContact (0) will represent 
             the BusinessContact instances -->

    ...
    <Member name="TypeId"><PersistentFiled/></Member>

</Type>

<Type name="Supplier">
    <JoinedInheritance />
    <Discriminator value="Supplier" />
        <!-- the BusinessContactTypes.Supplier (1) will represent 
             the Supplier instances -->

    ...
</Type>
                

Requirements

Type: TechTalk.Genome.Mapping.TypeDiscriminatorXmlData

Assembly: TechTalk.Genome.dll

Version: 4.2.11

Editions: Professional, Evaluation, Express

Database Platforms: Microsoft SQL Server 2000, Microsoft SQL Server 2005, Oracle 9i Release 2, Oracle 10g Release 2

See Also

Type Identity Providers | <Discriminator> Element