home | downloads | recipes | blogs | staff | about us | *

The Art of Lightning
Revenge of the Free Code - Castle Microkernel ContainerJames Zimmerman4/8/2008

Have at this one. It's an implementation of IServiceContainer that proxies the Castle Project Microkernel DI container. This allows you to swap between implementations of containers based on the inherent .Net service container interface. The unit tests leverage the base type I've provided here

The Code

//Copyright (c) 2008 James Zimmerman

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

using System;
using System.ComponentModel.Design;
using Castle.MicroKernel;

/// <summary>
/// Concrete implementation of <see cref="IServiceContainer"/> that leverages the <see cref="IKernel"/> system.
/// </summary>
public class CastleServiceContainer : IServiceContainer
{
    private readonly IKernel kernel = new DefaultKernel();

    #region IServiceContainer Members

    ///<summary>
    ///Adds the specified service to the service container.
    ///</summary>
    ///
    ///<param name="serviceType">The type of service to add. </param>
    ///<param name="serviceInstance">An instance of the service type to add. This object must implement or inherit from the type indicated by the <paramref name="serviceType" /> parameter. </param>
    public void AddService(Type serviceType, Object serviceInstance)
    {
        if (serviceType == null) throw new ArgumentNullException( "serviceType" );
        if (serviceInstance == null) throw new ArgumentNullException( "serviceInstance" );

        kernel.AddComponentInstance( serviceType.FullName, serviceType, serviceInstance );
    }

    ///<summary>
    ///Adds the specified service to the service container, and optionally promotes the service to any parent service containers.
    ///</summary>
    ///
    ///<param name="serviceType">The type of service to add. </param>
    ///<param name="serviceInstance">An instance of the service type to add. This object must implement or inherit from the type indicated by the <paramref name="serviceType" /> parameter. </param>
    ///<param name="promote">true to promote this request to any parent service containers; otherwise, false. </param>
    public void AddService(Type serviceType, Object serviceInstance, Boolean promote)
    {
        this.AddService( serviceType, serviceInstance );
    }

    ///<summary>
    ///Adds the specified service to the service container.
    ///</summary>
    ///
    ///<param name="serviceType">The type of service to add. </param>
    ///<param name="callback">A callback object that is used to create the service. This allows a service to be declared as available, but delays the creation of the object until the service is requested. </param>
    public void AddService(Type serviceType, ServiceCreatorCallback callback)
    {
        if (serviceType == null) throw new ArgumentNullException( "serviceType" );
        if (callback == null) throw new ArgumentNullException( "callback" );
    }

    ///<summary>
    ///Adds the specified service to the service container, and optionally promotes the service to parent service containers.
    ///</summary>
    ///
    ///<param name="serviceType">The type of service to add. </param>
    ///<param name="callback">A callback object that is used to create the service. This allows a service to be declared as available, but delays the creation of the object until the service is requested. </param>
    ///<param name="promote">true to promote this request to any parent service containers; otherwise, false. </param>
    public void AddService(Type serviceType, ServiceCreatorCallback callback, bool promote)
    {
        throw new NotImplementedException();
    }

    ///<summary>
    ///Removes the specified service type from the service container.
    ///</summary>
    ///
    ///<param name="serviceType">The type of service to remove. </param>
    public void RemoveService(Type serviceType)
    {
        this.kernel.RemoveComponent( serviceType.FullName );
    }

    ///<summary>
    ///Removes the specified service type from the service container, and optionally promotes the service to parent service containers.
    ///</summary>
    ///
    ///<param name="serviceType">The type of service to remove. </param>
    ///<param name="promote">true to promote this request to any parent service containers; otherwise, false. </param>
    public void RemoveService(Type serviceType, Boolean promote)
    {
        this.RemoveService( serviceType );
    }

    #endregion

    #region IServiceProvider Members

    ///<summary>
    ///Gets the service object of the specified type.
    ///</summary>
    ///
    ///<returns>
    ///A service object of type <paramref name="serviceType" />.-or- null if there is no service object of type <paramref name="serviceType" />.
    ///</returns>
    ///
    ///<param name="serviceType">An object that specifies the type of service object to get. </param><filterpriority>2</filterpriority>
    public Object GetService(Type serviceType)
    {
        if (typeof(IServiceContainer).IsAssignableFrom( serviceType )) return this;

        try
        {
            return this.kernel[serviceType];
        }
        catch (ComponentNotFoundException)
        {
            return null;
        }
    }

    #endregion
}

The Tests

[TestFixture()]
public class CastleServiceContainerTest : ServiceContainerTestsBase
{
    protected override IServiceContainer CreateContainer()
    {
        return new CastleServiceContainer();
    }
}

Jimmy Zimms wants some more coffee
 

Look for more blogs |

(c) 2001-2009 Stuart Williams, E-Mail Stuart Williams