protected void DataPortal_Fetch(InventoryCriteria criteria) { bool cancel = false; OnFetching(criteria, ref cancel); if (cancel) return; string commandText = String.Format("SELECT [ItemId], [Qty] FROM [dbo].[Inventory] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using(var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) Map(reader); else throw new Exception(String.Format("The record was not found in 'dbo.Inventory' using the following criteria: {0}.", criteria)); } } } OnFetched(); }
/// <summary> /// This call to delete is for immediate deletion and doesn't keep track of any entity state. /// </summary> /// <param name="criteria">The Criteria.</param> private void DoDelete(InventoryCriteria criteria) { bool cancel = false; OnDeleting(criteria, ref cancel); if (cancel) { return; } string commandText = String.Format("DELETE FROM [dbo].[Inventory] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); //result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. int result = command.ExecuteNonQuery(); if (result == 0) { throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute."); } } } OnDeleted(); }
private Inventory Create(InventoryCriteria criteria) { var item = (Inventory)Activator.CreateInstance(typeof(Inventory), true); bool cancel = false; OnCreating(ref cancel); if (cancel) { return(item); } var resource = Fetch(criteria); using (BypassPropertyChecks(item)) { item.Qty = resource.Qty; } CheckRules(item); MarkNew(item); OnCreated(); return(item); }
private Inventory Create(InventoryCriteria criteria) { var item = (Inventory)Activator.CreateInstance(typeof(Inventory), true); bool cancel = false; OnCreating(ref cancel); if (cancel) return item; var resource = Fetch(criteria); using (BypassPropertyChecks(item)) { item.Qty = resource.Qty; } CheckRules(item); MarkNew(item); OnCreated(); return item; }
protected void DoDelete(ref Inventory item) { // If we're not dirty then don't update the database. if (!item.IsDirty) { return; } // If we're new then don't call delete. if (item.IsNew) { return; } var criteria = new InventoryCriteria { ItemId = item.ItemId }; DoDelete(criteria); MarkNew(item); }
/// <summary> /// Fetch Inventory. /// </summary> /// <param name="criteria">The criteria.</param> /// <returns></returns> public Inventory Fetch(InventoryCriteria criteria) { bool cancel = false; OnFetching(criteria, ref cancel); if (cancel) { return(null); } Inventory item; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand("[dbo].[CSLA_Inventory_Select]", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { item = Map(reader); } else { throw new Exception(String.Format("The record was not found in 'dbo.Inventory' using the following criteria: {0}.", criteria)); } } } } MarkOld(item); OnFetched(); return(item); }
/// <summary> /// Fetch Inventory. /// </summary> /// <param name="criteria">The criteria.</param> /// <returns></returns> public Inventory Fetch(InventoryCriteria criteria) { bool cancel = false; OnFetching(criteria, ref cancel); if (cancel) { return(null); } Inventory item; string commandText = String.Format("SELECT [ItemId], [Qty] FROM [dbo].[Inventory] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using (var reader = new SafeDataReader(command.ExecuteReader())) { if (reader.Read()) { item = Map(reader); } else { throw new Exception(String.Format("The record was not found in 'dbo.Inventory' using the following criteria: {0}.", criteria)); } } } } MarkOld(item); OnFetched(); return(item); }
public void Delete(InventoryCriteria criteria) { // Note: this call to delete is for immediate deletion and doesn't keep track of any entity state. DoDelete(criteria); }
private void DoUpdate(ref Inventory item, bool stopProccessingChildren) { bool cancel = false; OnUpdating(ref cancel); if (cancel) { return; } // Don't update if the item isn't dirty. if (item.IsDirty) { if (item.OriginalItemId != item.ItemId) { // Insert new child. var temp = (Inventory)Activator.CreateInstance(typeof(Inventory), true); temp.ItemId = item.ItemId; temp.Qty = item.Qty; temp = temp.Save(); // Mark child lists as dirty. This code may need to be updated to one-to-one relationships. // Update Children // Delete the old. var criteria = new InventoryCriteria { ItemId = item.OriginalItemId }; Delete(criteria); // Mark the original as the new one. item.OriginalItemId = item.ItemId; MarkOld(item); CheckRules(item); OnUpdated(); return; } const string commandText = "UPDATE [dbo].[Inventory] SET [ItemId] = @p_ItemId, [Qty] = @p_Qty WHERE [ItemId] = @p_ItemId; SELECT [ItemId] FROM [dbo].[Inventory] WHERE [ItemId] = @p_OriginalItemId"; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_OriginalItemId", item.OriginalItemId); command.Parameters.AddWithValue("@p_ItemId", item.ItemId); command.Parameters.AddWithValue("@p_Qty", item.Qty); //result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. int result = command.ExecuteNonQuery(); if (result == 0) { throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute."); } } } } item.OriginalItemId = item.ItemId; MarkOld(item); CheckRules(item); if (!stopProccessingChildren) { // Update Child Items. } OnUpdated(); }
/// <summary> /// This call to delete is for immediate deletion and doesn't keep track of any entity state. /// </summary> /// <param name="criteria">The Criteria.</param> private void DoDelete(InventoryCriteria criteria) { bool cancel = false; OnDeleting(criteria, ref cancel); if (cancel) return; string commandText = String.Format("DELETE FROM [dbo].[Inventory] {0}", ADOHelper.BuildWhereStatement(criteria.StateBag)); using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand(commandText, connection)) { command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); //result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. int result = command.ExecuteNonQuery(); if (result == 0) throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute."); } } OnDeleted(); }
protected void DoDelete(ref Inventory item) { // If we're not dirty then don't update the database. if (!item.IsDirty) return; // If we're new then don't call delete. if (item.IsNew) return; var criteria = new InventoryCriteria{ItemId = item.ItemId}; DoDelete(criteria); MarkNew(item); }
private void DoUpdate(ref Inventory item, bool stopProccessingChildren) { bool cancel = false; OnUpdating(ref cancel); if (cancel) return; // Don't update if the item isn't dirty. if (item.IsDirty) { if(item.OriginalItemId != item.ItemId) { // Insert new child. var temp = (Inventory)Activator.CreateInstance(typeof(Inventory), true); temp.ItemId = item.ItemId; temp.Qty = item.Qty; temp = temp.Save(); // Mark child lists as dirty. This code may need to be updated to one-to-one relationships. // Update Children // Delete the old. var criteria = new InventoryCriteria {ItemId = item.OriginalItemId}; Delete(criteria); // Mark the original as the new one. item.OriginalItemId = item.ItemId; MarkOld(item); CheckRules(item); OnUpdated(); return; } const string commandText = "UPDATE [dbo].[Inventory] SET [ItemId] = @p_ItemId, [Qty] = @p_Qty WHERE [ItemId] = @p_ItemId; SELECT [ItemId] FROM [dbo].[Inventory] WHERE [ItemId] = @p_OriginalItemId"; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using(var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_OriginalItemId", item.OriginalItemId); command.Parameters.AddWithValue("@p_ItemId", item.ItemId); command.Parameters.AddWithValue("@p_Qty", item.Qty); //result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. int result = command.ExecuteNonQuery(); if (result == 0) throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute."); } } } item.OriginalItemId = item.ItemId; MarkOld(item); CheckRules(item); if(!stopProccessingChildren) { // Update Child Items. } OnUpdated(); }
protected override void DataPortal_Update() { bool cancel = false; OnUpdating(ref cancel); if (cancel) return; if(OriginalItemId != ItemId) { // Insert new child. Inventory item = new Inventory {ItemId = ItemId, Qty = Qty}; item = item.Save(); // Mark editable child lists as dirty. This code may need to be updated to one-to-one relationships. // Create a new connection. using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); FieldManager.UpdateChildren(this, connection); } // Delete the old. var criteria = new InventoryCriteria {ItemId = OriginalItemId}; DataPortal_Delete(criteria); // Mark the original as the new one. OriginalItemId = ItemId; OnUpdated(); return; } const string commandText = "UPDATE [dbo].[Inventory] SET [ItemId] = @p_ItemId, [Qty] = @p_Qty WHERE [ItemId] = @p_OriginalItemId; SELECT [ItemId] FROM [dbo].[Inventory] WHERE [ItemId] = @p_OriginalItemId"; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using(var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_OriginalItemId", this.OriginalItemId); command.Parameters.AddWithValue("@p_ItemId", this.ItemId); command.Parameters.AddWithValue("@p_Qty", this.Qty); //result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed. int result = command.ExecuteNonQuery(); if (result == 0) throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute."); LoadProperty(_originalItemIdProperty, this.ItemId); } } OnUpdated(); }
/// <summary> /// CodeSmith generated stub method that is called when fetching the <see cref="Inventory"/> object. /// </summary> /// <param name="criteria"><see cref="InventoryCriteria"/> object containing the criteria of the object to fetch.</param> /// <param name="cancel">Value returned from the method indicating whether the object fetching should proceed.</param> partial void OnFetching(InventoryCriteria criteria, ref bool cancel);
/// <summary> /// CodeSmith generated stub method that is called when deleting the <see cref="Inventory"/> object. /// </summary> /// <param name="criteria"><see cref="InventoryCriteria"/> object containing the criteria of the object to delete.</param> /// <param name="cancel">Value returned from the method indicating whether the object deletion should proceed.</param> partial void OnDeleting(InventoryCriteria criteria, ref bool cancel);
/// <summary> /// Fetch Inventory. /// </summary> /// <param name="criteria">The criteria.</param> /// <returns></returns> public Inventory Fetch(InventoryCriteria criteria) { bool cancel = false; OnFetching(criteria, ref cancel); if (cancel) return null; Inventory item; using (var connection = new SqlConnection(ADOHelper.ConnectionString)) { connection.Open(); using (var command = new SqlCommand("[dbo].[CSLA_Inventory_Select]", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddRange(ADOHelper.SqlParameters(criteria.StateBag)); using(var reader = new SafeDataReader(command.ExecuteReader())) { if(reader.Read()) item = Map(reader); else throw new Exception(String.Format("The record was not found in 'dbo.Inventory' using the following criteria: {0}.", criteria)); } } } MarkOld(item); OnFetched(); return item; }