檔案事物管理Transactional File Manager的執行個體詳解

來源:互聯網
上載者:User
推薦一個檔案事物管理 Transactional File Manager

Project Description

Transactional File Manager is a .NET API that supports including file system operations such as file copy, move, delete, append, etc. in a transaction. It's an implementation of System.Transaction.IEnlistmentNotification (works with System.Transactions.TransactionScope).

This library allows you to wrap file system operations in transactions like this:

// Wrap a file copy and a database insert in the same transactionTxFileManager fileMgr = new TxFileManager();using (TransactionScope scope1 = new TransactionScope()){// Copy a filefileMgr.Copy(srcFileName, destFileName);// Insert a database recorddbMgr.ExecuteNonQuery(insertSql);scope1.Complete();}

Current Features

  • Support the following file operations in transactions:

    • AppendAllText

    • Copy

    • CreateDirectory

    • DeleteDirectory

    • DeleteFile

    • Move

    • Snapshot

    • WriteAllText

    • WriteAllBytes

This library supports any file system and is not a wrapper over Transactional NTFS (see AlphaFS).

Examples

// Completely unrealistic example showing how various file operations, including operations done // by library/3rd party code, can participate in transactions.IFileManager fileManager = new TxFileManager();using (TransactionScope scope1 = new TransactionScope()){    fileManager.WriteAllText(inFileName, xml);    // Snapshot allows any file operation to be part of our transaction.    // All we need to know is the file name.    //The statement below tells the TxFileManager to remember the state of this file.    // So even though XslCompiledTransform has no knowledge of our TxFileManager, the file it creates (outFileName)    // will still be restored to this state in the event of a rollback.    fileManager.Snapshot(outFileName);    XslCompiledTransform xsl = new XslCompiledTransform(true);    xsl.Load(uri);    xsl.Transform(inFileName, outFileName);    // write to database 1. This database op will get committed/rolled back along with the file operations we are doing in this transaction.    myDb1.ExecuteNonQuery(sql1);    // write to database 2. The transaction is promoted to a distributed transaction here.    myDb2.ExecuteNonQuery(sql2);    // let's delete some files    for (string fileName in filesToDelete)    {        fileManager.Delete(fileName);    }    // Just for kicks, let's start a new nested  transaction. Since we specify RequiresNew here, this nested transaction    // will be committed/rolled back separately from the main transaction.    // Note that we can still use the same fileManager instance. It knows how to sort things out correctly.    using (TransactionScope scope2 = new TransactionScope(TransactionScopeOptions.RequiresNew))    {        fileManager.MoveFile(anotherFile, anotherFileDest);    }    // move some files    for (string fileName in filesToMove)    {        fileManager.Move(fileName, GetNewFileName(fileName));    }    // Finally, let's create a few temporary files...    // disk space has to be used for something.    // The nice thing about FileManager.GetTempFileName is that    // The temp file will be cleaned up automatically for you when the TransactionScope completes.    // No more worries about temp files that get left behind.    for (int i=0; i<10; i++)    {        fileManager.WriteAllText(fileManager.GetTempFileName(), "testing 1 2");    }    scope1.Complete();    // In the event an exception occurs, everything done here will be rolled back including the output xsl file.}

這是一個開源項目。原始專案網站是 事務檔案管理工具。

著作權(c)2008-2013 Chinh Do

特此授予任何獲得本軟體和相關文檔檔案(“軟體”)副本的人免費許可,無限制地處理本軟體,包括但不限於使用,複製,修改,合并的權利,發布,分發,再授權和/或出售本軟體的副本,並允許提供本軟體的人員遵守以下條件:

上述著作權聲明和本許可聲明應包含在本軟體的所有副本或主要部分。

該軟體“按原樣”提供,不附帶任何明示或暗示的保證,包括但不限於適銷性,適用於特定用途和不侵權的保證。在任何情況下,作者或著作權者均不對任何索賠,損害或其他責任負責,無論是否因與本軟體或本軟體的使用或其他交易相關的任何合約,侵權行為或其他方面的行為軟體。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.