交易范围类似的功能

7
我希望设置与交易范围非常相似的东西,它会在服务上创建一个版本,并将在范围结束时删除/提交。事务范围内运行的每个SQL语句都会在一些连接池/事务存储中查找以确定其是否在范围内,并做出适当反应。调用者不需要在每次调用时传递交易。我正在寻找这种功能。
下面是更多关于此的信息:https://blogs.msdn.microsoft.com/florinlazar/2005/04/19/transaction-current-and-ambient-transactions/ 下面是基本的可处理类:
public sealed class VersionScope : IDisposable
{
    private readonly GeodatabaseVersion _version;
    private readonly VersionManager _versionManager;

    public VersionScope(Configuration config)
    {
        _versionManager = new VersionManager(config);
        _version = _versionManager.GenerateTempVersion();
        _versionManager.Create(_version);
        _versionManager.VerifyValidVersion(_version);
        _versionManager.ServiceReconcilePull();
        _versionManager.ReconcilePull(_version);
    }

    public void Dispose()
    {
        _versionManager.Delete(_version);
    }

    public void Complete()
    {
        _versionManager.ReconcilePush(_version);
    }
}

我希望自己编写的所有代码都没有版本概念。我只想在代码的最低层包含一个简单的 Version = GetCurrentVersionWithinScope()
如果同时有多个实例在内存中运行,那么实现这样的功能最安全的方式是什么?
我非常天真地认为可以找到进程正在运行的内存块的唯一标识符。然后将当前工作版本存储到全局数组或并发字典中。然后,在需要当前版本的代码中,使用其内存块标识符并映射到创建的版本。
编辑:
用法示例:
using (var scope = new VersionScope(_config))
{
    AddFeature(); // This has no concept of scope passed to it, and could error out forcing a dispose() without a complete()
    scope.Complete();
}

在这里实现“IDisposable”似乎有些奇怪。 - maccettura
你可以直接使用 TransactionScope 吗?它支持与环境事务的连接。请参阅 Microsoft Docs 上的 Implementing an Implicit Transaction using Transaction Scope 文章中的 Managing transaction flow using TransactionScopeOption 部分以及 Daniel Marbach 的文章 TransactionScope and Async/Await. Be one with the flow! - Leonid Vasilev
2个回答

4
最直接的方法是使用ThreadStaticThreadLocal将当前版本存储在线程本地存储中,这样多个线程就不会相互干扰。例如,假设我们有一个版本类:
public class Version {
    public Version(int number) {
        Number = number;
    }
    public int Number { get; }

    public override string ToString() {
        return "Version " + Number;
    }
}

然后,VersionScope 的实现可以像这样进行:

public sealed class VersionScope : IDisposable {
    private bool _isCompleted;
    private bool _isDisposed;
    // note ThreadStatic attribute
    [ThreadStatic] private static Version _currentVersion;
    public static Version CurrentVersion => _currentVersion;

    public VersionScope(int version) {
        _currentVersion = new Version(version);
    }

    public void Dispose() {
        if (_isCompleted || _isDisposed)
            return;
        var v = _currentVersion;
        if (v != null) {
            DeleteVersion(v);
        }
        _currentVersion = null;
        _isDisposed = true;
    }

    public void Complete() {
        if (_isCompleted || _isDisposed)
            return;
        var v = _currentVersion;
        if (v != null) {
            PushVersion(v);
        }
        _currentVersion = null;
        _isCompleted = true;
    }

    private void DeleteVersion(Version version) {
        Console.WriteLine($"Version {version} deleted");
    }

    private void PushVersion(Version version) {
        Console.WriteLine($"Version {version} pushed");
    }
}

它会起作用,但不支持嵌套作用域,这是不好的,所以为了解决这个问题,在开始新的作用域时需要存储上一个作用域,并在 CompleteDispose 时恢复它:

public sealed class VersionScope : IDisposable {
    private bool _isCompleted;
    private bool _isDisposed;
    private static readonly ThreadLocal<VersionChain> _versions = new ThreadLocal<VersionChain>();

