Skip to content

Commit 8f5d11a

Browse files
committed
Add IComponentRegistry dependency container interface
This change adds a new interface called IComponentRegistry which acts as a dependency container for components in PowerShell Editor Services. It also adds a default ComponentRegistry class which implements this interface so that it can be used in the server.
1 parent 2798d02 commit 8f5d11a

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.Collections.Generic;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
10+
namespace Microsoft.PowerShell.EditorServices.Components
11+
{
12+
/// <summary>
13+
/// Specifies the contract for a registry of component interfaces.
14+
/// </summary>
15+
public interface IComponentRegistry
16+
{
17+
/// <summary>
18+
/// Registers an instance of the specified component type
19+
/// or throws an ArgumentException if an instance has
20+
/// already been registered.
21+
/// </summary>
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+
TComponent Register<TComponent>(TComponent componentInstance)
30+
where TComponent : class;
31+
32+
/// <summary>
33+
/// Gets the registered instance of the specified
34+
/// component type or throws a KeyNotFoundException if
35+
/// no instance has been registered.
36+
/// </summary>
37+
/// <returns>The implementation of the specified type.</returns>
38+
TComponent Get<TComponent>()
39+
where TComponent : class;
40+
41+
/// <summary>
42+
/// Attempts to retrieve the instance of the specified
43+
/// component type and, if found, stores it in the
44+
/// componentInstance parameter.
45+
/// </summary>
46+
/// <param name="componentInstance">
47+
/// The out parameter in which the found instance will be stored.
48+
/// </param>
49+
/// <returns>
50+
/// True if a registered instance was found, false otherwise.
51+
/// </returns>
52+
bool TryGet<TComponent>(out TComponent componentInstance)
53+
where TComponent : class;
54+
}
55+
}

src/PowerShellEditorServices/Session/EditorSession.cs

Lines changed: 6 additions & 0 deletions
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.Console;
78
using Microsoft.PowerShell.EditorServices.Extensions;
89
using Microsoft.PowerShell.EditorServices.Session;
@@ -71,6 +72,11 @@ public class EditorSession
7172
/// </summary>
7273
public RemoteFileManager RemoteFileManager { get; private set; }
7374

75+
/// <summary>
76+
/// Gets the IComponentCollection instance for this session.
77+
/// </summary>
78+
public IComponentRegistry Components { get; } = new ComponentRegistry();
79+
7480
#endregion
7581

7682
#region Constructors

0 commit comments

Comments
 (0)