PageRenderTime 33ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/Source/Bifrost.Ninject/Container.cs

#
C# | 124 lines | 102 code | 22 blank | 0 comment | 1 complexity | d02e9c963b8e4e3fd968d1d13a96d2ee MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using Bifrost.Execution;
  5using Ninject;
  6using Ninject.Parameters;
  7
  8namespace Bifrost.Ninject
  9{
 10    public class Container : IContainer
 11    {
 12        readonly List<Type> _boundServices;
 13
 14        public Container(IKernel kernel)
 15        {
 16            Kernel = kernel;
 17            _boundServices = new List<Type>();
 18        }
 19
 20        public IKernel Kernel { get; private set; }
 21
 22        public T Get<T>()
 23        {
 24            return (T)Get(typeof(T), false);
 25        }
 26
 27        public T Get<T>(bool optional)
 28        {
 29            return (T)Get(typeof(T), optional);
 30        }
 31
 32        public object Get(Type type)
 33        {
 34            return Get(type, false);
 35        }
 36
 37        public object Get(Type type, bool optional)
 38        {
 39            var request = Kernel.CreateRequest(type, null, new IParameter[0], optional, true);
 40            return Kernel.Resolve(request).SingleOrDefault();
 41        }
 42
 43        public IEnumerable<T> GetAll<T>()
 44        {
 45            return Kernel.GetAll<T>();
 46        }
 47
 48        public bool HasBindingFor(Type type)
 49        {
 50            return Kernel.GetBindings(type).Count() != 0;
 51        }
 52
 53        public bool HasBindingFor<T>()
 54        {
 55            return HasBindingFor(typeof (T));
 56        }
 57
 58        public IEnumerable<object> GetAll(Type type)
 59        {
 60            return Kernel.GetAll(type);
 61        }
 62
 63        public IEnumerable<Type> GetBoundServices()
 64        {
 65            return _boundServices;
 66        }
 67
 68        public void Bind(Type type, Func<Type> resolveCallback)
 69        {
 70            throw new NotImplementedException();
 71        }
 72
 73        public void Bind<T>(Func<Type> resolveCallback)
 74        {
 75            throw new NotImplementedException();
 76        }
 77
 78        public void Bind(Type type, Func<Type> resolveCallback, BindingLifecycle lifecycle)
 79        {
 80            throw new NotImplementedException();
 81        }
 82
 83        public void Bind<T>(Func<Type> resolveCallback, BindingLifecycle lifecycle)
 84        {
 85            throw new NotImplementedException();
 86        }
 87
 88        public void Bind<T>(Type type)
 89        {
 90            Kernel.Bind<T>().To(type);
 91            _boundServices.Add(typeof(T));
 92        }
 93
 94        public void Bind(Type service, Type type)
 95        {
 96            Kernel.Bind(service).To(type);
 97            _boundServices.Add(service);
 98        }
 99
100        public void Bind<T>(Type type, BindingLifecycle lifecycle)
101        {
102            Kernel.Bind<T>().To(type).WithLifecycle(lifecycle);
103            _boundServices.Add(typeof(T));
104        }
105
106        public void Bind(Type service, Type type, BindingLifecycle lifecycle)
107        {
108            Kernel.Bind(service).To(type).WithLifecycle(lifecycle);
109            _boundServices.Add(service);
110        }
111
112        public void Bind<T>(T instance)
113        {
114            Kernel.Bind<T>().ToConstant(instance);
115            _boundServices.Add(typeof(T));
116        }
117
118        public void Bind(Type service, object instance)
119        {
120            Kernel.Bind(service).ToConstant(instance);
121            _boundServices.Add(service);
122        }
123    }
124}