/// <summary> /// Create a <see cref="Unity.UnityContainer"/> with the given parent container. /// </summary> /// <param name="parent">The parent <see cref="Unity.UnityContainer"/>. The current object /// will apply its own settings first, and then check the parent for additional ones.</param> private UnityContainer(UnityContainer parent) { // Validate input Debug.Assert(null != parent, nameof(parent)); Debug.Assert(null != parent._root, nameof(parent._root)); // Register with parent _parent = parent; _root = parent._root; _parent.LifetimeContainer.Add(this); // Defaults and policies LifetimeContainer = new LifetimeContainer(this); ModeFlags = parent._root.ModeFlags; Defaults = _root.Defaults; Context = new ContainerContext(this); // Dynamic Members Register = InitAndAdd; // Validators ValidateType = _root.ValidateType; ValidateTypes = _root.ValidateTypes; DependencyResolvePipeline = _root.DependencyResolvePipeline; }
static async Task Main(string[] args) { System.AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper; if (args.Length < 2 || args.Length > 2) { Console.WriteLine("Usage: GeigerPublisher <serial port> <broker hostname>"); Console.WriteLine("Exiting..."); Environment.Exit(1); } Console.CancelKeyPress += (sender, eventArgs) => { _source.Cancel(); eventArgs.Cancel = true; }; _container = new UnityContainer(); _container.RegisterType <IGeigerPublisher, MQTTPublisher>(); _publisher = _container.Resolve <IGeigerPublisher>(); #if DEBUG _container.RegisterType <IGeigerReader, InfinityReader>(); #else _container.RegisterType <IGeigerReader, SerialReader>(); #endif _reader = _container.Resolve <IGeigerReader>(); await _publisher.ConnectAsync(args[1], _source.Token); await _reader.StartReadAsync(args[0], publishConnected, publishReading, _source.Token); _publisher?.DisconnectAsync(); Console.WriteLine("Geigerpublisher exiting..."); }
/// <inheritdoc /> public object?Get(Type type, Type policyInterface) { var key = new HashKey(type); // Iterate through containers hierarchy for (UnityContainer?container = Container; null != container; container = container._parent) { // Skip to parent if no registrations if (null == container._registry) { continue; } var registry = container._registry; var targetBucket = key.HashCode % registry.Buckets.Length; for (var i = registry.Buckets[targetBucket]; i >= 0; i = registry.Entries[i].Next) { ref var entry = ref registry.Entries[i]; if (entry.Key != key || entry.Policies is ImplicitRegistration) { continue; } return(entry.Policies.Get(policyInterface)); } }
/// <inheritdoc /> public bool IsRegistered(Type type, string?name) { int hashCode = NamedType.GetHashCode(type, name); // Iterate through hierarchy and check if exists for (UnityContainer?container = this; null != container; container = container._parent) { // Skip to parent if no registry if (null == container._metadata || null == container._registry) { continue; } // Look for exact match var registry = container._registry; var targetBucket = (hashCode & HashMask) % registry.Buckets.Length; for (var i = registry.Buckets[targetBucket]; i >= 0; i = registry.Entries[i].Next) { ref var candidate = ref registry.Entries[i]; if (candidate.HashCode != hashCode || candidate.Type != type || !(candidate.Value is ImplicitRegistration set) || set.Name != name) { continue; } return(true); } }
/// <inheritdoc /> public bool IsRegistered(Type type, string?name) { var key = new HashKey(type, name); // Iterate through hierarchy and check if exists for (UnityContainer?container = this; null != container; container = container._parent) { // Skip to parent if no registry if (null == container._metadata) { continue; } Debug.Assert(null != container._registry); var registry = container._registry; var targetBucket = key.HashCode % registry.Buckets.Length; // Look for exact match for (var i = registry.Buckets[targetBucket]; i >= 0; i = registry.Entries[i].Next) { ref var candidate = ref registry.Entries[i]; if (candidate.Key != key || !candidate.IsExplicit) { continue; } return(true); } }
private IRegistration GetSimpleRegistration(Type type, string?name) { var key = new HashKey(type, name); // Iterate through containers hierarchy for (UnityContainer?container = this; null != container; container = container._parent) { // Skip to parent if no registrations if (null == container._metadata) { continue; } Debug.Assert(null != container._registry); var registry = container._registry; // Check for exact match for (var i = registry.Buckets[key.HashCode % registry.Buckets.Length]; i >= 0; i = registry.Entries[i].Next) { ref var candidate = ref registry.Entries[i]; if (candidate.Key != key) { continue; } // Found a registration if (!(candidate.Policies is IRegistration)) { candidate.Policies = container.CreateRegistration(type, name, candidate.Policies); } return((IRegistration)candidate.Policies); } }
internal IEnumerable <TElement> ResolveEnumerable <TElement>(Func <Type, ImplicitRegistration, object?> resolve, string?name) { object?value; var set = new HashSet <string?>(); int hash = typeof(TElement).GetHashCode(); // Iterate over hierarchy for (UnityContainer?container = this; null != container; container = container._parent) { // Skip to parent if no data if (null == container._metadata || null == container._registry) { continue; } // Hold on to registries var registry = container._registry; // Get indexes and iterate over them var length = container._metadata.GetEntries <TElement>(hash, out int[]? data); if (null != data && null != registry) { for (var i = 1; i < length; i++) { var index = data[i]; var registration = (ExplicitRegistration)registry.Entries[index].Value; if (set.Add(registration.Name)) { try { value = resolve(typeof(TElement), registration); } catch (ArgumentException ex) when(ex.InnerException is TypeLoadException) { continue; } yield return((TElement)value); } } } } // If nothing registered attempt to resolve the type if (0 == set.Count) { try { var registration = GetRegistration(typeof(TElement), name); value = resolve(typeof(TElement), registration); } catch { yield break; } yield return((TElement)value); } }
internal bool IsRegistered(Type type) { var key = new HashKey(type); // Iterate through containers hierarchy for (UnityContainer?container = this; null != container; container = container._parent) { // Skip to parent if no registrations if (null == container._metadata) { continue; } var metadata = container._metadata;; var targetBucket = key.HashCode % metadata.Buckets.Length; for (var i = metadata.Buckets[targetBucket]; i >= 0; i = metadata.Entries[i].Next) { if (metadata.Entries[i].HashKey != key) { continue; } return(true); } return(false); } return(false); }
private ResolveDelegate <BuilderContext>?TryGetPipeline(ref HashKey key) { // Iterate through containers hierarchy for (UnityContainer?container = this; null != container; container = container._parent) { // Skip to parent if no registrations if (null == container._metadata) { continue; } Debug.Assert(null != container._registry); var registry = container._registry; // Check for exact match for (var i = registry.Buckets[key.HashCode % registry.Buckets.Length]; i >= 0; i = registry.Entries[i].Next) { ref var candidate = ref registry.Entries[i]; if (candidate.Key != key) { continue; } // Found it return(candidate.Pipeline); } }
/// <inheritdoc /> public object?Get(Type type, Type policyInterface) { var hashCode = type?.GetHashCode() ?? 0; // Iterate through containers hierarchy for (UnityContainer?container = Container; null != container; container = container._parent) { // Skip to parent if no registrations if (null == container._registry) { continue; } var registry = container._registry; var targetBucket = (hashCode & HashMask) % registry.Buckets.Length; for (var i = registry.Buckets[targetBucket]; i >= 0; i = registry.Entries[i].Next) { ref var entry = ref registry.Entries[i]; if (entry.HashCode != hashCode || entry.Type != type || entry.Value is ImplicitRegistration) { continue; } return(entry.Value.Get(policyInterface)); } }
/// <summary> /// Create a <see cref="Unity.UnityContainer"/> with the given parent container. /// </summary> /// <param name="parent">The parent <see cref="Unity.UnityContainer"/>. The current object /// will apply its own settings first, and then check the parent for additional ones.</param> private UnityContainer(UnityContainer parent) { // Validate input Debug.Assert(null != parent); Debug.Assert(null != parent._root); // Register with parent _parent = parent; _root = parent._root; _parent.LifetimeContainer.Add(this); // Defaults and policies LifetimeContainer = new LifetimeContainer(this); ExecutionMode = parent._root.ExecutionMode; Defaults = _root.Defaults; Context = new ContainerContext(this); // Dynamic Members Register = InitAndAdd; // Validators ValidateType = _root.ValidateType; ValidateTypes = _root.ValidateTypes; DependencyResolvePipeline = _root.DependencyResolvePipeline; ///////////////////////////////////////////////////////////// // Build Mode var build = _root.ExecutionMode.BuildMode(); PipelineFromRegistration = build switch { ModeFlags.Activated => PipelineFromRegistrationActivated, ModeFlags.Compiled => PipelineFromRegistrationCompiled, _ => (FromRegistration)PipelineFromRegistrationOptimized }; PipelineFromUnregisteredType = build switch { ModeFlags.Activated => PipelineFromUnregisteredTypeActivated, ModeFlags.Compiled => PipelineFromUnregisteredTypeCompiled, _ => (FromUnregistered)PipelineFromUnregisteredTypeOptimized }; PipelineFromOpenGeneric = build switch { ModeFlags.Activated => PipelineFromOpenGenericActivated, ModeFlags.Compiled => PipelineFromOpenGenericCompiled, _ => (FromOpenGeneric)PipelineFromOpenGenericOptimized }; }
internal IEnumerable <TElement> ComplexArray <TElement>(Func <Type, IRegistration, object?> resolve, Type type) { object?value; var set = new QuickSet(); var key = new HashKey(typeof(TElement)); var typeKey = new HashKey(type); // Iterate over hierarchy for (UnityContainer?container = this; null != container; container = container._parent) { // Skip to parent if no data if (null == container._metadata) { continue; } // Hold on to registries Debug.Assert(null != container._registry); var registry = container._registry; // Get indexes and iterate over them var length = container._metadata.GetMeta(ref typeKey, out int[]? data); if (null != data) { for (var i = 1; i < length; i++) { var index = data[i]; var registration = (ExplicitRegistration)registry.Entries[index].Policies; if (null != registration.Name && set.Add(registration.Name)) { try { var itemKey = new HashKey(typeof(TElement), registration.Name); var item = container.GetOrAdd(ref itemKey, typeof(TElement), registration.Name, registration); value = resolve(typeof(TElement), item); } catch (ArgumentException ex) when(ex.InnerException is TypeLoadException) { continue; } #pragma warning disable CS8601 // Possible null reference assignment. yield return((TElement)value); #pragma warning restore CS8601 // Possible null reference assignment. } } } } }
internal IEnumerable <TElement> ComplexArray <TElement>(Func <Type, ImplicitRegistration, object?> resolve, Type type) { object?value; var set = new HashSet <string?>(); int hashCode = type.GetHashCode(); // Iterate over hierarchy for (UnityContainer?container = this; null != container; container = container._parent) { // Skip to parent if no data if (null == container._metadata || null == container._registry) { continue; } // Hold on to registries var registry = container._registry; // Get indexes and iterate over them var length = container._metadata.GetEntries(hashCode, type, out int[]? data); if (null != data) { for (var i = 1; i < length; i++) { var index = data[i]; var registration = (ExplicitRegistration)registry.Entries[index].Value; if (null != registration.Name && set.Add(registration.Name)) { try { var item = container.GetOrAdd(typeof(TElement), registration.Name, registration); value = resolve(typeof(TElement), item); } catch (ArgumentException ex) when(ex.InnerException is TypeLoadException) { continue; } yield return((TElement)value); } } } } }
internal bool IsRegistered(Type type) { var hashCode = type?.GetHashCode() ?? 0; // Iterate through containers hierarchy for (UnityContainer?container = this; null != container; container = container._parent) { // Skip to parent if no registrations if (null == container._metadata) { continue; } if (container._metadata.Contains(hashCode, type)) { return(true); } } return(false); }
internal IEnumerable <TElement> ResolveEnumerable <TElement>(Func <Type, IRegistration, object?> resolve, Type typeDefinition, string?name) { object?value; var set = new QuickSet(); var key = new HashKey(typeof(TElement)); var keyGeneric = new HashKey(typeDefinition); // Iterate over hierarchy for (UnityContainer?container = this; null != container; container = container._parent) { // Skip to parent if no data if (null == container._metadata) { continue; } // Hold on to registries Debug.Assert(null != container._registry); var registry = container._registry; // Get indexes for bound types and iterate over them var length = container._metadata.GetMeta(ref key, out int[]? data); if (null != data) { for (var i = 1; i < length; i++) { var index = data[i]; var registration = (ExplicitRegistration)registry.Entries[index].Policies; if (!set.Add(registration.Name)) { continue; } try { value = resolve(typeof(TElement), registration); } catch (ArgumentException ex) when(ex.InnerException is TypeLoadException) { continue; } #pragma warning disable CS8601 // Possible null reference assignment. yield return((TElement)value); #pragma warning restore CS8601 // Possible null reference assignment. } } // Get indexes for unbound types and iterate over them length = container._metadata.GetMeta(ref keyGeneric, out data); if (null != data) { for (var i = 1; i < length; i++) { var index = data[i]; var registration = (ExplicitRegistration)registry.Entries[index].Policies; if (set.Add(registration.Name)) { try { var itemKey = new HashKey(typeof(TElement), registration.Name); var item = container.GetOrAdd(ref itemKey, typeof(TElement), registration.Name, registration); value = resolve(typeof(TElement), item); } catch (MakeGenericTypeFailedException) { continue; } catch (InvalidRegistrationException) { continue; } #pragma warning disable CS8601 // Possible null reference assignment. yield return((TElement)value); #pragma warning restore CS8601 // Possible null reference assignment. } } } } // If nothing registered attempt to resolve the type if (0 == set.Count) { try { var registration = GetRegistration(typeof(TElement), name); value = resolve(typeof(TElement), registration); } catch { yield break; } #pragma warning disable CS8601 // Possible null reference assignment. yield return((TElement)value); #pragma warning restore CS8601 // Possible null reference assignment. } }
internal bool IsRegistered(ref BuilderContext context) { Type?generic = null; int targetBucket, hashGeneric = -1; int hashExact = NamedType.GetHashCode(context.Type, context.Name); #if NETSTANDARD1_0 || NETCOREAPP1_0 var info = context.Type.GetTypeInfo(); if (info.IsGenericType) { generic = info.GetGenericTypeDefinition(); hashGeneric = NamedType.GetHashCode(generic, context.Name); } #else if (context.Type.IsGenericType) { generic = context.Type.GetGenericTypeDefinition(); hashGeneric = NamedType.GetHashCode(generic, context.Name); } #endif // Iterate through containers hierarchy for (UnityContainer?container = this; null != container; container = container._parent) { // Skip to parent if no registrations if (null == container._metadata || null == container._registry) { continue; } var registry = container._registry; // Check for exact match targetBucket = (hashExact & HashMask) % registry.Buckets.Length; for (var i = registry.Buckets[targetBucket]; i >= 0; i = registry.Entries[i].Next) { ref var candidate = ref registry.Entries[i]; if (candidate.HashCode != hashExact || candidate.Type != context.Type) { continue; } // Found a registration return(true); } // Skip to parent if not generic if (null == generic) { continue; } // Check for factory with same name targetBucket = (hashGeneric & HashMask) % registry.Buckets.Length; for (var i = registry.Buckets[targetBucket]; i >= 0; i = registry.Entries[i].Next) { ref var candidate = ref registry.Entries[i]; if (candidate.HashCode != hashGeneric || candidate.Type != generic) { continue; } // Found a factory return(true); }