Note that there are some explanatory texts on larger screens.

plurals
  1. PORepository Connection Pooling
    text
    copied!<p>I'm in a hoo-ha with my boss as I can't shift to using newer technologies until I have proof of some outstanding issues. One of the main concerns is how repositories deal with connections. One of the supposedly largest overheads is connecting and disconnecting to/from the database. If I have a repository where I do the following:</p> <pre><code>public ContractsControlRepository() : base(ConfigurationManager.ConnectionStrings["AccountsConnectionString"].ToString()) { } </code></pre> <p>with the class like so:</p> <pre><code>public class ContractsControlRepository : DataContext, IContractsControlRepository </code></pre> <p>with functions like:</p> <pre><code>public IEnumerable&lt;COContractCostCentre&gt; ListContractCostCentres(int contractID) { string query = "SELECT C.ContractID, C.CCCode, MAC.CostCentre, C.Percentage FROM tblCC_Contract_CC C JOIN tblMA_CostCentre MAC ON MAC.CCCode = C.CCCode WHERE C.ContractID = {0}"; return this.ExecuteQuery&lt;COContractCostCentre&gt;(query, contractID); } </code></pre> <p>Now if in my controller action called <code>_contractsControlRepository.ListContractCostCentres(2)</code> followed immediately by another call to the repository, does it use the same connection? When does the connection open in the controller? When is it closed?</p> <p>Cheers</p> <p><strong>EDIT</strong></p> <p>I'm using hand-written LINQ as suggested by <a href="http://blog.codeville.net/" rel="nofollow noreferrer">Steve Sanderson</a> in his <a href="http://rads.stackoverflow.com/amzn/click/1430210079" rel="nofollow noreferrer">ASP.NET MVC book</a>.</p> <p><strong>EDIT EDIT</strong></p> <p>To clarify, I'm using LINQ as my ORM, but I'm using raw SQL queries (as shown in the extract above) for querying. For example, here's a controller action:</p> <pre><code>public ActionResult EditBusiness(string id) { Business business = _contractsControlRepository.FetchBusinessByID(id); return View(business); } </code></pre> <p>I'm not opening/closing connections.</p> <p>Here's a larger, more complete extract of my repo:</p> <pre><code>public class ContractsControlRepository : DataContext, IContractsControlRepository { public ContractsControlRepository() : base(ConfigurationManager.ConnectionStrings["AccountsConnectionString"].ToString()) { } public IEnumerable&lt;COContractCostCentre&gt; ListContractCostCentres(int contractID) { string query = "SELECT C.ContractID, C.CCCode, MAC.CostCentre, C.Percentage FROM tblCC_Contract_CC C JOIN tblMA_CostCentre MAC ON MAC.CCCode = C.CCCode WHERE C.ContractID = {0}"; return this.ExecuteQuery&lt;COContractCostCentre&gt;(query, contractID); } </code></pre> <p>Then ContractsControlRepository is instantiated in my controller and used like _contractsControlRepository.ListContractCostCentres(2). Connections aren't opened manually, DataContext deals with that for me.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload