title: How to manage (or eliminate) parallel class hierarchies?
body: <p>I'm designing a simple form creation engine, in which users may compose new forms using various boilerplate field types (Date, Text, DropDown)</p>
  
  <p>I decided to model the domain object (Form Field) independently of the objects that would be used to render these fields to a UI.</p>
  
  <p>Here's the interface that defines the contract for the domain and some of it's specializations:</p>
  
  <pre><code>namespace Acme.Core.Domain{
      public interface IFormField
              {
                  bool Visible { get; set; }
                  string Key { get; set; }
                  event EventHandler&lt;FieldVisibilityChangedEventArgs&gt; VisibilityChanged;
                  FieldType Type{get;}
                  void Validate(IEnumerable&lt;ValidationError&gt; errors);
                  int DataId {get;set;}
              }
  
      public interface IDropDownField:IFormField{
  
                  IDictionary&lt;string, string&gt; Items { get; set; }
                  KeyValuePair&lt;string, string&gt; SelectedValue { get; set; }
  
          }
       public interface IDateField:IFormField{
  
                  DateTime? SelectedDate{get;set}
  
          }
  }
  </code></pre>
  
  <p>For the UI side of things, I constructed a parallel type hierarchy.  This keeps the domain object, which is concerned with business rules around data validation separate from UI concerns, namely how to render a given field (MVC HtmlHelper vs WebForm WebControl):</p>
  
  <pre><code>namespace Acme.UI{
  
          public interface IControl 
          {
                  //parallel to IFormField
                  bool Visible { get; set; }
                  string ID { get; set; }     
          }
  
          public interface IDropListControl:IControl 
          {
                  //parallel to IDropDownField
  
          }
          public interface IDatePickerControl: IControl 
          {
                  //parallel to IDateField
  
          }
  
         public interface IControlFactory {
  
               IControl CreateControl(IFormField field);
         }
      }
  </code></pre>
  
  <p>While this design gives me the freedom to design the domain model independently of the UI, I haven't come across a clean way to connect and manage the two hierarchies. I feel like I should be able to leverage generics to connect parallel classes to one another, but I can't quite grok how that would look.  Is there a pattern that solves the association problem or eliminates the need for a parallel class hierarchies altogether?</p>
  
  <p>EDIT:  My UI tier references the my business layer (Core.csproj).  Here are a few examples of how I connect the UI class hierarchy to the domain class hierarchy. These types don't currently use generics, but I feel that they ought to.</p>
  
  <pre><code>    // create concrete instances of IControl based on the the domain object passed in 
     public interface IControlFactory {
  
                   IControl CreateControl(IFormField field);
         }
         // scrape values form the UI controls and apply them to the appropriate domain object
         public interface IFormFieldDataBinder{
  
                  void Bind(IFormField field, IControl control);
         }
  </code></pre>