title: Manipulator Factory
body: <p><strong>EDIT4 Gave Truck a property to show the SetNrOfWheels method is tailored to a specific Truck</strong></p>
  
  <p><strong>EDIT3 Made a connection between B - Vehicle and C1 - Truck</strong></p>
  
  <p><strong>EDIT2 Removed interface on B</strong></p>
  
  <p><strong>EDIT1 Changed Car into Vehicle</strong></p>
  
  <p>I have two questions about a problem I ran into several times. I wonder if I must use another pattern, or if someone could point me in the right/better direction.</p>
  
  <p>If you know an article here describing the answer, please let me know and I will try to delete this one.</p>
  
  <p>Consider an abstract base class B (i.e. Vehicle), which is inherited by a number of child classes, C1, C2, etc. (i.e. Truck, Motor) Suppose these classes are pure data carriers, i.e. data contracts, and consist of a number of properties. Although not strictly necessary for the story, they use a default constructor without arguments and all properties are public, and simple value type objects. They may or may not inherit some predefined properties, but most importantly, B is more or less a marker, rather than it enforces functionality (since it is a data contract).</p>
  
  <p>Example:</p>
  
  <pre><code>abstract Vehicle {
    int NrOfWheels { get; set;}
  }
  
  Truck : Vehicle {
    bool HasExtraTrailer { get; set; }
  }
  </code></pre>
  
  <p>Now consider a manipulator of B called BM (i.e. VehicleWheelSetter), which is either an abstract base class or an interface (you decide what's best here ;)). It has (at least abstract) functionality to manipulate B classes. Suppose each manipulator, i.e. C1M, C2M, (TruckWheelSetter, MotorWheelSetter) inherits from BM. Important is, that the type is enforced, ergo, C1M only manipulates C1 type of classes. This could be enforced by BM being generic, say BM&lt;T&gt; where T : B, although this is limited (this would allow for an XM : BM&lt;B&gt; still)</p>
  
  <p>Example:</p>
  
  <pre><code>interface IVehicleWheelSetter&lt;in T&gt; where T : Vehicle {
    void SetNrOfWheels(T vehicle)
  }
  
  TruckWheelSetter : IVehicleWheelSetter&lt;Truck&gt; {
    void SetNrOfWheels(Truck vehicle) { 
      vehicle.NrOfWheels = 2 * 3;
      if (vehicle.HasExtraTrailer) vehicle.NrOfWheels += 2 * 2;
  } } }
  </code></pre>
  
  <p><strong>Question 1: Is there a way to make sure a concrete BM (i.e. C1M) is always manipulating a concrete B (in this case C1)? Or does this require another pattern?</strong></p>
  
  <p>Let's assume that in general, this will be much more complex then assigning a number (which could obviously be retrieved from a configuration or database). It's not a coincidence I wrote 2 * 3, to show that this could be a complex operation.</p>
  
  <p>Suppose I know I get B classes all the time in some service that has to do a manipulation, like a factory, something like this:</p>
  
  <pre><code>VehicleWheelSetterFactory {
    IVehicleWheelSetter&lt;T&gt; CreateVehicleWheelSetter&lt;T&gt;() where T : Vehicle {
      // code to create the right selector here
  } }
  </code></pre>
  
  <p>Now, there are various ways to do this:</p>
  
  <pre><code>if (typeof(T) == typeof(Truck)) 
    return (IVehicleWheelSetter&lt;T&gt;)(new TruckWheelSetter());
  throw new NotImplementedException();
  </code></pre>
  
  <p>or some more advanced reflection based options. </p>
  
  <p>There are some problems with this, aside from some 'it does not feel right'. There will also be cases in which a generic does not work, i.e. T is unknown. In that case, a Type in the argument could help:</p>
  
  <pre><code>object CreateVehicleWheelSetter(Type vehicleType) { 
    if (vehicleType == typeof(Truck)) 
      return new TruckWheelSetter();
    throw new NotImplementedException();
  }
  </code></pre>
  
  <p>but how to cast the object to an IVehicleWheelSetter&lt;T&gt;? T is still unknown! So, maybe the interface should be this:</p>
  
  <pre><code>interface ICarWheelSetter
  {
      void SetNrOfWheels(Vehicle vehicle);
  }
  </code></pre>
  
  <p>But now I have the original problem: I cannot guarantee the Vehicle is a Truck in TruckWheelSetter. This also not be resolved this way:</p>
  
  <pre><code>interface IVehicleWheelSetter
  {
      void SetNrOfWheels&lt;T&gt;(T car) where T : Vehicle;
  }
  </code></pre>
  
  <p><strong>Question 2: Is there a better way to do this?</strong></p>