Click here to Skip to main content
15,885,278 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.7K   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 Extending1.Components
{
	using System;
	using System.Data;

	/// <summary>
	/// Example of a real connection factory class.
	/// Note that the Type of the connection is a property
	/// of the class, so its easy to change to 
	/// SqlConnection, Odbc, Oracle, MySql etc. 
	/// </summary>
	public class DefaultConnectionFactory : IConnectionFactory
	{
		private Type connectionType = typeof(FakeConnection);
		private String _connectionString;

		public DefaultConnectionFactory( String connectionString )
		{
			_connectionString = connectionString;
		}

		public Type ConnectionType
		{
			get { return connectionType; }
			set { connectionType = value; }
		}

		public IDbConnection CreateConnection()
		{
			return new FakeConnection();
		}
	}

	class FakeConnection : IDbConnection
	{
		#region IDbConnection Members

		public void ChangeDatabase(string databaseName)
		{
		}

		public IDbTransaction BeginTransaction(System.Data.IsolationLevel il)
		{
			return null;
		}

		IDbTransaction System.Data.IDbConnection.BeginTransaction()
		{
			return null;
		}

		public System.Data.ConnectionState State
		{
			get { return new System.Data.ConnectionState(); }
		}

		public string ConnectionString
		{
			get { return null; }
			set
			{
			}
		}

		public IDbCommand CreateCommand()
		{
			return null;
		}

		public void Open()
		{
		}

		public void Close()
		{
		}

		public string Database
		{
			get { return null; }
		}

		public int ConnectionTimeout
		{
			get { return 0; }
		}

		#endregion

		#region IDisposable Members

		public void Dispose()
		{
		}

		#endregion
	}
}

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