Note that there are some explanatory texts on larger screens.

plurals
  1. PObusiness service calls multiple methods on the Data Access Layer each opening a connection
    primarykey
    data
    text
    <p>I am doing this pseudo-code in my unitservice which is calling 2 methods of the UnitDataProvider class.</p> <pre><code>var units = dataProvider.GetChildrenUnits(parentId); unit.HierarchyIndex = units.Where( u =&gt; u.TemplateId == unit.TemplateId &amp;&amp; u.ParentId == null).Max( u =&gt; u.HierarchyIndex) + 1; dataProvider.AddSiblingUnit(unit); </code></pre> <p>Every method opens a connection.</p> <p>How could this be refactorized to use only 1 connection or reuse an open connection?</p> <p>Or do you think its normal to a connection for each dataprovider method call?</p> <p>SOLUTION: all data access logic on the database is done within the DAL not the BLL as I think the below is NO business logic for my business its just database logic.</p> <pre><code>public void AddSiblingUnit(Unit unit) { if (unit.ParentId == null) { throw new OnlyOneRootNodeIsAllowedException("Only one Root node is allowed!", null); } // Server side check if the root is selected because 2 root units are not allowed lock (_lockObject) { using (var trans = new TransactionScope()) using (var con = new SqlConnection(_connectionString)) using (var cmd = new SqlCommand()) { cmd.Connection = con; con.Open(); // SELECT HierarchyIndex for the new inserted sibling string selectCommandText = "SELECT HierarchyIndex FROM UNIT WHERE UnitId = @UnitId"; // UnitId of the selected sibling cmd.CommandText = selectCommandText; cmd.Parameters.AddWithValue("UnitId", unit.UnitId); // the parentId which is the selected UnitId int hierarchyIndexOfSelectedSibling = Convert.ToInt32(cmd.ExecuteScalar()); int hierarchyIndexOfNewSibling = hierarchyIndexOfSelectedSibling + 1; // UPDATE all sibling units whose HierarchyIndex is greater than the HierarchyIndex of the selected sibling string updateCommandText = "UPDATE UNIT SET HierarchyIndex = HierarchyIndex + 1 WHERE HierarchyIndex &gt;= @HierarchyIndex AND ParentId = @ParentId"; cmd.CommandText = updateCommandText; cmd.Parameters.AddWithValue("HierarchyIndex", hierarchyIndexOfNewSibling); cmd.Parameters.AddWithValue("ParentId", unit.ParentId); cmd.ExecuteNonQuery(); // INSERT new sibling string insertCommandText = "INSERT INTO UNIT (Name,TemplateId,CreatedAt,HierarchyIndex,ParentId) VALUES (@Name,@TemplateId,@CreatedAt,@HierarchyIndex,@ParentId);Select Scope_Identity();"; cmd.CommandText = insertCommandText; cmd.Parameters.AddWithValue("Name", unit.Name); cmd.Parameters.AddWithValue("TemplateId", unit.TemplateId); cmd.Parameters.Add("CreatedAt", SqlDbType.DateTime2).Value = unit.CreatedAt; unit.UnitId = Convert.ToInt32(cmd.ExecuteScalar()); trans.Complete(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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