Click or drag to resize

Getting started

First of all, add to your project the Novacta.Transactions.IO NuGet package.

The main type in the library is FileManager. It represents a manager of file resources which can be enlisted in a transaction.

Since FileManager is an abstract class, you do not instantiate it in your code. Instead, you can use one of its specialized subclasses, such as CreateFileManager, EditFileManager, DeleteFileManager, and CopyFileManager, to create, update, delete, and copy files, respectively.

The following example shows the standard use case scenario.

C#
using System;
using System.Transactions;

namespace Novacta.Transactions.IO.CodeExamples
{
    public class UseCaseExample0  
    {
        public void Main()
        {
            var transactionSuccessfullyCommitted = true;

            try
            {
                // Create a TransactionScope to manage 
                // your resources, such as files or database
                // connections. In this way, it is guaranteed 
                // that the managing actions can commit or roll back 
                // as a single unit of work.
                using (TransactionScope scope = new TransactionScope())
                {
                    // Add the file managers you need and let 
                    // them explicitly enlist the current transaction.

                    // To copy a source file to a destination file
                    // in case of a successfully committed transaction.
                    var copyManager =
                        new CopyFileManager(
                            sourcePath: "file-to-copy.txt",
                            managedPath: "destination-file.txt",
                            overwrite: true);
                    copyManager.EnlistVolatile(EnlistmentOptions.None);

                    // To delete a file 
                    // in case of a successfully committed transaction.
                    var deleteManager =
                        new DeleteFileManager(
                            managedPath: "file-to-delete.txt");
                    deleteManager.EnlistVolatile(EnlistmentOptions.None);

                    // To create a file 
                    // in case of a successfully committed transaction
                    // (this will create an empty file: if a file having 
                    // specific contents is needed, define a new 
                    // class derived from CreateFileManager and 
                    // override method OnCommit).
                    var createManager =
                        new CreateFileManager(
                            managedPath: "file-to-create.txt",
                            overwrite: true);
                    createManager.EnlistVolatile(EnlistmentOptions.None);

                    // To edit a file 
                    // in case of a successfully committed transaction
                    // (here MyEditFileManager derives from EditFileManager
                    // and has overridden method OnCommit to state
                    // how the managed file must be edited).
                    var editManager =
                        new MyEditFileManager(
                            managedPath: "file-to-edit.txt");
                    editManager.EnlistVolatile(EnlistmentOptions.None);

                    // Add here additional resource managers,
                    // such as database connections, that need
                    // enlisting in the current transaction.

                    // The Complete method commits the transaction. 
                    // If an exception has been thrown, Complete 
                    // is not called and the transaction is rolled back.
                    scope.Complete();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Transaction rolled back.");
                Console.WriteLine("Reason:");
                Console.WriteLine(e);
                transactionSuccessfullyCommitted = false;
            }
            finally
            {
                if (transactionSuccessfullyCommitted)
                {
                    Console.WriteLine(
                        "Transaction successfully committed.");
                }
            }
        }
    }
}

// Executing method Main() produces the following output:
// 
// Transaction successfully committed.

You can also address specific advanced scenarios by defining your own file manager. For details on how to proceed, see the following documentation).