PageRenderTime 68ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Bifrost.Ninject/Container.cs

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