|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Microsoft.PowerShell.EditorServices.Components |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Provides a default implementation for the IComponentRegistry |
| 15 | + /// interface. |
| 16 | + /// </summary> |
| 17 | + public class ComponentRegistry : IComponentRegistry |
| 18 | + { |
| 19 | + private Dictionary<Type, object> componentRegistry = |
| 20 | + new Dictionary<Type, object>(); |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Registers an instance of the specified component type |
| 24 | + /// or throws an ArgumentException if an instance has |
| 25 | + /// already been registered. |
| 26 | + /// </summary> |
| 27 | + /// <param name="componentInstance"> |
| 28 | + /// The instance of the component to be registered. |
| 29 | + /// </param> |
| 30 | + /// <returns> |
| 31 | + /// The provided component instance for convenience in assignment |
| 32 | + /// statements. |
| 33 | + /// </returns> |
| 34 | + public TComponent Register<TComponent>(TComponent componentInstance) |
| 35 | + where TComponent : class |
| 36 | + { |
| 37 | + this.componentRegistry.Add(typeof(TComponent), componentInstance); |
| 38 | + return componentInstance; |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Gets the registered instance of the specified |
| 44 | + /// component type or throws a KeyNotFoundException if |
| 45 | + /// no instance has been registered. |
| 46 | + /// </summary> |
| 47 | + /// <returns>The implementation of the specified type.</returns> |
| 48 | + public TComponent Get<TComponent>() |
| 49 | + where TComponent : class |
| 50 | + { |
| 51 | + return (TComponent)this.componentRegistry[typeof(TComponent)]; |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Attempts to retrieve the instance of the specified |
| 56 | + /// component type and, if found, stores it in the |
| 57 | + /// componentInstance parameter. |
| 58 | + /// </summary> |
| 59 | + /// <param name="componentInstance"> |
| 60 | + /// The out parameter in which the found instance will be stored. |
| 61 | + /// </param> |
| 62 | + /// <returns> |
| 63 | + /// True if a registered instance was found, false otherwise. |
| 64 | + /// </returns> |
| 65 | + public bool TryGet<TComponent>(out TComponent componentInstance) |
| 66 | + where TComponent : class |
| 67 | + { |
| 68 | + object componentObject = null; |
| 69 | + componentInstance = null; |
| 70 | + |
| 71 | + if (this.componentRegistry.TryGetValue(typeof(TComponent), out componentObject)) |
| 72 | + { |
| 73 | + componentInstance = componentObject as TComponent; |
| 74 | + return componentInstance != null; |
| 75 | + } |
| 76 | + |
| 77 | + return false; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments