Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C#

Introducing the LinFu Framework, Part IV: Simple.IOC – The Five Minute Inversion of Control Container

Rate me:
Please Sign up or sign in to vote.
4.89/5 (25 votes)
15 Nov 2007LGPL312 min read 74.5K   626   41  
A fully functional, yet minimalistic inversion of control container
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;

namespace Simple.IoC.Loaders
{
    public abstract class BaseFactoryLoader : IFactoryLoader 
    {
        #region IFactoryLoader Members

        public virtual void LoadFactory(IContainer container, Type loadedType)
        {
            if (!CanLoad(loadedType))
                return;

            IEnumerable<Type> itemTypes = GetItemTypes(loadedType);

			foreach(Type itemType in itemTypes)
			{
	            Debug.Assert(itemType != null);
	            if (itemType == null)
	                continue;

	            Type factoryType = GetFactoryType(itemType);

	            // Create the factory itself
	            object instance = CreateFactory(factoryType, loadedType, itemType);
	            object factoryInstance = Cast(factoryType, instance);
			    
	            if (factoryInstance == null)
	                continue;

	            // Add the object factory to the container
	            MethodInfo addFactoryDefinition =
	                typeof(BaseFactoryLoader).GetMethod("AddFactory", BindingFlags.NonPublic | BindingFlags.Static);

	            Debug.Assert(addFactoryDefinition.IsGenericMethodDefinition);

	            MethodInfo addFactory = addFactoryDefinition.MakeGenericMethod(itemType);
	            addFactory.Invoke(null, new object[] { factoryInstance, container });
            }
        }

        #endregion

        protected abstract object CreateFactory(Type factoryType, Type currentType, Type serviceType);
        protected abstract bool CanLoad(Type loadedType);
        protected virtual Type GetFactoryType(Type currentType)
        {
            return typeof (IFactory<>).MakeGenericType(currentType);
        }
        protected abstract IEnumerable<Type> GetItemTypes(Type currentType);

        #region Private Members
        private static void AddFactory<T>(object factoryInstance, IContainer container)
        {
            IFactory<T> factory = factoryInstance as IFactory<T>;
            container.AddFactory(factory);
        }
        private static object Cast(Type targetType, object instance)
        {
            MethodInfo castMethodDefinition = typeof(BaseFactoryLoader).GetMethod("CastAs", BindingFlags.NonPublic | BindingFlags.Static);
            Debug.Assert(castMethodDefinition != null);

            MethodInfo castMethod = castMethodDefinition.MakeGenericMethod(targetType);
            return castMethod.Invoke(null, new object[] { instance });
        }
        private static object CastAs<T>(object instance)
            where T : class
        {
            return instance as T;
        }
        #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, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Readify
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions