title: AutoMapper: Int to String and back again
body: <p>EDIT: to include TypeConverter</p>
  
  <p>To set the stage, I am stripping out code from a existing WCF service to put into a business object (BO) that will be referenced by the WCF to provide clients with information.  A requirement is to make the employeeId of an Employee object to be an integer rather than the string currently used.  I'm using AutoMapper to map all of the objects between the BO and WCF so that contracts do not break.  However I'm struggling with how to provide the mapping back and forth for the EmployeeId to be an integer in BO and still contractually serve strings via the WCF.</p>
  
  <p>BO</p>
  
  <pre><code>public class Employee 
  {
      public int Id {get; set;}
      ......
  }
  </code></pre>
  
  <p>WCF</p>
  
  <pre><code>[DataContract(Name = "Employee", Namespace="url")]
  public class Employee
  {
      [DataMember(Order = 1)]
      public string Id {get; set;}
      ......
  }
  </code></pre>
  
  <p>Mapper</p>
  
  <pre><code>Mapper.CreateMap&lt;Employee.Entity.Employee, Employee.Service.Entity.Employee&gt;()
  </code></pre>
  
  <p>PaddedStringTypeConverter class:</p>
  
  <pre><code>public class PaddedStringTypeConverter : ITypeConverter&lt;int, string&gt;
  {
      public string Convert(ResolutionContext context)
      {
          var sourceValue = System.Convert.ToInt32(context.SourceValue);
          return sourceValue.ToString("D9");
      }
  }
  </code></pre>
  
  <p>I've seen that I could use Custom Type Converters in AutoMapper to change the BO from an integer to our fixed length of nine characters so that an integer value of 4610 would be equivalent to a string of "000004610".  However, how do I get it back to an integer value.</p>
  
  <p>How would you do this?</p>