title: DryIoc and ExpressMapper
body: <p>I'm using DryIoc and ExpressMapper.</p>
  
  <p>I wrap ExpressMapper inside another class and at some point it should be use to map to a type with a parametrized constructor. Actually it's when I map a view model to a business model. The parameter is an instance of a repository class.</p>
  
  <p>Using TDD, my Mapper class cended up like this (note the constructor accepting a Func).</p>
  
  <pre><code>public class Mapper&lt;T1, T2&gt; : IMapper&lt;T1, T2&gt;
  {
      private readonly MappingServiceProvider _mapper;
  
      public Mapper()
      {
          _mapper = new MappingServiceProvider();
          _mapper.Register&lt;T1, T2&gt;();
      }
  
      public Mapper(Func&lt;T2&gt; func)
      {
          _mapper = new MappingServiceProvider();
          _mapper.Register&lt;T1, T2&gt;().Instantiate((t1) =&gt; func());
      }
  
      public T2 Map(T1 t)
      {
          return _mapper.Map&lt;T1, T2&gt;(t);
      }
  }
  </code></pre>
  
  <p>Thus I tried to use the Ioc like this :</p>
  
  <pre><code>        Func&lt;IActivitiesModel&gt;factoryActivitiesModel = () =&gt; container.Resolve&lt;IActivitiesModel&gt;();
          container.Register(Made.Of(() =&gt; factoryActivitiesModel));
          // () =&gt; new ActivitiesModel(container.Resolve&lt;IActivityRepository&gt;(IfUnresolved.Throw)
          container.Register&lt;IMapper&lt;ActivitiesViewModel, IActivitiesModel&gt;, Mapper&lt;ActivitiesViewModel, IActivitiesModel&gt;&gt;(Reuse.Singleton, Made.Of(
              () =&gt; new Mapper&lt;ActivitiesViewModel, IActivitiesModel&gt;(Arg.Of&lt;Func&lt;IActivitiesModel&gt;&gt;())
          ));
  </code></pre>
  
  <p>But it didn't do.</p>
  
  <pre><code>Unable to use null factory object with factory method ActivitiesMVC.Ioc.&lt;&gt;c__DisplayClass4_0::System.Func`1[ActivitiesLogic.Models.IActivitiesModel] factoryActivitiesModel when resolving: Func&lt;ActivitiesLogic.Models.IActivitiesModel&gt;.
  </code></pre>
  
  <p>Anyway, I tried different approaches afterward but none seemed to work.</p>
  
  <p>I'd like to avoid to store the container as a static singleton and having the mapper relies on it (I'd like the mapper to remain Ioc agnostic).</p>
  
  <p>How can it be achieved?</p>
  
  <p>EDIt : current solution (with bad static singleton)</p>
  
  <pre><code>public class Ioc
  {
      public class Factory&lt;T&gt; : IFactory&lt;T&gt;
      {
          public T Create()
          {
              return Container.Resolve&lt;T&gt;();
          }
      }
  
      private static Lazy&lt;Container&gt; _container;
  
      public static Container Container =&gt; _container.Value;
  
      static Ioc()
      {
          _container = new Lazy&lt;Container&gt;(GetContainer);
      }
  
      static private Container GetContainer()
      {
          var container = new Container(rules =&gt; rules
              //.WithoutThrowOnRegisteringDisposableTransient()
              .WithTrackingDisposableTransients());
          container.Register(Made.Of(() =&gt; new ActivityController(Arg.Of&lt;IMapper&lt;ActivitiesViewModel, IActivitiesModel&gt;&gt;())));
          container.Register&lt;ILabContext, LabContext&gt;(new SingletonReuse());
          container.Register&lt;IActivityRepository, ActivityRepository&gt;(new SingletonReuse());
          container.Register&lt;IActivitiesModel&gt;(made: Made.Of(() =&gt; new ActivitiesModel(Arg.Of&lt;IActivityRepository&gt;())));
          container.Register(typeof(IFactory&lt;&gt;), typeof(Factory&lt;&gt;), new SingletonReuse());
          container.Register&lt;IMapper&lt;ActivitiesViewModel, IActivitiesModel&gt;, Mapper&lt;ActivitiesViewModel, IActivitiesModel&gt;&gt;(Reuse.Singleton, Made.Of(
              () =&gt; new Mapper&lt;ActivitiesViewModel, IActivitiesModel&gt;(Arg.Of&lt;IFactory&lt;IActivitiesModel&gt;&gt;())
          ));
          container.Register&lt;IMapper&lt;ActivitiesModel, ActivitiesEntity&gt;, Mapper&lt;ActivitiesModel, ActivitiesEntity&gt;&gt;(new SingletonReuse(), Made.Of(
              () =&gt; new Mapper&lt;ActivitiesModel, ActivitiesEntity&gt;()
          ));
  
          return container;
          ;
      }
  }
  </code></pre>