Skip to content

Commit 6681330

Browse files
committed
Change IComponentRegistry interface to use Type parameters
This change modifies the IComponentRegistry method signatures to take a Type parameter directly rather than using a generic parameter. This allows callers in PowerShell to request component implementations without resorting to generic method invocation. An IComponentRegistryExtensions class is also added to provide extension methods which expose the original generic method signatures for convenience in C# code.
1 parent 6aef7a2 commit 6681330

File tree

4 files changed

+118
-20
lines changed

4 files changed

+118
-20
lines changed

src/PowerShellEditorServices.Host/EditorServicesHost.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using Microsoft.PowerShell.EditorServices.Components;
67
using Microsoft.PowerShell.EditorServices.CodeLenses;
78
using Microsoft.PowerShell.EditorServices.Extensions;
89
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;

src/PowerShellEditorServices/Components/ComponentRegistry.cs

+15-11
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ public class ComponentRegistry : IComponentRegistry
2424
/// or throws an ArgumentException if an instance has
2525
/// already been registered.
2626
/// </summary>
27+
/// <param name="componentType">
28+
/// The component type that the instance represents.
29+
/// </param>
2730
/// <param name="componentInstance">
2831
/// The instance of the component to be registered.
2932
/// </param>
3033
/// <returns>
3134
/// The provided component instance for convenience in assignment
3235
/// statements.
3336
/// </returns>
34-
public TComponent Register<TComponent>(TComponent componentInstance)
35-
where TComponent : class
37+
public object Register(Type componentType, object componentInstance)
3638
{
37-
this.componentRegistry.Add(typeof(TComponent), componentInstance);
39+
this.componentRegistry.Add(componentType, componentInstance);
3840
return componentInstance;
3941
}
4042

@@ -44,11 +46,13 @@ public TComponent Register<TComponent>(TComponent componentInstance)
4446
/// component type or throws a KeyNotFoundException if
4547
/// no instance has been registered.
4648
/// </summary>
49+
/// <param name="componentType">
50+
/// The component type for which an instance will be retrieved.
51+
/// </param>
4752
/// <returns>The implementation of the specified type.</returns>
48-
public TComponent Get<TComponent>()
49-
where TComponent : class
53+
public object Get(Type componentType)
5054
{
51-
return (TComponent)this.componentRegistry[typeof(TComponent)];
55+
return this.componentRegistry[componentType];
5256
}
5357

