Skip to content

Commit 280b1b1

Browse files
committed
Provide a means of locating the VBE for use along COM classes which are not managed by a IoC container.
1 parent 0aaf7c1 commit 280b1b1

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Rubberduck.Main/Extension.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void OnConnection(object Application, ext_ConnectMode ConnectMode, object
5959
_addin = RootComWrapperFactory.GetAddInWrapper(AddInInst);
6060
_addin.Object = this;
6161

62+
VbeProvider.Initialize(_vbe);
6263
VBENativeServices.HookEvents(_vbe);
6364

6465
#if DEBUG
@@ -242,6 +243,7 @@ private void ShutdownAddIn()
242243
_logger.Log(LogLevel.Info, "Rubberduck is shutting down.");
243244
_logger.Log(LogLevel.Trace, "Unhooking VBENativeServices events...");
244245
VBENativeServices.UnhookEvents();
246+
VbeProvider.Terminate();
245247

246248
_logger.Log(LogLevel.Trace, "Releasing dockable hosts...");
247249

Rubberduck.Main/VbeProvider.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
2+
using Rubberduck.VBEditor.VBERuntime;
3+
4+
namespace Rubberduck
5+
{
6+
/// <summary>
7+
/// ANTI-PATTERN: Service locator for COM class. Think carefully before using it, and regret it.
8+
/// </summary>
9+
/// <remarks>
10+
/// This is a hacky workaround to provide support to COM-visible classes without breaking the
11+
/// interface or violating the security settings of the Office host. Because a COM class must
12+
/// have a parameterless constructor if it is to be newed up and because COM class cannot come
13+
/// from the IoC container nor have any dependencies coming out of it, we use the service
14+
/// locator anti-pattern here to provide the necessary functionality for the COM classes'
15+
/// internal implementations. The use should never expand beyond that limited scope.
16+
/// </remarks>
17+
internal static class VbeProvider
18+
{
19+
internal static void Initialize(IVBE vbe)
20+
{
21+
Vbe = vbe;
22+
VbeRuntime = new VBERuntimeAccessor(vbe);
23+
}
24+
25+
internal static void Terminate()
26+
{
27+
Vbe.Dispose();
28+
Vbe = null;
29+
VbeRuntime = null;
30+
}
31+
32+
internal static IVBE Vbe { get; private set; }
33+
internal static IVBERuntime VbeRuntime { get; private set; }
34+
}
35+
}

0 commit comments

Comments
 (0)