您的位置:首页 > 大数据 > 人工智能

windows phone:A Really Super Light and Simple IoC Container for Windows Phone 7

2013-05-06 14:56 330 查看
转载于 http://msmvps.com/blogs/vcsjones/archive/2010/11/25/a-really-super-light-and-simple-ioc-container-for-windows-phone-7.aspx
I finally managed to get the Windows Phone 7 tools installed. I’m not going to vent on that anymore because I feel like I’ve done that enough already – and once they did install correctly they’ve been very pleasurable to use. I started working on an application,
and old habits die hard. I like Inversion of Control, it’s a major need for me simply because I’ve forced my mind to work like that. I’ve previously worked with Unity and Windsor. Unity has grown on me a lot, and I like it. However, none of them seem to work
in Windows Phone 7, or at least they weren’t designed to. So I wrote my own
really super simple IoC container for Windows Phone 7. I wanted the following features, and nothing else (for now):

Able to register types such that arguments of it’s constructor are resolved
Able to register an interface and type such that if the interface is resolved, the component is resolved and it’s constructor arguments are resolved

Assume that there is either a default constructor or a single constructor that takes parameters

Everything will have a singleton lifetime.

Super simple requirements. This will be running on phone hardware, so it needs to be lightweight, too. It fits in a single class which is about 65 lines. I’ve split it into two different files using a partial class to keep the registration separate. I can
imagine some additional features that other people might want, such as a transient lifestyle, use WeakReferences if you are registering a lot of things, etc.

public static partial class IoC{    private static readonly Dictionary<Type, Type> _registration = new Dictionary<Type, Type>();    private static readonly Dictionary<Type, object> _rot = new Dictionary<Type, object>();    private static readonly object[] _emptyArguments = new object[0];    private static readonly object _syncLock = new object();     static partial void RegisterAll();     static IoC()    {        RegisterAll();    }     public static object Resolve(Type type)    {        lock (_syncLock)        {            if (!_rot.ContainsKey(type))            {                if (!_registration.ContainsKey(type))                {                    throw new Exception("Type not registered.");                }                var resolveTo = _registration[type] ?? type;                var constructorInfos = resolveTo.GetConstructors();                if (constructorInfos.Length > 1)                    throw new Exception("Cannot resolve a type that has more than one constructor.");                var constructor = constructorInfos[0];                var parameterInfos = constructor.GetParameters();                if (parameterInfos.Length == 0)                {                    _rot[type] = constructor.Invoke(_emptyArguments);                }                else                {                    var parameters = new object[parameterInfos.Length];                    foreach (var parameterInfo in parameterInfos)                    {                        parameters[parameterInfo.Position] = Resolve(parameterInfo.ParameterType);                    }                    _rot[type] = constructor.Invoke(parameters);                }            }            return _rot[type];        }    }     public static T Resolve<T>()    {        return (T) Resolve(typeof (T));    }     public static void Register<I, C>() where C:class, I    {        lock (_syncLock)        {            _registration.Add(typeof(I), typeof(C));        }    }     public static void Register<C>() where C:class    {        lock(_syncLock)        {            _registration.Add(typeof(C), null);        }    }}

view raw

IoC.cs This Gist brought to you by
GitHub.

public static partial class IoC{    static partial void RegisterAll()    {        Register<IMyInterface, IMyImplementation>();        Register<IMyComponent>();    }}

view raw

IoC_Registration.cs This Gist brought to you by
GitHub.

For the source, see the
Gist on GitHub.

Published Thu, Nov 25 2010 19:08 by
vcsjones
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