Try fast search NHibernate

04 November 2010

ConfORM: understanding polymorphism (interfaces)

The Domain

ConfORMInterfacePolymorphisDemo
Three entities and two components some ones implementing the same interface.

The mapping

  1. var entities = new[] { typeof(User), typeof(Email), typeof(InstantMessage) };
  2. var orm = new ObjectRelationalMapper();
  3. orm.TablePerClass(entities);
  4.  
  5. var mapper = new Mapper(orm);
  6. mapper.Customize<IHasMessage>(x => x.Property(hasMessage => hasMessage.Message, pm => { pm.Type(NHibernateUtil.StringClob); pm.Lazy(true); }));
  7. mapper.Customize<Tweet>(x => x.Property(tweet => tweet.Message, pm => { pm.Type(NHibernateUtil.String); pm.Length(140); pm.Lazy(false); }));
  8.  
  9. var mapping = mapper.CompileMappingFor(entities);
At line 6 you can see the map of the property Message of the interface.
At line 7 you can see exception of the above rule only for the class Twett.

Where is the exception not of a convention but of the persistent representation of the property of an interface is clear, no ?

The XML

If you analyze the below XML you will see the ConfORM’s capability to understand the polymorphism.
<class name="User">
  <id name="Id" type="Int32">
    <generator class="hilo" />
  </id>
  <property name="Name" />
  <bag name="WelcomeMessage">
    <key column="user_key" />
    <composite-element class="UserWelcomeMessage">
      <property name="ForDate" />
      <property name="Message" type="StringClob" lazy="true" />
    </composite-element>
  </bag>
  <bag name="Tweets">
    <key column="user_key" />
    <composite-element class="Tweet">
      <property name="Message" type="String" length="140" />
    </composite-element>
  </bag>
</class>
<class name="Email">
  <id name="Id" type="Int32">
    <generator class="hilo" />
  </id>
  <property name="To" />
  <property name="Cc" />
  <property name="Message" type="StringClob" lazy="true" />
</class>
<class name="InstantMessage">
  <id name="Id" type="Int32">
    <generator class="hilo" />
  </id>
  <property name="To" />
  <property name="Message" type="StringClob" lazy="true" />
</class>


Are you ConfORM ?

P.S. Now is time to work in another level of polymorphism between class-interfaces relations.

No comments:

Post a Comment