通过 WCF 实现 LINQ-to-SQL 事务 [英] Implementing LINQ-to-SQL transactions through WCF

查看:17
本文介绍了通过 WCF 实现 LINQ-to-SQL 事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务,用于将投标添加到数据库,即 MS SQL Server 2005.WCF 使用 LINQ-to-SQL.

I have a WCF service which is used to add tenders to the database, which is MS SQL Server 2005. WCF uses LINQ-to-SQL.

每个投标可以有很多文件和很多项目.客户可以在每次服务调用时添加一个对象.也就是说,做这样的事情:

Each tender can have a lot of documents and a lot of items. The customers can add one object per service call. That is, the do something like this:

TendersServiceClient service = new TenderServiceClient();
service.BeginTransaction();

// Adding a new tender
service.AddTender(TenderDTO tenderInfo);

// Adding tender's documents
foreach (DocumentDTO documentInfo in documents)
   service.AddTenderDocument(tenderInfo.TenderID, documentInfo);

// Adding tender's items
foreach (ItemDTO itemInfo in items)
   service.AddTenderItem(tenderInfo.TenderID, itemInfo);

service.CommitTransaction();

注意 BeginTransaction() 和 CommitTransaction().也就是说,以上所有过程要么完全成功,要么完全回滚.例如,如果其中一项无法插入,则整个投标不应该存在......

Notice the BeginTransaction() and CommitTransaction(). That is, all the above procedure must either succeed completely or be rolled back completely. For example, if one of the items couldn't be inserted, then the whole tender shouldn't exist...

所以问题是我如何实现这种交易.当然,问题在于 WCF 是无状态的.因此为每个服务调用创建了新的 DataContext.如果我改用静态 DataContext,那么我将能够使用其内置的事务功能,但是我如何处理可以同时尝试添加另一个投标的其他客户(当然,他们必须是,在此交易之外)?

So the question is how do I implement this kind of transaction. The problem is that WCF is stateless, of course. So new DataContext is created for each service call. If I use a static DataContext instead, then I'll be able to use its built-in transactions capabilities, but then how can I handle other customers who can try to add another tender in the same time (they must be, of course, outside this transaction)?

所以请帮助我使用某种设计模式来实现这一点.我可以随意更改服务和客户端的代码,因此请随时提出您的建议 =)

So please help me with some kind of design pattern to achieve this. I am free to change the code both of the service and of the client, so feel free with your suggestions =)

推荐答案

你控制服务的接口吗?

如果是这样,当然优雅的解决方案是让服务在单个方法中接受聚合的 Tender 对象,而不是使用您现在拥有的繁琐方法.然后 Tender 会将 Items 和 Documents 作为子集合,并且数据访问代码可以更轻松地处理单个事务中的所有更新.

If so, surely the elegant solution is for the service to accept an aggregate Tender object in a single method rather than having the chatty methods that you have now. The Tender would then have Items and Documents as subcollections, and the data access code could handle all the updates in single transaction much more easily.

除非我有误解,否则它似乎与 Order/OrderDetails 场景非常相似,应用相同的逻辑.

Unless I am misunderstanding, it seems very similar to an Order/OrderDetails scenario, where the same logic applies.

这篇关于通过 WCF 实现 LINQ-to-SQL 事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