如何在一次交易中执行多项操作 [英] How to perform multiple operations in under single transaction

查看:93
本文介绍了如何在一次交易中执行多项操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,要求将记录添加到表中,然后-如果添加了记录,则在云上创建资源,如果在云上创建了资源,则使用资源标识符更新表中的记录.因此,它们是3个操作,当它们中的任何一个都不成功时,我想还原所有操作.

I have a scenario where it requires to add a record in to table, then - creating a resource on the cloud if record is added, then update the record in table with the resource identifier if resource is created on cloud. So, they are 3 operations and I want to revert all of it when any of them doesn't succeed.

我们一次拥有多个Db操作的TransactionScope,但我想知道如何实现?感谢您的帮助!

We have TransactionScope for Multiple Db Operations in one go but I'm wondering how to achieve this? Appreciate your help!

修改

PS:可以有许多类似的操作-依次说10个或更多,它们甚至可能与数据库操作无关.他们可能只是按顺序创建10个文件-因此,如果任何文件创建失败,则应删除/撤消所有以前的文件.

PS: There could be any number of operations like that - say 10 or more in a sequence, and they may not even related to DB operations. They could just be creating 10 files in a sequence - so when any of the file creation fails - all the previous files should be deleted/undone.

推荐答案

如何使用命令模式?这可能不是完美的命令模式实现,但非常接近.见下文:

How about going a command pattern way? It's may not be perfect command pattern implementation but something very close. See below:

public interface ICommand {
    ICommandResult Execute();
    ICommandResult Rollback();
}

public interface ICommandResult {
    bool Success { get; set; }
    object Data { get; set; }
    Exception Error { get; set; }
}

public class CommandResult : ICommandResult { 
    public bool Success { get; set; }
    public object Data { get; set; }
    public Exception Error { get; set; }
}

public class AddToDBCommand : ICommand {
    private ICommandResult result;
    private int newRecordId;

    public AddToDBCommand(<params_if_any>) {
        result = new CommandResult();
    }

    public ICommandResult Execute() {
        try {
            // insert record into db
            result.Success = true;
            result.Data = 10; // new record id
        }
        catch (Exception ex) {
            result.Success = false;
            result.Error = ex;
        }
        return result;
    }

    public ICommandResult Rollback() {
        try {
            // delete record inserted by this command instance
            // use ICommandResult.Data to get the 'record id' for deletion
            Console.WriteLine("Rolling back insertion of record id: " + result.Data);
            // set Success
        }
        catch(Exception ex) {
            // set Success and Error
            // I'm not sure what you want to do in such case
        }
        return result;
    }
}

类似地,您将创建用于创建云资源和更新db中的记录的命令.在主代码中,您可以保存ICommand对象的集合并执行每个对象.

Similarly you would create commands for creating cloud resource and updating record in db. In main code you can hold collection of ICommand objects and execute each one.

var commands = new List<ICommand>
{
    new AddToDBCommand(<params_if_any>),
    new AddToCloudCommand(<params_if_any>),
    new UpdateInDBCommand(<param_if_any>)
};

然后在循环中可以调用Execute,如果返回Success = false,则将当前命令索引记录在集合中,并在向每个命令调用Rollback的同时向后循环.

Then in the loop you can call Execute, if it returns Success = false then record the current command index in collection and loop backward whilst calling Rollback on each command.

这篇关于如何在一次交易中执行多项操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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