5458
/// <summary>
@@ -59,18 +63,18 @@ public TComponent Get<TComponent>()
5963
/// <param name="componentInstance">
6064
/// The out parameter in which the found instance will be stored.
6165
/// </param>
66+
/// <param name="componentType">
67+
/// The component type for which an instance will be retrieved.
68+
/// </param>
6269
/// <returns>
6370
/// True if a registered instance was found, false otherwise.
6471
/// </returns>
65-
public bool TryGet<TComponent>(out TComponent componentInstance)
66-
where TComponent : class
72+
public bool TryGet(Type componentType, out object componentInstance)
6773
{
68-
object componentObject = null;
6974
componentInstance = null;
7075

71-
if (this.componentRegistry.TryGetValue(typeof(TComponent), out componentObject))
76+
if (this.componentRegistry.TryGetValue(componentType, out componentInstance))
7277
{
73-
componentInstance = componentObject as TComponent;
7478
return componentInstance != null;
7579
}
7680

src/PowerShellEditorServices/Components/IComponentRegistry.cs

+15-9
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6-
using System.Collections.Generic;
7-
using System.Threading;
8-
using System.Threading.Tasks;
6+
using System;
97

108
namespace Microsoft.PowerShell.EditorServices.Components
119
{
@@ -19,37 +17,45 @@ public interface IComponentRegistry
1917
/// or throws an ArgumentException if an instance has
2018
/// already been registered.
2119
/// </summary>
20+
/// <param name="componentType">
21+
/// The component type that the instance represents.
22+
/// </param>
2223
/// <param name="componentInstance">
2324
/// The instance of the component to be registered.
2425
/// </param>
2526
/// <returns>
2627
/// The provided component instance for convenience in assignment
2728
/// statements.
2829
/// </returns>
29-
TComponent Register<TComponent>(TComponent componentInstance)
30-
where TComponent : class;
30+
object Register(
31+
Type componentType,
32+
object componentInstance);
3133

3234
/// <summary>
3335
/// Gets the registered instance of the specified
3436
/// component type or throws a KeyNotFoundException if
3537
/// no instance has been registered.
3638
/// </summary>
39+
/// <param name="componentType">
40+
/// The component type for which an instance will be retrieved.
41+
/// </param>
3742
/// <returns>The implementation of the specified type.</returns>
38-
TComponent Get<TComponent>()
39-
where TComponent : class;
43+
object Get(Type componentType);
4044

4145
/// <summary>
4246
/// Attempts to retrieve the instance of the specified
4347
/// component type and, if found, stores it in the
4448
/// componentInstance parameter.
4549
/// </summary>
50+
/// <param name="componentType">
51+
/// The component type for which an instance will be retrieved.
52+
/// </param>
4653
/// <param name="componentInstance">
4754
/// The out parameter in which the found instance will be stored.
4855
/// </param>
4956
/// <returns>
5057
/// True if a registered instance was found, false otherwise.
5158
/// </returns>
52-
bool TryGet<TComponent>(out TComponent componentInstance)
53-
where TComponent : class;
59+
bool TryGet(Type componentType, out object componentInstance);
5460
}
5561
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
namespace Microsoft.PowerShell.EditorServices.Components
7+
{
8+
/// <summary>
9+
/// Provides generic helper methods for working with IComponentRegistry
10+
/// methods.
11+
/// </summary>
12+
public static class IComponentRegistryExtensions
13+
{
14+
/// <summary>
15+
/// Registers an instance of the specified component type
16+
/// or throws an ArgumentException if an instance has
17+
/// already been registered.
18+
/// </summary>
19+
/// <param name="componentRegistry">
20+
/// The IComponentRegistry instance.
21+
/// </param>
22+
/// <param name="componentInstance">
23+
/// The instance of the component to be registered.
24+
/// </param>
25+
/// <returns>
26+
/// The provided component instance for convenience in assignment
27+
/// statements.
28+
/// </returns>
29+
public static TComponent Register<TComponent>(
30+
this IComponentRegistry componentRegistry,
31+
TComponent componentInstance)
32+
where TComponent : class
33+
{
34+
return
35+
(TComponent)componentRegistry.Register(
36+
typeof(TComponent),
37+
componentInstance);
38+
}
39+
40+
/// <summary>
41+
/// Gets the registered instance of the specified
42+
/// component type or throws a KeyNotFoundException if
43+
/// no instance has been registered.
44+
/// </summary>
45+
/// <param name="componentRegistry">
46+
/// The IComponentRegistry instance.
47+
/// </param>
48+
/// <returns>The implementation of the specified type.</returns>
49+
public static TComponent Get<TComponent>(
50+
this IComponentRegistry componentRegistry)
51+
where TComponent : class
52+
{
53+
return (TComponent)componentRegistry.Get(typeof(TComponent));
54+
}
55+
56+
/// <summary>
57+
/// Attempts to retrieve the instance of the specified
58+
/// component type and, if found, stores it in the
59+
/// componentInstance parameter.
60+
/// </summary>
61+
/// <param name="componentRegistry">
62+
/// The IComponentRegistry instance.
63+
/// </param>
64+
/// <param name="componentInstance">
65+
/// The out parameter in which the found instance will be stored.
66+
/// </param>
67+
/// <returns>
68+
/// True if a registered instance was found, false otherwise.
69+
/// </returns>
70+
public static bool TryGet<TComponent>(
71+
this IComponentRegistry componentRegistry,
72+
out TComponent componentInstance)
73+
where TComponent : class
74+
{
75+
object componentObject = null;
76+
componentInstance = null;
77+
78+
if (componentRegistry.TryGet(typeof(TComponent), out componentObject))
79+
{
80+
componentInstance = componentObject as TComponent;
81+
return componentInstance != null;
82+
}
83+
84+
return false;
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)