Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#

Introducing Castle - Part I

Rate me:
Please Sign up or sign in to vote.
4.90/5 (56 votes)
30 Dec 200414 min read 270.6K   1.5K   167  
Introduces the benefits of using an inversion of control approach to design a system.
// Copyright 2004 DigitalCraftsmen - http://www.digitalcraftsmen.com.br/
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Extending2.Components
{
	using System;
	using System.Data;
	using System.Collections;

	using Castle.Model;

	[PerThread]
	public class DefaultTransactionManager : ITransactionManager
	{
		private SimpleTransaction _current;

		public ITransaction CreateTransaction()
		{
			_current = new SimpleTransaction();
			return _current;
		}

		public ITransaction CurrentTransaction
		{
			get { return _current; }
		}

		public void Release(ITransaction transaction)
		{
			// We could ensure that the transaction
			// was properly closed here

			(transaction as SimpleTransaction).Dispose();

			if (transaction == _current)
			{
				_current = null;
			}
		}
	}

	public class SimpleTransaction : ITransaction, IDisposable
	{
		IList _enlistedResources = new ArrayList();

		public void Enlist(IDbConnection connection)
		{
			_enlistedResources.Add( connection.BeginTransaction() );
		}

		public void Enlist(IDbCommand command)
		{
			foreach(IDbTransaction transaction in _enlistedResources)
			{
				if (transaction.Connection == command.Connection)
				{
					command.Transaction = transaction;
					break;
				}
			}
		}

		public void Commit()
		{
			foreach(IDbTransaction transaction in _enlistedResources)
			{
				if (transaction.Connection.State != ConnectionState.Closed)
				{
					transaction.Commit();
				}
			}
		}

		public void Rollback()
		{
			foreach(IDbTransaction transaction in _enlistedResources)
			{
				if (transaction.Connection.State != ConnectionState.Closed)
				{
					transaction.Rollback();
				}
			}
		}

		public void Dispose()
		{
			foreach(IDbTransaction transaction in _enlistedResources)
			{
				if (transaction.Connection.State != ConnectionState.Closed)
				{
					transaction.Connection.Close();
				}
			}

			_enlistedResources.Clear();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Brazil Brazil
Hamilton Verissimo has been working with software development for 9 years. He's involved with a lot of open source projects to fill his spare time.

Comments and Discussions