Click here to Skip to main content
15,896,606 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.6K   626   41  
A fully functional, yet minimalistic inversion of control container
using System;
using System.Collections.Generic;
using System.Text;
using Simple.IoC.Factories;

namespace Simple.IoC.Loaders
{
    public class AutoFactoryLoader : BaseFactoryLoader
    {
        private static readonly Dictionary<LifecycleType, Type> 
            _typeMap = new Dictionary<LifecycleType, Type>();
        static AutoFactoryLoader()
        {
            _typeMap[LifecycleType.OncePerRequest] = typeof (OncePerRequestFactory<,>);
            _typeMap[LifecycleType.OncePerThread] = typeof (OncePerThreadFactory<,>);
            _typeMap[LifecycleType.Singleton] = typeof (SingletonFactory<,>);
        }
        protected override object CreateFactory(Type factoryInterfaceType, Type implementingType, Type serviceType)
        {
            object factoryInstance = null;
            object[] attributes = implementingType.GetCustomAttributes(typeof (ImplementsAttribute), true);
            foreach(object attribute in attributes)
            {
                ImplementsAttribute currentAttribute = attribute as ImplementsAttribute;
                if (currentAttribute == null)
                    continue;

                // Match the factory with the service type and instantiate it
                if (currentAttribute.ServiceType != serviceType)
                    continue;

                Type factoryGenericType = _typeMap[currentAttribute.LifeCycleType];
                Type factoryType = factoryGenericType.MakeGenericType(serviceType, implementingType);
                factoryInstance = Activator.CreateInstance(factoryType);
                break;
            }

            return factoryInstance;
        }

        public override void LoadFactory(IContainer container, Type loadedType)
        {
            // Load any custom factories associated with the assembly
            CustomFactoryLoader factoryLoader = new CustomFactoryLoader();
            factoryLoader.LoadFactory(container, loadedType);

            base.LoadFactory(container, loadedType);
        }
        protected override bool CanLoad(Type loadedType)
        {
            if (loadedType.IsAbstract || loadedType.IsInterface || !loadedType.IsClass)
                return false;

            if (!loadedType.IsDefined(typeof(ImplementsAttribute), true))
                return false;

            return true;
        }

        protected override IEnumerable<Type> GetItemTypes(Type currentType)
        {
            List<Type> results = new List<Type>();
            foreach (object currentAttribute in currentType.GetCustomAttributes(true))
            {
                if (!(currentAttribute is ImplementsAttribute))
                    continue;

                ImplementsAttribute attribute = currentAttribute as ImplementsAttribute;

                results.Add(attribute.ServiceType);
            }

            return results;
        }
    }
}

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