    public static Version CurrentVersion => _versions.Value?.Current;

    public VersionScope(int version) {
        var cur = _versions.Value;
        // remember previous versions if any
        _versions.Value = new VersionChain(new Version(version), cur);
    }

    public void Dispose() {
        if (_isCompleted || _isDisposed)
            return;
        var cur = _versions.Value;
        if (cur != null) {
            DeleteVersion(cur.Current);
            // restore previous
            _versions.Value = cur.Previous;
        }
        _isDisposed = true;
    }

    public void Complete() {
        if (_isCompleted || _isDisposed)
            return;
        var cur = _versions.Value;
        if (cur != null) {
            PushVersion(cur.Current);
            // restore previous
            _versions.Value = cur.Previous;
        }
        _isCompleted = true;
    }

    private void DeleteVersion(Version version) {
        Console.WriteLine($"Version {version} deleted");
    }

    private void PushVersion(Version version) {
        Console.WriteLine($"Version {version} pushed");
    }

    // just a class to store previous versions
    private class VersionChain {
        public VersionChain(Version current, VersionChain previous) {
            Current = current;
            Previous = previous;
        }

        public Version Current { get; }
        public VersionChain Previous { get; }
    }
}

这已经是你可以使用的一些东西了。以下是示例用法(我使用单个线程,但如果有多个线程分别执行此操作-它们不会互相干扰):

static void Main(string[] args) {
    PrintCurrentVersion(); // no version
    using (var s1 = new VersionScope(1)) {
        PrintCurrentVersion(); // version 1
        s1.Complete();
        PrintCurrentVersion(); // no version, 1 is already completed
        using (var s2 = new VersionScope(2)) {
            using (var s3 = new VersionScope(3)) {
                PrintCurrentVersion(); // version 3
            } // version 3 deleted
            PrintCurrentVersion(); // back to version 2
            s2.Complete();
        }
        PrintCurrentVersion(); // no version, all completed or deleted
    }
    Console.ReadKey();
}

private static void PrintCurrentVersion() {
    Console.WriteLine("Current version: " + VersionScope.CurrentVersion);
}

然而,当您使用异步调用时,这种方法将不起作用,因为ThreadLocal与线程绑定,但异步方法可以跨多个线程。然而,有一个类似的结构体名为AsyncLocal,其值将通过异步调用流动。因此,我们可以向VersionScope添加构造函数参数,指示是否需要异步流。事务范围以类似的方式工作 - 有一个TransactionScopeAsyncFlowOption,您将其传递到TransactionScope构造函数中,指示它是否会通过异步调用流动。
修改后的版本如下:
public sealed class VersionScope : IDisposable {
    private bool _isCompleted;
    private bool _isDisposed;
    private readonly bool _asyncFlow;
    // thread local versions
    private static readonly ThreadLocal<VersionChain> _tlVersions = new ThreadLocal<VersionChain>();
    // async local versions
    private static readonly AsyncLocal<VersionChain> _alVersions = new AsyncLocal<VersionChain>();
    // to get current version, first check async local storage, then thread local
    public static Version CurrentVersion => _alVersions.Value?.Current ?? _tlVersions.Value?.Current;
    // helper method
    private VersionChain CurrentVersionChain => _asyncFlow ? _alVersions.Value : _tlVersions.Value;

    public VersionScope(int version, bool asyncFlow = false) {
        _asyncFlow = asyncFlow;

        var cur = CurrentVersionChain;
        // remember previous versions if any
        if (asyncFlow) {
            _alVersions.Value = new VersionChain(new Version(version), cur);
        }
        else {
            _tlVersions.Value = new VersionChain(new Version(version), cur);
        }
    }

    public void Dispose() {
        if (_isCompleted || _isDisposed)
            return;
        var cur = CurrentVersionChain;
        if (cur != null) {
            DeleteVersion(cur.Current);
            // restore previous
            if (_asyncFlow) {
                _alVersions.Value = cur.Previous;
            }
            else {
                _tlVersions.Value = cur.Previous;
            }
        }
        _isDisposed = true;
    }

