Click here to Skip to main content
15,896,269 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 271.3K   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
{
	using System;
	using System.Collections;

	using Castle.Model;
	using Castle.Model.Configuration;

	using Castle.MicroKernel;
	using Castle.MicroKernel.LifecycleConcerns;
	using Castle.MicroKernel.ModelBuilder;

	/// <summary>
	/// Summary description for StartableFacility.
	/// </summary>
	public class StartableFacility : IFacility
	{
		private ArrayList _waitList = new ArrayList();
		private IKernel _kernel;

		public void Init(IKernel kernel, IConfiguration facilityConfig)
		{
			_kernel = kernel;

			kernel.ComponentModelBuilder.AddContributor( new StartableInspector() );
			kernel.ComponentRegistered += 
				new ComponentDataDelegate(OnComponentRegistered);
		}

		public void Terminate()
		{
			// Nothing to do
		}

		private class StartableInspector : IContributeComponentModelConstruction
		{
			public void ProcessModel(IKernel kernel, ComponentModel model)
			{
				bool startable = 
					typeof(IStartable).IsAssignableFrom(model.Implementation);

				model.ExtendedProperties["startable"] = startable;

				if (startable)
				{
					model.LifecycleSteps.Add( 
						LifecycleStepType.Commission, new StartableConcern() );
				}
			}

			private class StartableConcern : ILifecycleConcern
			{
				public void Apply(ComponentModel model, object component)
				{
					(component as IStartable).Start();
				}
			}
		}

		private void OnComponentRegistered(String key, IHandler handler)
		{
			bool startable = (bool) 
				handler.ComponentModel.ExtendedProperties["startable"];
			
			if (startable)
			{
				if (handler.CurrentState == HandlerState.WaitingDependency)
				{
					_waitList.Add( handler );
				}
				else
				{
					Start( key );
				}
			}

			CheckWaitingList();
		}

		/// <summary>
		/// For each new component registered,
		/// some components in the WaitingDependency
		/// state may have became valid, so we check them
		/// </summary>
		private void CheckWaitingList()
		{
			IHandler[] handlers = (IHandler[]) 
				_waitList.ToArray( typeof(IHandler) );

			foreach(IHandler handler in handlers)
			{
				if (handler.CurrentState == HandlerState.Valid)
				{
					Start( handler.ComponentModel.Name );

					_waitList.Remove(handler);
				}
			}
		}

		/// <summary>
		/// Request the component instance
		/// </summary>
		/// <param name="key"></param>
		private void Start(String key)
		{
			object instance = _kernel[key];
		}
	}
}

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