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.
view raw
IoC.cs This Gist brought to you by
GitHub.
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
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); } }} |
IoC.cs This Gist brought to you by
GitHub.
public static partial class IoC{ static partial void RegisterAll() { Register<IMyInterface, IMyImplementation>(); Register<IMyComponent>(); }} |
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
相关文章推荐
- pugixml Light-weight, simple and fast XML parser for C++ with XPath support
- Lets write a Tiny IoC Container to learn and for fun
- IoC容器和 Dependency Injection模式 Inversion of Control Containers and the Dependency Injection pattern
- Useful and Simple srcipts for Oracle CoolBackup
- 关于控制反转IoC--Inversion of Control Containers and the Dependency Injection pattern
- Spring 依赖注入(hint: specify index and/or type arguments for simple parameters to avoid type ambiguiti)
- [译] Perceptual Losses for Real-Time Style Transfer and Super-Resolution(Stanford University)
- J2EE without EJB 读书笔记 -- Light weight Container & IoC
- CCS算法(Contour-Constrained Superpixels for Image and Video Processing)
- 论文解读之Perceptual Losses for Real-Time Style Transfer and Super-Resolution
- <Ejb3InAction> Sample02 for Glassfish - Simple example of Stateless, Stateful, MDB and JPA Enity
- 论文总结-Perceptual Losses for Real-Time Style Transfer and Super-Resolution
- REST Client – simple DSL for accessing HTTP and REST resources
- 部分选译Perceptual losses for real-time style transfer and super-resolution.
- Creating a simple Pivot table using LINQ and Telerik RadTreeView for Silverlight
- [eShopOnContainers 学习系列] - 01 - Roadmap and Milestones for future releases
- C#: Writing a CookieContainer to Disk and Loading Back In For Use
- I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum o
- 超级IOC容器SuperContainer
- MWPhotoBrowser, simple and elegant image library for iOS