Loading ...

A question on transactions

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  data access   » A question on transactions

A question on transactions

Posts under the topic: A question on transactions

Posted: 6/19/2011

Lurker 45  points  Lurker
  • Joined on: 6/19/2011
  • Posts: 9

Hi

I am developing an asp.net application which has a data access layer (DAL) as well as a business logic layer (BLL). In the BLL I have an object named Project, which has a number of properties.

When I call the method Save to save the project object in the database, the Save method of the Data Access Layer object ProjectDetails is called which saves the object to the database, Projects table.

Now, I have added a new member to the Project object, which is a list of another BLL object, List<Deadline>. The Deadline object also has comparable DeadlineDetails object in the DAL to write it to the DB, Deadlines table.

I should also mention that the Projects table is linked with the Deadlines table, on ProjectId field.

My question is the following:

In saving the Project object, should I call the Project.Save and then the Deadline.Save methods of the BLL, or should I amend the ProjectDetails class in the DAL (containing the List<DeadlineDetails>) so as to write both the ProjectDetails and the DeadlineDetails? Should I include that in a transaction (so as to ensure that both the project old properties and the new List are written)?

What kind of transaction would be appropriate for this? SQL transaction?

 

Chris


Posted: 6/20/2011

Starter 727  points  Starter
  • Joined on: 6/6/2011
  • Posts: 74
  Answered

Hi Chris,

My recomendation is to write another method in your DAL, and use transaction. In your transaction scope you should first save your Project object, and return your Project Id, if this is successful, then with a foreach loop save your Deadline Details adding the returned 'Project Id' in every DeadlineDetails object and save it. 

If an error occurs rollback your transaction and alert the user with some error message, if necessary.

In this case you will be sure that the ProjectDetails are saved along DeadlineDetails.

And most important thing use try{] catch{}. :)

Here is a sample code:

CustomResponseObject Response = new CustomResponseObject();
bool _result = false;

try
{
	using (var _scope = new TransactionScope())
	{
		//first save the project 
		long _prID = ProjectDetails.Save(prDetails);
		// check if project is successfuly saved
		if (_prID != null && _prID > 0)
		{
			foreach (Deadline _detail in ProjectDetails.DeadlineDetails)
			{
				// save Deadline Detail
				_prDetailSaved = DeadlineDetails.Save(_detail);
				
				// check if true
				if (_prDetailSaved)
				{
					_result = true;
				}
				if (!_prDetailSaved)
				{
					this.Response.StatusDescription = "Error saving project details!";
					break;
				}
			}
			
			if (_result)                                        
				_scope.Complete();
		}
}
catch(Exception _exc)
{
	LogManager.Log(this, _exc);
	this.Response.ConfirmationStatus = Status.Failure;
	this.Response.StatusCode = StatusCode.GeneralError;
	this.Response.StatusDescription = _exc.ToString();
	this.Response.IsFault = true;
	
	return this.Response;
}


you should not forget to call
_scope.Complete(), otherwise the system will return status true, means the informations are successfuly saved but, they will not be saved if you forget to call _scope.Complete(), this is commitment of the transaction.

And one more thing you should add System.Transactions in you references and using System.Transactions; in your code.

Best Regards,

Gjorgji Dimitrov


Posted: 6/21/2011

Lurker 45  points  Lurker
  • Joined on: 6/19/2011
  • Posts: 9

Gjorgji 

5 stars for yor answer; it worked first time! Thanks a lot.

I have another question for you (although it is more of a beginner's question/dilemma):

After creating my create method, I am now faced with the task of creating the update method and my dilemma is this:

The user may change some deadlines (i.e. change their properties), or add some new or remove some existing....I have written the UpdateDeadlineDetails method which works for an existing deadline (it's based on its id). The problem I have is in adding new/removing existing deadlines; should I delete all of them and re-create them (in which case I don't care about updating any changes in existing deadlines) or should I update and then insert logic in my code to see if new deadlines are there (their id would be set to 0) and create them? In the same scenario, how would I go about deleting the ones the user removed? In other words how do I know which to delete? Do I compare them with what in the database? I currently have them in Lists as explained above.

 

Chris


Posted: 6/21/2011

Starter 727  points  Starter
  • Joined on: 6/6/2011
  • Posts: 74
  Answered

Hi Chris,

My recomendation for the DeadlineDetails table is do not delete DeadlineDetails rows on user interaction, but to add a column 'IsActive' or 'Deleted' that will be datatype Bit (true or false), and when a user wants to remove some Deadline then you should change the flag to true or false, depend what kind of logic you will implement (negative or positive). 

So when you will going to update you will check this column if it's deleted then there is no need to update this row, or you can update it independent of existance.

Another plus with this column is that you will have a track/log which deadline are deleted, like history.

Best Regards,

Gjorgji Dimitrov


Posted: 6/21/2011

Lurker 45  points  Lurker
  • Joined on: 6/19/2011
  • Posts: 9

Great advice, thanks for your help.

Chris


Page 1 of 1 (5 items)