PageRenderTime 26ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/Source/Bifrost.Unity/Container.cs

#
C# | 151 lines | 123 code | 27 blank | 1 comment | 2 complexity | 9d971dcca5ce992f4eaefb356a5b3bad MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using Bifrost.Execution;
  5using Microsoft.Practices.Unity;
  6
  7
  8namespace Bifrost.Unity
  9{
 10    public class Container : IContainer
 11    {
 12        readonly IUnityContainer _unityContainer;
 13
 14        public Container(IUnityContainer unityContainer)
 15        {
 16            _unityContainer = unityContainer;
 17        }
 18
 19        public T Get<T>()
 20        {
 21            return (T)Get(typeof(T), false);
 22        }
 23
 24        public T Get<T>(bool optional)
 25        {
 26            return (T)Get(typeof(T), optional);
 27        }
 28
 29        public object Get(Type type)
 30        {
 31            return Get(type, false);
 32        }
 33
 34        public object Get(Type type, bool optional)
 35        {
 36            // Todo : Figure out a way to resolve types "optionally"
 37            try
 38            {
 39                return _unityContainer.Resolve(type);
 40            }
 41            catch(Exception ex)
 42            {
 43                if (!optional)
 44                    throw ex;
 45                else
 46                    return null;
 47            }
 48        }
 49
 50        public IEnumerable<T> GetAll<T>()
 51        {
 52            return _unityContainer.ResolveAll<T>();
 53        }
 54
 55        public bool HasBindingFor(Type type)
 56        {
 57            return _unityContainer.IsRegistered(type);
 58        }
 59
 60        public bool HasBindingFor<T>()
 61        {
 62            return _unityContainer.IsRegistered<T>();
 63        }
 64
 65        public IEnumerable<object> GetAll(Type type)
 66        {
 67            return _unityContainer.ResolveAll(type);
 68        }
 69
 70        public IEnumerable<Type> GetBoundServices()
 71        {
 72            var query = from r in _unityContainer.Registrations
 73                        select r.RegisteredType;
 74            return query;
 75        }
 76
 77        public void Bind(Type service, Func<Type> resolveCallback)
 78        {
 79            throw new NotImplementedException();
 80        }
 81
 82        public void Bind<T>(Func<Type> resolveCallback)
 83        {
 84            throw new NotImplementedException();
 85        }
 86
 87        public void Bind(Type service, Func<Type> resolveCallback, BindingLifecycle lifecycle)
 88        {
 89            throw new NotImplementedException();
 90        }
 91
 92        public void Bind<T>(Func<Type> resolveCallback, BindingLifecycle lifecycle)
 93        {
 94            throw new NotImplementedException();
 95        }
 96
 97        public void Bind<T>(Type type)
 98        {
 99            _unityContainer.RegisterType(typeof(T), type);
100        }
101
102        public void Bind(Type service, Type type)
103        {
104            _unityContainer.RegisterType(service, type);
105        }
106
107        public void Bind<T>(Type type, BindingLifecycle lifecycle)
108        {
109            _unityContainer.RegisterType(typeof(T), type);
110        }
111
112        public void Bind(Type service, Type type, BindingLifecycle lifecycle)
113        {
114            _unityContainer.RegisterType(service, type);
115        }
116
117        public void Bind<T>(T instance)
118        {
119            _unityContainer.RegisterInstance<T>(instance);
120        }
121
122        public void Bind(Type service, object instance)
123        {
124            _unityContainer.RegisterInstance(service, instance);
125        }
126
127        LifetimeManager GetLifetimeManagerFromBindingLifecycle(BindingLifecycle lifecycle)
128        {
129            switch (lifecycle)
130            {
131                case BindingLifecycle.Singleton:
132                    {
133                        return new ContainerControlledLifetimeManager();
134                    } break;
135                case BindingLifecycle.Thread : 
136                    {
137                        return new PerThreadLifetimeManager();
138                    } break;
139                case BindingLifecycle.Transient:
140                    {
141                        return new TransientLifetimeManager();
142                    } break;
143            }
144
145
146            
147
148            return new PerResolveLifetimeManager();
149        }
150    }
151}