title: AutoMapper Map to different type based on an enum?
body: <p>I'm starting to implement <a href="https://github.com/AutoMapper/AutoMapper" rel="noreferrer">AutoMapper</a>, first I managed to integrate it with Castle.Windsor, which I'm already using. Now I have a <code>Post</code> entity which I want to map to either a <code>LinkPostModel</code> or an <code>ImagePostModel</code>. Both inherit from <code>PostModel</code></p>
  
  <p><strong>1)</strong> This is what I have so far:</p>
  
  <pre><code>public class PostModelFromPostEntityConverter : ITypeConverter&lt;Post, PostModel&gt;
  {
      private readonly IPostService postService;
  
      public PostModelFromPostEntityConverter(IPostService postService)
      {
          if (postService == null)
          {
              throw new ArgumentNullException("postService");
          }
          this.postService = postService;
      }
  
      public PostModel Convert(ResolutionContext context)
      {
          Post post = (Post)context.SourceValue;
          Link link = post.Link;
          if (link.Type == LinkType.Html)
          {
              return new LinkPostModel
              {
                  Description = link.Description,
                  PictureUrl = link.Picture,
                  PostId = post.Id,
                  PostSlug = postService.GetTitleSlug(post),
                  Timestamp = post.Created,
                  Title = link.Title,
                  UserMessage = post.UserMessage,
                  UserDisplayName = post.User.DisplayName
              };
          }
          else if (link.Type == LinkType.Image)
          {
              return new ImagePostModel
              {
                  PictureUrl = link.Picture,
                  PostId = post.Id,
                  PostSlug = postService.GetTitleSlug(post),
                  Timestamp = post.Created,
                  UserMessage = post.UserMessage,
                  UserDisplayName = post.User.DisplayName
              };
          }
          return null;
      }
  }
  </code></pre>
  
  <p>Obviously the point in implementing <a href="https://github.com/AutoMapper/AutoMapper" rel="noreferrer">AutoMapper</a> is removing repeat code like this, so how am I supposed to map the common stuff, before adding my custom rules (such as the if-clause)</p>
  
  <p>Ideally I'd want this to be something like:</p>
  
  <pre><code>public class PostModelFromPostEntityConverter : ITypeConverter&lt;Post, PostModel&gt;
  {
      [...]
  
      public PostModel Convert(ResolutionContext context)
      {
          Post post = (Post)context.SourceValue;
          Link link = post.Link;
          if (link.Type == LinkType.Html)
          {
              return Mapper.Map&lt;Post, LinkPostModel&gt;(post);
              // and a few ForMember calls?
          }
          else if (link.Type == LinkType.Image)
          {
              return Mapper.Map&lt;Post, ImagePostModel&gt;(post);
              // and a few ForMember calls?
          }
          return null;
      }
  }
  </code></pre>
  
  <p><strong>2)</strong> After this mapping is complete. I have a "parent" mapping, where I need to map an <code>IEnumerable&lt;Post&gt;</code> the following model:</p>
  
  <pre><code>public class PostListModel : IHasOpenGraphMetadata
  {
      public OpenGraphModel OpenGraph { get; set; } // og:model just describes the latest post
      public IList&lt;PostModel&gt; Posts { get; set; }
  }
  </code></pre>
  
  <p>So basically I'd need another <code>TypeConverter</code> <em>(right?)</em>, which allows me to map the posts list first, and then create the <code>og:model</code></p>
  
  <p>I have this, but it feels kind of clunky, I feel it could be better:</p>
  
  <pre><code>public class PostListModelFromPostEntityEnumerableConverter : ITypeConverter&lt;IEnumerable&lt;Post&gt;, PostListModel&gt;
  {
      public PostListModel Convert(ResolutionContext context)
      {
          IEnumerable&lt;Post&gt; posts = (IEnumerable&lt;Post&gt;)context.SourceValue;
          PostListModel result = new PostListModel
          {
              Posts = posts.Select(Mapper.Map&lt;Post, PostModel&gt;).ToList()
          };
          Post first = posts.FirstOrDefault();
          result.OpenGraph = Mapper.Map&lt;Post, OpenGraphModel&gt;(first);
          return result;
      }
  }
  </code></pre>
  
  <p><strong>3)</strong> I didn't actually run the code yet, so another question comes to mind, and that is why aren't mappings strongly typed in converters?</p>
  
  <pre><code>IEnumerable&lt;Post&gt; posts = (IEnumerable&lt;Post&gt;)context.SourceValue;
  </code></pre>
  
  <p>where it could actually be</p>
  
  <pre><code>IEnumerable&lt;Post&gt; posts = context.SourceValue;
  </code></pre>