title: Need to speed up automapper...It takes 32 seconds to do 113 objects
body: <p>Hi I have some major problems with auto mapper and it being slow. I am not sure how to speed it up.</p>
  
  <p>I am using nhibernate,fluent nhibernate and asp.net mvc 3.0</p>
  
  <pre><code>[Serializable()]
      public class Test
      {
          public virtual int Id { get; private set; }
          public virtual string Name { get;  set; }
          public virtual string Description { get; set; }
          public virtual DateTimeDate { get; set; }
          public virtual IList&lt;Reminder&gt; Reminders { get; set; }
          public virtual IList&lt;Reminder2&gt; Reminders2 { get; set; }
          public virtual Test2 Test2 { get; set; }
  
          public Test()
          {
              Reminders = new List&lt;Reminders&gt;();
              Reminders2 = new List&lt;Reminders2&gt;();
          }
  
      }
  </code></pre>
  
  <p>So as you can see I got some properties, Some other classes as in my database I have references between them.</p>
  
  <p>I then do this</p>
  
  <pre><code>var a = // get all items (returns a collection of Test2)
  var List&lt;MyViewModel&gt; collection = new List&lt;MyViewModel&gt;();
       foreach (Test2 t in a)
              {
                  MyViewModel vm = Mapper.Map&lt;Test2, MyViewModel&gt;(t);
                  vm.SetDateFormat(t.DateTimeDate, DateFilters.All.ToString());
  
                  collection.Add(vm);
              }
  </code></pre>
  
  <p>// view model</p>
  
  <pre><code>    public class MyViewModel
          {
              public int Id  { get; private set; }
              public string Name { get; set; }
              public string Description { get; set; }
              public DateTime DateTimeDate { get; set; }
              public string FormatedDueDate { get; set; }
              public string Test2Prefix { get; set; }
              public string Test2BackgroundColor { get; set; }
              public string SelectedDateFilter { get; set; }
              public bool DescState { get; set; }
              public bool AlertState { get; set; }
  
  
              /// &lt;summary&gt;
              /// Constructor
              /// &lt;/summary&gt;
              public MyViewModel()
              {
                  // Default values
                  SelectedDateFilter = "All";
                  DescState = false;
                  AlertState = false;
              }
  
              /// &lt;summary&gt;
              /// Sets the date formatter string used
              /// &lt;/summary&gt;
              /// &lt;param name="dateFormat"&gt;&lt;/param&gt;
              public void SetDateFormat(DateTime dueDate, string dateFilter)
              {
                  // simple if statement to format date.
              }
          }
  </code></pre>
  
  <p>// mapping</p>
  
  <pre><code>  Mapper.CreateMap&lt;Test2,MyViewModel&gt;().ForMember(dest =&gt; dest.DescState, opt =&gt;
   opt.ResolveUsing&lt;DescStateResolver&gt;())
                   .ForMember(dest =&gt; dest.AlertState, opt =&gt;
   opt.ResolveUsing&lt;AlertStateResolver&gt;());
  </code></pre>
  
  <p>// resolvers</p>
  
  <pre><code>public class AlertStateResolver : ValueResolver&lt;Task, bool&gt;
      {
          protected override bool ResolveCore(Task source)
          {
              if (source.Reminders.Count &gt; 0 || source.Reminders2.Count &gt; 0)
              {
                  return true;
              }
              else
              {
                  return false;
              }
          }
      }   
  
    public class DescStateResolver : ValueResolver&lt;Task,bool&gt;
      {
          protected override bool ResolveCore(Task source)
          {
              if (String.IsNullOrEmpty(source.Description))
              {
                  return false;
              }
              else
              {
                  return true;
              }
          }
      }
  </code></pre>
  
  <p>Ignore the weird names and any typos my real object works just fine and makes sense.</p>
  
  <p>So I used the stop watch and did this</p>
  
  <pre><code>Stopwatch a = new Stopwatch()
      foreach (Test2 t in a)
                  {
                      a.Start()                     
                      MyViewModel vm = Mapper.Map&lt;Test2, MyViewModel&gt;(t);
                      a.Stop()
                      vm.SetDateFormat(t.DateTimeDate, DateFilters.All.ToString());
  
                      collection.Add(vm);
                  }
  
  var b = a.Elapsed; // comes back with 32 seconds.
  </code></pre>
  
  <p>I need to optimized this very badly.</p>