title: StructureMap & Lazy Initialization via Constructor with Interfaces
body: <p>I use StructureMap v4.6.1.0 and I have a structure where I create an instance using the constructor into which I insert an interface of that class, and this class usually invokes my constructor that has its parameters in its service which they use</p>
  
  <pre><code> private readonly IFirstService _firstService;
   private readonly ISecondService _secondService;
  
   private readonly ILog _log;
  
   public ProductController(IFirstService firstService, ISecondService secondService, ILog log)
   {
         _firstService = firstService;
         _secondService = secondService;
  
         _log = log;
   }
  
   [Route("Default")]
   public ActionResult First()
   {
         var model = _firstService.DoIt();
         return View("~/Views/First/index.cshtml", model);
   }
  
   [Route("Default")]
   public ActionResult Second()
   {
         var model = _secondService.DoIt();
         return View("~/Views/Second/index.cshtml", model);
   }
  </code></pre>
  
  <p>Main problem of this solution is then I call Controller then it makes a 2 instances (one for firstService and second for secondService), but this services I call for specific controller page method. </p>
  
  <p>For example, service call in constructor interface for the factory, log and repository loader, thas mean when I call controller constructor I load all repositories from both services-</p>
  
  <ul>
  <li>Can I use C#.NET Lazy(T) or Func?</li>
  <li>Can I use interface as a parameter in the method for the selected page?</li>
  <li>Can I use Cache for read-only data from the repository?</li>
  <li>Another solution?</li>
  </ul>
  
  <p>When I use Lazy I got the message then called procedure is not defined</p>
  
  <p>I am looking for best architecture solution, I try some Lazy and also code optimalization, but I have always encountered a problem</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>StructureMap Container Registration</p>
  
  <pre><code>Scan(
      scan =&gt;
      {
             scan.TheCallingAssembly();
             scan.WithDefaultConventions();
             scan.With(new ControllerConvention());
      });
  
  For&lt;ILog&gt;().Use(c =&gt; LogManager.GetLogger(GetType())).Singleton();
  
  For&lt;IFirstService&gt;().Use&lt;FirstService&gt;().Singleton();
  For&lt;ISecondService&gt;().Use&lt;SecondService&gt;().Singleton();
  </code></pre>
  
  <p><strong>My Solution:</strong></p>
  
  <ul>
  <li>Can I use interface as parameter in method for the selected page?</li>
  </ul>
  
  <p>In class constructor I use interface of StructureMap </p>
  
  <pre><code>private readonly IContainer _container;
  private readonly ILog _log;
  
  public ProductController(IContainer container, ILog log)
  {
         _container = container;
         _log = log;
  }
  </code></pre>
  
  <p>And in the method I use</p>
  
  <pre><code>var model = _container.GetInstance&lt;IFirstService&gt;().DoIt();
  </code></pre>
  
  <ul>
  <li>Can I use Cache for read-only data from the repository?</li>
  </ul>
  
  <p>I use .NET lib <em>using static System.Web.HttpRuntime;</em> and use code below in method called in the constructor of the repository class</p>
  
  <pre><code>if (!(Cache[_cacheName] is IEnumerable&lt;YourObject&gt; result)) // Cache is empty
              {
                  _log.Info("-- Loading from DB --");
                  lock (CacheLockObject)
                  {
                      result = Cache[_cacheName] as IEnumerable&lt;YourObject&gt;;
                      if (result == null)
                      {
                          result = LoadAll(); // load data from DB
                          Cache.Insert(_cacheName, result, null,
                              DateTime.Now.AddMinutes(10), TimeSpan.Zero);
                      }
  
                      return result;
                  }
              }
  
              _log.Info("-- Loading from Cache --");
              return result;
  </code></pre>
  
  <p>Thank you</p>