【问题标题】:Accomplishing "Business" transactions multi-tier architecture with physical separation实现物理分离的“业务”交易多层架构
【发布时间】:2014-10-19 19:03:56
【问题描述】:

给定一个应用程序分解为以下层/层

应用层、业务层、数据层(通过 WCF 向业务公开,与应用/业务和数据物理分离)和数据。 如何完成从业务层开始但作用于数据层的事务?

数据层:

[DataContract]
public class Customer
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; }
}

[DataContract]
public class Order
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public int CustomerId { get; set; }
}

/*
    CRUD classes on both entities (I'm hoping this implementation does not matter on the data tier, as the transactions I hope to start on the business end and could potentially have different implementations on the data tier across different areas)
*/

业务:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Order
{
    public int Id { get; set; }
    public Customer PurchasedBy { get; set; }
}

public class PlaceOrderService
{

    public void Execute(Order order)
    {

        try
        {
            // Begin Transaction
            // Call to data tier to create/get customer depending on if they are new or existing
            // Call to data tier to create order
            // Commit transaction
        }
        catch (Exception)
        {
            // Rollback transactions
        }
    }

}

【问题讨论】:

  • 在这样的场景中是否没有办法实现工作单元模式?我才刚刚开始研究它,所以第一次在多层环境中实施它让我不知道从哪里开始。
  • 我实际上从未使用过它,但我相信这就是它的用途。它应该适用于 WCF。

标签: c# wcf transactions n-tier-architecture


【解决方案1】:

如果数据层在物理上是独立的并且不代表业务事务立面,那么您必须推出自己的补偿资源管理器。如果数据层位于业务立面上,那么您可以创建 SQL 事务并将其传递给每个数据层调用,以便它们可以加入事务。

在许多情况下,我们可以使用的唯一事务能力是 SQL Server 数据库引擎事务。在多数据库环境中,即使使用队列和其他非关系数据库也是不可用的。在这种情况下,您需要在各种数据存储架构中为事务的每个组件(即,如果您打算进行 INSERT,则为 DELETE)设计一个数据反转操作,并使用这些操作来撤销失败的事务。

【讨论】:

    【解决方案2】:

    您需要通过服务设计和管理自己的逻辑事务。

    通常这意味着以下流程:

    1. 从事务服务请求启动逻辑事务
    2. 接收交易的逻辑交易 ID
    3. 流程中的所有消息/调用都将包含事务 ID,并且 可能意味着物理数据库中用于未提交数据的隔离区域 而交易仍在进行中。
    4. 在进行相关调用并验证数据后,根据验证/验证的结果请求提交逻辑事务或请求回滚。

    我已经看到在基于消息传递的系统中实现了这一点,它工作得相对较好,但确实需要一些开销来正确管理逻辑事务。

    【讨论】:

      猜你喜欢
      • 2016-04-21
      • 2010-12-07
      • 2013-02-21
      • 2023-03-27
      • 2017-01-03
      • 2023-03-25
      • 2014-04-12
      • 2014-04-13
      • 1970-01-01
      相关资源
      最近更新 更多