Skip to content

How to improve my code where I need to undo what I did #1198

Answered by StefanBertels
shoter asked this question in Q&A
Discussion options

You must be logged in to vote

Very interesting question. I'm interested in nice solutions for this, too.

Finally this is similar to database transactions where you can rollback (or commit). IMHO code gets more functional (FP style) if we try to avoid any intermediate side effect. But I find this is quite often impossible. An easy example is "create 2 files" which might fail after file 1 is created.

Maybe something like this:

public sealed class TransactionLog : IDisposable
{
    private readonly Stack<IDisposable> _rollbacks = new ();

    public Unit AddRollback(Action rollbackAction)
    {
        _rollbacks.Push(Disposable.Create(rollbackAction));
        return unit;
    }

    public Unit Commit()
    {
        _…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@shoter
Comment options

@shoter
Comment options

@StefanBertels
Comment options

Answer selected by shoter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants