title: ASP.NET MVC Autofac Singleton
body: <p>I am using autofac with ASP.NET MVC, using the Repository Pattern.
  I have a class like this</p>
  
  <pre><code> public class IoC : ContainerBuilder
  {
  
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      private readonly static IoC _instance = new IoC();
  
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      private static object _lock;
  
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      private IContainer _componentsContainer;
  
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      public static IoC Instance
      {
          get
          {
              return _instance;
          }
      }
  
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public IContainer GetComponentsContainer()
      {
          if (_componentsContainer == null)
          {
              lock (_lock)
              {
                  if (_componentsContainer == null)
                      _componentsContainer = this.Build();
              }
          }
  
          return _componentsContainer;
      }
  
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public T Resolve&lt;T&gt;() where T : class
      {
          return GetComponentsContainer().Resolve&lt;T&gt;();
      }
  
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public ILifetimeScope BeginLifetimeScope()
      {
          return GetComponentsContainer().BeginLifetimeScope();
      }
  
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      private IoC()
      {
          _lock = new object();
          ConfigureDependencies();
      }
  
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      private void ConfigureDependencies()
      {
          //Configure all your depedendencies here!!
  
          //Database connection
          var connectionString = ConfigurationManager.ConnectionStrings["Pixie"].ConnectionString;
          this.Register(c =&gt; new SqlConnection(connectionString)).As&lt;IDbConnection&gt;().InstancePerLifetimeScope();
          //Database Connection OrmLite
          OrmLiteConfig.DialectProvider = SqlServerDialect.Provider;
          //Repository
          this.RegisterType&lt;Repository&gt;().As&lt;IRepository&gt;().InstancePerLifetimeScope();
      }
  
  }
  </code></pre>
  
  <p>And in my Application_Start method in Global.asax I have the following</p>
  
  <pre><code>protected void Application_Start()
          {
              //var builder = new ContainerBuilder();
              AreaRegistration.RegisterAllAreas();
              FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
              RouteConfig.RegisterRoutes(RouteTable.Routes);
              BundleConfig.RegisterBundles(BundleTable.Bundles);
              // Register your MVC controllers.
               IoC.Instance.RegisterControllers(typeof(MvcApplication).Assembly);
                IoC.Instance.RegisterModelBinders(Assembly.GetExecutingAssembly());
              IoC.Instance.RegisterModelBinderProvider();
              IoC.Instance.RegisterModule&lt;AutofacWebTypesModule&gt;();
              IoC.Instance.RegisterModule&lt;AutofacWebTypesModule&gt;();
              IoC.Instance.RegisterSource(new ViewRegistrationSource());
  
  
              var MVCControllersResolver = new AutofacDependencyResolver(IoC.Instance.GetComponentsContainer());
  }
  </code></pre>
  
  <p>My Repository is as follows</p>
  
  <pre><code> public class Repository : DataConnection, IRepository
  {
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      /// &lt;param name="connection"&gt;&lt;/param&gt;
      public Repository(IDbConnection connection)
          : base(connection)
      {
      }
  
      public IDbConnection GetConnection()
      {
          return Connection;
      }
  
      public IEnumerable&lt;T&gt; Select&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; objFunc) where T : new()
      {
  
          // Orm Lite Version 
          return Connection.Select&lt;T&gt;(objFunc);
  
  
  
  
         }
  }
  </code></pre>
  
  <p>where DataConnection is a class like this</p>
  
  <pre><code>public class DataConnection : IDisposable
  {
      #region Properties
  
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      private IDbConnection _connection;
  
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      protected IDbConnection Connection
      {
          get
          {
              if (_connection.State != ConnectionState.Open &amp;&amp; _connection.State != ConnectionState.Connecting)
                  _connection.Open();
  
              return _connection;
          }
      }
  
      #endregion
  
      /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      /// &lt;param name="connection"&gt;&lt;/param&gt;
      public DataConnection(IDbConnection connection)
      {
          _connection = connection;
      }
  
      /// &lt;summary&gt;
      /// Close the connection if this is open
      /// &lt;/summary&gt;
      public void Dispose()
      {
          if (_connection != null &amp;&amp; _connection.State != ConnectionState.Closed)
              _connection.Close();
      }
  }
  </code></pre>
  
  <p>And my controllers are initialized like this</p>
  
  <pre><code>   public class QueryController : Controller
  {
      private IRepository Repository;
      public QueryController(IRepository repository)
      {
          this.Repository = repository;
      }
  public ActionResult Index()
      {
  
          IEnumerable&lt;Product&gt; products = Repository.Select&lt;Product&gt;(q=&gt;q.Id == 1);
          return View(products);
      }
  }
  </code></pre>
  
  <p>That way I initialize my Repository and my controllers. Now I have the following problem.</p>
  
  <p>I have a class (model) like this </p>
  
  <pre><code>public class OSADCOL
      {
  
          public int id { get; set; }
          public Nullable&lt;int&gt; TBLid { get; set; }
          public Nullable&lt;int&gt; LOVid { get; set; }
          public Nullable&lt;int&gt; COLGRPid { get; set; }
          public Nullable&lt;int&gt; TabCOLGRPid { get; set; }
  }
  </code></pre>
  
  <p>This class get's its data from the database via requests like this in the controller</p>
  
  <pre><code>IEnumerable&lt;OSQDCOL&gt; osqdcols = Connection.Select&lt;OSQDCOL&gt;("TBLid = @id", new {id = Id });
  </code></pre>
  
  <p>or</p>
  
  <pre><code>IEnumerable&lt;OSQDCOL&gt; osqdcols = Repository.Select&lt;OSQDCOL&gt;("TBLid = @id", new {id = Id });
  </code></pre>
  
  <p>Data like this is being used throughout my app in many controllers and is just readonly. Instead of making a new request on the database for every HTTPrequest that needs this data, is there a way to initialise these objects once on Application start? May the singleton pattern is required but I don't know how to implement it using autofac.
  Any Ideas?</p>