Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. namespace NServiceBus.Lamar
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using global::Lamar;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using NServiceBus;
  9.  
  10. public class LamarObjectBuilder : ObjectBuilder.Common.IContainer
  11. {
  12. private readonly IContainer container;
  13. private readonly bool isContainerOwner;
  14. private readonly ISet<Type> configuredInstances = new HashSet<Type>();
  15. public LamarObjectBuilder()
  16. {
  17. this.container = new Container(c =>
  18. {
  19. });
  20. this.isContainerOwner = true;
  21. }
  22.  
  23. public LamarObjectBuilder(IContainer container)
  24. : this (container, false)
  25. {
  26. }
  27.  
  28. protected LamarObjectBuilder(IContainer container, bool isContainerOwner)
  29. {
  30. this.container = container;
  31. this.isContainerOwner = isContainerOwner;
  32. }
  33.  
  34. public object Build(Type typeToBuild)
  35. {
  36. if(!this.container.Model.HasRegistrationFor(typeToBuild))
  37. {
  38. throw new ArgumentException(typeToBuild + " is not registered in the container");
  39. }
  40. return this.container.GetInstance(typeToBuild);
  41. }
  42.  
  43. public IEnumerable<object> BuildAll(Type typeToBuild)
  44. {
  45. return this.container.GetAllInstances(typeToBuild).Cast<object>();
  46. }
  47.  
  48. public NServiceBus.ObjectBuilder.Common.IContainer BuildChildContainer()
  49. {
  50. return new LamarObjectBuilder(this.container.GetNestedContainer() as IContainer, true);
  51. }
  52.  
  53. public void Configure(Type component, DependencyLifecycle dependencyLifecycle)
  54. {
  55. lock (this.configuredInstances)
  56. {
  57. if (this.configuredInstances.Contains(component))
  58. {
  59. return;
  60. }
  61. }
  62.  
  63. var lifecycle = GetLifetime(dependencyLifecycle);
  64.  
  65. this.container.Configure(services =>
  66. {
  67. var serviceRegistry = services as ServiceRegistry;
  68. if(serviceRegistry == null)
  69. {
  70. throw new InvalidOperationException("cannot configure services without service registry.");
  71. }
  72.  
  73. var configuredInstance = serviceRegistry.For(component)
  74. .Use(component);
  75.  
  76. configuredInstance.Lifetime = lifecycle;
  77.  
  78. serviceRegistry.Policies.SetAllProperties(p => p.TypeMatches(t => t == component));
  79.  
  80. var interfaces = GetAllInterfacesImplementedBy(component).ToList();
  81.  
  82. foreach (var implementedInterface in interfaces)
  83. {
  84. var configuredInterfaceInstance = serviceRegistry.For(implementedInterface)
  85. .Use(component);
  86.  
  87. configuredInterfaceInstance.Lifetime = lifecycle;
  88. serviceRegistry.Policies.SetAllProperties(p => p.TypeMatches(t => t == implementedInterface));
  89. }
  90. });
  91.  
  92. lock (configuredInstances)
  93. {
  94. configuredInstances.Add(component);
  95. }
  96. }
  97.  
  98. public void Configure<T>(Func<T> component, DependencyLifecycle dependencyLifecycle)
  99. {
  100. if (typeof(T).IsValueType)
  101. {
  102. throw new ArgumentException("Invalid type");
  103. }
  104.  
  105. var pluginType = typeof(T);
  106.  
  107. lock (configuredInstances)
  108. {
  109. if (configuredInstances.Contains(pluginType))
  110. {
  111. return;
  112. }
  113. }
  114.  
  115. var lifecycle = GetLifetime(dependencyLifecycle);
  116.  
  117. container.Configure(services =>
  118. {
  119. var serviceRegistry = services as ServiceRegistry;
  120. if (serviceRegistry == null)
  121. {
  122. throw new InvalidOperationException("cannot configure services without service registry.");
  123. }
  124.  
  125. serviceRegistry.Add(new ServiceDescriptor(pluginType, s => component.Invoke(), lifecycle));
  126.  
  127. serviceRegistry.Policies.SetAllProperties(p => p.TypeMatches(t => t == pluginType));
  128.  
  129. foreach (var implementedInterface in GetAllInterfacesImplementedBy(pluginType))
  130. {
  131. var configuredInterfaceInstance = serviceRegistry.For(implementedInterface).Use(pluginType);
  132.  
  133. serviceRegistry.Policies.SetAllProperties(p => p.TypeMatches(t => t == implementedInterface));
  134. }
  135. });
  136.  
  137. lock (configuredInstances)
  138. {
  139. configuredInstances.Add(pluginType);
  140. }
  141. }
  142.  
  143. public void Dispose()
  144. {
  145. if (this.isContainerOwner)
  146. {
  147. this.container.Dispose();
  148. }
  149. }
  150.  
  151. public bool HasComponent(Type componentType)
  152. {
  153. return this.container.Model.HasRegistrationFor(componentType);
  154. }
  155.  
  156. public void RegisterSingleton(Type lookupType, object instance)
  157. {
  158.  
  159. this.container.Configure((services) => {
  160. services.AddSingleton(lookupType, instance);
  161. var serviceRegistry = services as ServiceRegistry;
  162. if (serviceRegistry == null)
  163. {
  164. throw new InvalidOperationException("cannot configure services without service registry.");
  165. }
  166.  
  167. serviceRegistry.Policies.SetAllProperties(p => p.TypeMatches(t => t == lookupType));
  168. });
  169. }
  170.  
  171. public void Release(object instance)
  172. {
  173. }
  174.  
  175. private static ServiceLifetime GetLifetime(DependencyLifecycle dependencyLifecycle)
  176. {
  177. switch (dependencyLifecycle)
  178. {
  179. case DependencyLifecycle.InstancePerCall:
  180. return ServiceLifetime.Transient;
  181. case DependencyLifecycle.SingleInstance:
  182. return ServiceLifetime.Singleton;
  183. case DependencyLifecycle.InstancePerUnitOfWork:
  184. return ServiceLifetime.Scoped;
  185. }
  186.  
  187. throw new ArgumentException("Unhandled lifecycle - " + dependencyLifecycle);
  188. }
  189.  
  190. private static IEnumerable<Type> GetAllInterfacesImplementedBy(Type t)
  191. {
  192. return t.GetInterfaces().Where(x => !x.FullName.StartsWith("System."));
  193. }
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement