title: ASP.NET MVC - Mapping with Automapper
body: <p>I'm currently trying to figure out when to use ViewModels and when not to. I'm using Automapper for the task and currently have the following code:</p>
  
  <p>// AccountController.cs</p>
  
  <pre><code>[AuthWhereRole(Roles = AuthorizeRole.Developer)]
  public ActionResult List()
  {
      MembershipUserCollection users = _memberShipService.GetAllUsers();
      IEnumerable&lt;ListUsersViewModel&gt; viewModel =
              Mapper.Map&lt;IEnumerable&lt;MembershipUser&gt;, IEnumerable&lt;ListUsersViewModel&gt;&gt;(users.Cast&lt;MembershipUser&gt;().AsEnumerable());
  
      return View("List", viewModel);
  }
  </code></pre>
  
  <p>// ListUsersViewModel.cs</p>
  
  <pre><code>public class ListUsersViewModel
  {
      public Guid Id { get; set; }
      public virtual string UserName { get; set; }
      public string LastLogOn { get; set; }
  }
  </code></pre>
  
  <p>// Bootstrapper.cs</p>
  
  <pre><code>public static void ConfigureAutoMapper()
  {
      Mapper.CreateMap&lt;MembershipUser, ListUsersViewModel&gt;()
              .ForMember(x =&gt; x.UserName, o =&gt; o.MapFrom(s =&gt; s.UserName))
              .ForMember(x =&gt; x.Id, o =&gt; o.MapFrom(s =&gt; s.ProviderUserKey))
              .ForMember(x =&gt; x.LastLogOn, o =&gt; o.MapFrom(s =&gt; s.LastLoginDate));
  }
  </code></pre>
  
  <p>I'm wondering whether its bad practice to map like this just to exclude some properties from the domain model? - And should I always use View Models, even when not needed? </p>
  
  <p>Thanks in advance.</p>