title: How to pass value to a custom ValueResolver?
body: <p>I'm struggling with the format for passing in a value to a custom ValueResolver. I am basically trying to convert an Int to and Object based on my ViewModel. However, I cannot get it to pass it the value to my ValueResolver</p>
  
  <p>I have the following Custom ValueResolver:</p>
  
  <pre><code>public class CustomResolver : ValueResolver&lt;CreateContainerViewModel, Container&gt;
  { 
      private int id = 0;
  
      public CustomResolver(int? sourceId)
      {
          if(sourceId.HasValue)
              id = sourceId.Value;
      }
      protected override Container ResolveCore(CreateContainerViewModel source)
      {
          if (id == 0) return null;
          else
          {
              ISession _session = DependencyResolver.Current.GetService&lt;ISessionFactory&gt;().GetCurrentSession();
              return _session.Get&lt;Container&gt;(id);
          }
      }
  }
  </code></pre>
  
  <p>It works fine when I create the map using a hard coded int like the following code</p>
  
  <pre><code>Mapper.CreateMap&lt;CreateContainerViewModel, Container&gt;()
  .ForMember(a =&gt; a.CurrentContainer, opt =&gt; opt.ResolveUsing(src =&gt; new CustomResolver(33)));
  </code></pre>
  
  <p>but it isn't working (throwing mapping exception) when I try and get the value from my viewmodel</p>
  
  <pre><code>Mapper.CreateMap&lt;CreateContainerViewModel, Container&gt;()
              .ForMember(dest =&gt; dest.CurrentContainer, opt =&gt; opt.MapFrom(src =&gt; new CustomResolver(src.CurrentContainerId)));
  </code></pre>
  
  <p>Can anyone point me in the direction of the correct syntax for passing in a value to a custom ValueResolver?</p>
  
  <p>Thanks</p>
  
  <p>UPDATE:</p>
  
  <p>basically trying to write something like this:</p>
  
  <pre><code>  public class IdToObjectResolver&lt;T&gt; : ValueResolver&lt;int, T&gt; where T : class
  { 
      private ISession _session;
      private int id;
      public IdToObjectResolver(Nullable&lt;int&gt; sourceId)
      {
          _session = DependencyResolver.Current.GetService&lt;ISessionFactory&gt;().GetCurrentSession();
          if(sourceId.HasValue)
              id = sourceId.Value;
      }
  
      protected override T ResolveCore(int source)
      {
          return _session.Get&lt;T&gt;(source);
      }
  }
  </code></pre>
  
  <p>This code doesnt currently make sense, but just trying to get a way of passing in the int...</p>