I'm writing some merge functionality in C# asp.NET MVC2. I am also using using Linq2SQL.
I have a block of code which calls two services, MessageService and UserService. These both in term call their appropriate repositories and make the amendments to the db. Each repository declares it's own instance of the repository so I'm thinking this will escalate the following code to DTC . The code is called from the AccountService, is this going to work at this level? And also is it bad practise to declare the DataContext at the top of every repository or should I pass the object around somehow? Thank you in advance
//Run the merge
try
{
using (TransactionScope scope = new TransactionScope())
{
// Update Messages to be owned by primary user
if (!_MessageService.UpdateCreatedById(MergeUser.UserID, CoreUser.UserID))
{
return false;
}
// Update Comments to be owned by primary user
foreach (var curComment in _MessageService.GetUserComments(MergeUser.UserID))
{
curComment.CreatedBy = CoreUser.UserID;
}
_MessageService.Save();
// Update Logins to be owned by primary user
foreach (var CurLogin in _UserService.GetLogins(MergeUser.UserID))
{
CurLogin.UserID = CoreUser.UserID;
}
_UserService.Save();
scope.Complete();
}
return true;
}
catch (Exception ex)
{
_ErrorStack.Add(ex.Message);
ErrorService.AddError(new ErrorModel("Portal", "WidgetRepository", ErrorHelper.ErrorTypes.Critical, ex));
return false;
}