title: NHibernate: Mapping members that are instances of unmapped classes
body: <p>I am working on a program that uses NHibernate and I have the following problem:
  Some of my classes have members which are instances of classes that basically just define some handlers.</p>
  
  <p>For example</p>
  
  <pre><code>public class Foo
  {
      virtual public ResultHandler handler {get; set;}
      virtual void Bar()
      {
         ...
         handler.HandleResult(...);
      }
  }
  
  public abstract class ResultHandler
  {
      abstract public void HandleResult(...);
  }
  
  public class ResultPrint : ResultHandler
  {
      override public void HandleResult(...)
      {
          ...
      }
  }
  
  public class ResultExport : ResultHandler
  {
      override public void HandleResult(...)
      {
          ...
      }
  }
  </code></pre>
  
  <p>Now ResultPrint and ResultExport don't have any members, so they don't have to be mapped to tables in the database, but when I store / retrieve an instance of Foo, I want to somehow store &amp; load the member "handler".</p>
  
  <p>I could store the class name in a string and use reflection to load it (but that would be unstable in case of renaming of classes and it would waste space in the database). It would be sufficient to store a single byte - but how do I tell NHibernate to do that?</p>
  
  <p>The best I can come up with is to map a virtual member like</p>
  
  <pre><code>virtual public byte HandlerClass
  {
      get
      {
          if(handler == null)
              return 0;
          switch(handler.GetType())
          {
              case typeof(ResultPrint):
                  return 1;
              ... 
          }
      }
      set
      {
          switch(value)
          {
              case 0:
                  handler = null;
                  break;
              case 1:
                  handler = new ResultPrint();
                  break;
              ...
          }
      }
  }
  </code></pre>
  
  <p>but that is absurdly complicated for something that should be a common use case...
  There has to be a "clean" way to make NHibernate to that on its own.
  Can anyone help me?</p>
  
  <p>Kind regards</p>
  
  <p>AlgoMan</p>
  
  <p>EDIT: I am using XML-based mapping btw.</p>