title: MVC syntax for calling repository class
body: <p>In my controller I am calling a repository class which calls store procedures, sql...etc</p>
  
  <p>i.e. Controller looks:</p>
  
  <pre><code> Repository repo = new Repository();
  
   public ActionResult Index()
   {
         var getservice = repo.GetList(...);
  
          foreach (var servicegroup in ServicesSelected)
          {
                  var Result = repo.CheckStatus(...);
            ....
          }
    }
  </code></pre>
  
  <p>my repository class looks like this:</p>
  
  <pre><code>   public int CheckStatus(...)
     {
          using (MyAppConnection context = new MyAppConnection())
          {
              return context.sp_web_Status(...);
          }
      }
  </code></pre>
  
  <p>Whenever I call a sp or a do database operation, I am surrounding within using (for implementing IDispose) and calling context.</p>
  
  <p>EXAMPLE in this action it calls GetList() and then there is a loop it calls e.g. 5 times and every time It generates a new context and i dont think it is the right way to generate new context each time.</p>
  
  <p>What would be the right way to use "one context" and also use "using" in each repository function to implement IDispose or should I not use "using" and create idisposable class </p>
  
  <p>sample example would be much appreciated for the right approach.</p>
  
  <p><strong>UPDATE - Want to use DI framework</strong> </p>
  
  <p>In My HomeController do I need to add reference to Repository class?</p>
  
  <pre><code>public class HomeController : Controller
  {
       SubscriptionRepository _repo;
  
      public HomeController (Repository repo)
      {
          _repo = repo;
      }
  </code></pre>
  
  <p>Inside the Index action call repository class</p>
  
  <pre><code>    _repo.GetList(...);
  </code></pre>
  
  <p>Inside Repository class, do I do the same as what I did in the HomeController? </p>
  
  <pre><code>public class Repository
  {
      private MyAppConnection _context;
  
      public Repository(MyAppConnection context)
      {
          _context = context;
      }
  </code></pre>
  
  <p>And then I remove the using from CheckStatus and have just</p>
  
  <pre><code>    public int CheckStatus(...)
      {
         return _context.sp_web_Status(...);
      }
  }
  </code></pre>
  
  <p>and in the NinjectWebCommon: RegisterServices</p>
  
  <p>Do I register both?</p>
  
  <pre><code> kernel.Bind&lt;Repository&gt;().ToSelf().InRequestScope();
   kernel.Bind&lt;MyAppConnection&gt;().ToSelf().InRequestScope();
  </code></pre>
  
  <p>Is this the correct way to use DI framework and dispose MyAppConnection?
  More concern if I am doing correct in the Repository class?</p>