C# asp.NET MVC2에서 병합 기능을 일부 작성했습니다. 나는 또한 Linq2SQL을 사용하고있다.transactionscope는 여러 서비스를 여러 번 호출해도 작동합니까?
MessageService와 UserService라는 두 개의 서비스를 호출하는 코드 블록이 있습니다. 이들은 둘 다 적절한 저장소를 호출하고 db에 대한 수정을합니다. 각 저장소는 저장소의 자체 인스턴스라고 선언하므로 다음 코드를 DTC으로 에스컬레이션 할 것입니다. 이 코드는 AccountService에서 호출됩니다.이 코드는이 수준에서 작동합니까? 또한 모든 저장소의 맨 위에있는 DataContext를 선언하는 것이 좋지 않습니까? 아니면 어떻게 든 객체를 전달해야합니까? 미리 감사드립니다.
//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;
}
그래서 paddy가 언급 한 서비스/저장소 간의 컨텍스트를 전달해서는 안됩니까? – Andrew
TransactionScope는 DataContext를 알지 못하므로 DataContext를 알 필요가 없습니다. TransactionScope는 DataContext보다 앞에 있습니다. –