    public void Complete() {
        if (_isCompleted || _isDisposed)
            return;
        var cur = CurrentVersionChain;
        if (cur != null) {
            PushVersion(cur.Current);
            // restore previous
            if (_asyncFlow) {
                _alVersions.Value = cur.Previous;
            }
            else {
                _tlVersions.Value = cur.Previous;
            }
        }
        _isCompleted = true;
    }

    private void DeleteVersion(Version version) {
        Console.WriteLine($"Version {version} deleted");
    }

    private void PushVersion(Version version) {
        Console.WriteLine($"Version {version} pushed");
    }

    // just a class to store previous versions
    private class VersionChain {
        public VersionChain(Version current, VersionChain previous) {
            Current = current;
            Previous = previous;
        }

        public Version Current { get; }
        public VersionChain Previous { get; }
    }
}

使用异步流的范围示例:

static void Main(string[] args) {
    Test();
    Console.ReadKey();
}

static async void Test() {
    PrintCurrentVersion(); // no version
    using (var s1 = new VersionScope(1, asyncFlow: true)) {
        await Task.Delay(100);
        PrintCurrentVersion(); // version 1
        await Task.Delay(100);
        s1.Complete();
        await Task.Delay(100);
        PrintCurrentVersion(); // no version, 1 is already completed
        using (var s2 = new VersionScope(2, asyncFlow: true)) {
            using (var s3 = new VersionScope(3, asyncFlow: true)) {
                PrintCurrentVersion(); // version 3
            } // version 3 deleted
            await Task.Delay(100);
            PrintCurrentVersion(); // back to version 2
            s2.Complete();
        }
        await Task.Delay(100);
        PrintCurrentVersion(); // no version, all completed or deleted
    }
}

private static void PrintCurrentVersion() {
    Console.WriteLine("Current version: " + VersionScope.CurrentVersion);
}

0

像这样使用IDisposable有些值得商榷。(请参见Is it abusive to use IDisposable and "using" as a means for getting "scoped behavior" for exception safety?

我自己发现它对某些事情很有用。这是我使用的一种模式:

class LevelContext
{
    private int _level;

    public int CurrentLevel
    {
        get { return _level; }
        set { _level = value < 0 ? 0 : value; }
    }

    public ILevel NewLevel(int depth = 1)
    {
        return new Level(this, depth);
    }

    /// <summary>
    /// Provides an interface that calling code can use to handle level objects.
    /// </summary>
    public interface ILevel : IDisposable
    {
        LevelContext Owner { get; }
        int Depth { get; }
        void Close();
    }

    /// <summary>
    /// Private class that provides an easy way to scope levels by allowing
    /// them to participate in the "using" construct. Creation of a Level results in an
    /// increase in owner's level, while disposal returns owner's level to what it was before.
    /// </summary>
    class Level : ILevel
    {
        public Level(LevelContext owner, int depth)
        {
            Owner = owner;
            Depth = depth;
            PreviousLevel = owner.CurrentLevel;
            Owner.CurrentLevel += Depth;
        }

        public LevelContext Owner { get; private set; }
        public int Depth { get; private set; }
        public int PreviousLevel { get; private set; }

        public void Close()
        {
            if (Owner != null)
            {
                Owner.CurrentLevel = PreviousLevel;
                Owner = null;
            }
        }

        void IDisposable.Dispose()
        {
            Close();
        }

    }

然后调用代码看起来像这样:

    static void Main(string[] args)
    {
        var lc = new LevelContext();

        Console.WriteLine(lc.CurrentLevel);

        using (lc.NewLevel())
            Console.WriteLine(lc.CurrentLevel);

        Console.WriteLine(lc.CurrentLevel);
    }

所以在你的情况下,你是正确的 - 你需要创建一个跟踪当前版本的东西。当 VersionScopes 被创建和释放时,那个东西应该得到更新。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接