PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Bifrost.Unity/Container.cs

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