title: WCF decoupling via ViewModel facade ASP.NET
body: <p>I am consuming several ASP.NET 4, WCF services in C# and want to know the best way of decoupling my code from the WCF structure. If this is not too much work as the client probably doesn't care.</p>
  
  <p>I created a facade layer so my WebForms UI can talk to it using flat simple types like string, int and datetime. The facade generates and read the Request/Response objects required by the WCF. I read the Response object and convert it into a simpler Presentation object for the client to use. However the Response object returns some complex object types, such as OrderHistory which contains OrderItem, which in itself contains for e.g. DeliveryAddress. </p>
  
  <p>Now should I be recreating all these complex objects as Views? i.e. OrderPresentation contains IEnumerable-OrderHistoryView-, which contains IEnumerable-OrderItemView-, which contains DeliveryAddressView or am I misinterpreting this? </p>
  
  <p>For some of the simpler objects I have been able to use AutoMapper but not sure how or if I should apply it to nested complex objects. Maybe I should not bother decoupling so much and just bind my facade and presentation objects to the WCF model?</p>
  
  <p>Help please? any good tutorial examples on this?</p>
  
  <p>I'm getting somewhere with:</p>
  
  <pre><code>        public static IList&lt;OrderHistoryView&gt; ConvertToOrderHistoryView(this OrderHistory[] orderHistory)
      {
          return Mapper.Map&lt;OrderHistory[], IList&lt;OrderHistoryView&gt;&gt;(orderHistory);
      }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>            Mapper.CreateMap&lt;OrderHistory, OrderHistoryView&gt;()
              .ForMember(dest =&gt; dest.Items, opt =&gt; opt.MapFrom(src =&gt; src.Items));
  </code></pre>