transactionscope 是否可以在对不同服务的多次调用上工作?
我正在 C# asp.NET MVC2 中编写一些合并功能。我也在使用 Linq2SQL。
我有一段代码调用两个服务:MessageService 和 UserService。这些都在术语中调用它们适当的存储库并对数据库进行修改。每个存储库都声明它自己的存储库实例,因此我认为这会将以下代码升级为 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这会起作用。
TransactionScope
利用分布式事务协调器,因此它能够托管数据库级别之外的事务。DataContext 生命周期的推荐做法是将其限制为工作单元。
Yes, This will work.
TransactionScope
leverages the Distributed transaction coordinator so it's capable of hosting Transactions beyond database levels.Recommended practice for DataContext lifecycle is to restrict it to a unit-of-work.
我的存储库上有两个构造函数,一个采用数据上下文,另一个不采用(然后实例化它自己的)。这意味着我可以根据需要使用共享数据上下文创建存储库。
然后,我的服务类在其构造函数中采用存储库对象,因此我可以使用共享数据上下文的存储库实例化多个服务(如果需要)。
I have two constructors on my repositories, one that takes a data context and one that does not (which then instatiates it's own). This means that I can create repositories using a shared data context as required.
My service classes then take a repository object in their constructor, so I can instantiate several services using repositories that are sharing a data context, if so required.