Giter Site home page Giter Site logo

bubdm / .net_transactional_file_manager Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thankyouaup/.net_transactional_file_manager

0.0 0.0 0.0 17 KB

Transactional File Manager is a .NET API that supports including file system operations such as file copy, move, delete, append, etc. It fork form codeplex '.NET Transactional File Manager( https://transactionalfilemgr.codeplex.com/ )'.

License: MIT License

C# 100.00%

.net_transactional_file_manager's Introduction

※ It fork form codeplex ' .NET Transactional File Manager '.


.NET Transactional File Manager

1. 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 transaction
TxFileManager fileMgr = new TxFileManager();
using (TransactionScope scope1 = new TransactionScope())
{
	// Copy a file
	fileMgr.Copy(srcFileName, destFileName);

	// Insert a database record
	dbMgr.ExecuteNonQuery(insertSql);

	scope1.Complete();
}

2. 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). For more details, you can refer to the Chinh Do blog.

3. Code example

// 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.

}

4. License

Copyright (c) 2008-2013 Chinh Do

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

.net_transactional_file_manager's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.