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