Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace NServiceBus.Lamar
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using global::Lamar;
- using Microsoft.Extensions.DependencyInjection;
- using NServiceBus;
- public class LamarObjectBuilder : ObjectBuilder.Common.IContainer
- {
- private readonly IContainer container;
- private readonly bool isContainerOwner;
- private readonly ISet<Type> configuredInstances = new HashSet<Type>();
- public LamarObjectBuilder()
- {
- this.container = new Container(c =>
- {
- });
- this.isContainerOwner = true;
- }
- public LamarObjectBuilder(IContainer container)
- : this (container, false)
- {
- }
- protected LamarObjectBuilder(IContainer container, bool isContainerOwner)
- {
- this.container = container;
- this.isContainerOwner = isContainerOwner;
- }
- public object Build(Type typeToBuild)
- {
- if(!this.container.Model.HasRegistrationFor(typeToBuild))
- {
- throw new ArgumentException(typeToBuild + " is not registered in the container");
- }
- return this.container.GetInstance(typeToBuild);
- }
- public IEnumerable<object> BuildAll(Type typeToBuild)
- {
- return this.container.GetAllInstances(typeToBuild).Cast<object>();
- }
- public NServiceBus.ObjectBuilder.Common.IContainer BuildChildContainer()
- {
- return new LamarObjectBuilder(this.container.GetNestedContainer() as IContainer, true);
- }
- public void Configure(Type component, DependencyLifecycle dependencyLifecycle)
- {
- lock (this.configuredInstances)
- {
- if (this.configuredInstances.Contains(component))
- {
- return;
- }
- }
- var lifecycle = GetLifetime(dependencyLifecycle);
- this.container.Configure(services =>
- {
- var serviceRegistry = services as ServiceRegistry;
- if(serviceRegistry == null)
- {
- throw new InvalidOperationException("cannot configure services without service registry.");
- }
- var configuredInstance = serviceRegistry.For(component)
- .Use(component);
- configuredInstance.Lifetime = lifecycle;
- serviceRegistry.Policies.SetAllProperties(p => p.TypeMatches(t => t == component));
- var interfaces = GetAllInterfacesImplementedBy(component).ToList();
- foreach (var implementedInterface in interfaces)
- {
- var configuredInterfaceInstance = serviceRegistry.For(implementedInterface)
- .Use(component);
- configuredInterfaceInstance.Lifetime = lifecycle;
- serviceRegistry.Policies.SetAllProperties(p => p.TypeMatches(t => t == implementedInterface));
- }
- });
- lock (configuredInstances)
- {
- configuredInstances.Add(component);
- }
- }
- public void Configure<T>(Func<T> component, DependencyLifecycle dependencyLifecycle)
- {
- if (typeof(T).IsValueType)
- {
- throw new ArgumentException("Invalid type");
- }
- var pluginType = typeof(T);
- lock (configuredInstances)
- {
- if (configuredInstances.Contains(pluginType))
- {
- return;
- }
- }
- var lifecycle = GetLifetime(dependencyLifecycle);
- container.Configure(services =>
- {
- var serviceRegistry = services as ServiceRegistry;
- if (serviceRegistry == null)
- {
- throw new InvalidOperationException("cannot configure services without service registry.");
- }
- serviceRegistry.Add(new ServiceDescriptor(pluginType, s => component.Invoke(), lifecycle));
- serviceRegistry.Policies.SetAllProperties(p => p.TypeMatches(t => t == pluginType));
- foreach (var implementedInterface in GetAllInterfacesImplementedBy(pluginType))
- {
- var configuredInterfaceInstance = serviceRegistry.For(implementedInterface).Use(pluginType);
- serviceRegistry.Policies.SetAllProperties(p => p.TypeMatches(t => t == implementedInterface));
- }
- });
- lock (configuredInstances)
- {
- configuredInstances.Add(pluginType);
- }
- }
- public void Dispose()
- {
- if (this.isContainerOwner)
- {
- this.container.Dispose();
- }
- }
- public bool HasComponent(Type componentType)
- {
- return this.container.Model.HasRegistrationFor(componentType);
- }
- public void RegisterSingleton(Type lookupType, object instance)
- {
- this.container.Configure((services) => {
- services.AddSingleton(lookupType, instance);
- var serviceRegistry = services as ServiceRegistry;
- if (serviceRegistry == null)
- {
- throw new InvalidOperationException("cannot configure services without service registry.");
- }
- serviceRegistry.Policies.SetAllProperties(p => p.TypeMatches(t => t == lookupType));
- });
- }
- public void Release(object instance)
- {
- }
- private static ServiceLifetime GetLifetime(DependencyLifecycle dependencyLifecycle)
- {
- switch (dependencyLifecycle)
- {
- case DependencyLifecycle.InstancePerCall:
- return ServiceLifetime.Transient;
- case DependencyLifecycle.SingleInstance:
- return ServiceLifetime.Singleton;
- case DependencyLifecycle.InstancePerUnitOfWork:
- return ServiceLifetime.Scoped;
- }
- throw new ArgumentException("Unhandled lifecycle - " + dependencyLifecycle);
- }
- private static IEnumerable<Type> GetAllInterfacesImplementedBy(Type t)
- {
- return t.GetInterfaces().Where(x => !x.FullName.StartsWith("System."));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement