Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit af9f364

Browse files
committed
Add comments to factory method
Cache the call to GetService<IVSGitExt>(). Use Func<IVSGitExt> rather than Lazy<IVSGitExt> (which was a hack).
1 parent 719e624 commit af9f364

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/GitHub.VisualStudio/Services/VSGitExtFactory.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,12 @@ public class VSGitExtFactory
1818
{
1919
static readonly ILogger log = LogManager.ForContext<VSGitExtFactory>();
2020

21-
readonly IGitHubServiceProvider serviceProvider;
22-
2321
[ImportingConstructor]
2422
public VSGitExtFactory(IGitHubServiceProvider serviceProvider)
2523
{
26-
this.serviceProvider = serviceProvider;
24+
VSGitExt = serviceProvider.GetService<IVSGitExt>();
2725
}
2826

29-
[Export(typeof(IVSGitExt))]
30-
public IVSGitExt VSGitExt => serviceProvider.GetService<IVSGitExt>();
31-
3227
public async static Task<IVSGitExt> Create(IAsyncServiceProvider sp)
3328
{
3429
var dte = await sp.GetServiceAsync(typeof(DTE)) as DTE;
@@ -37,13 +32,20 @@ public async static Task<IVSGitExt> Create(IAsyncServiceProvider sp)
3732
switch (dte.Version)
3833
{
3934
case "14.0":
40-
return new Lazy<IVSGitExt>(() => new VSGitExt14(sp.GetServiceAsync)).Value;
35+
return Create(() => new VSGitExt14(sp.GetServiceAsync));
4136
case "15.0":
42-
return new Lazy<IVSGitExt>(() => new VSGitExt15(sp.GetServiceAsync)).Value;
37+
return Create(() => new VSGitExt15(sp.GetServiceAsync));
4338
default:
4439
log.Error("There is no IVSGitExt implementation for DTE version {Version}", dte.Version);
4540
return null;
4641
}
4742
}
43+
44+
// NOTE: We're being careful to only reference VSGitExt14 and VSGitExt15 from inside a lambda expression.
45+
// This ensures that only the type that's compatible with the running DTE version is loaded.
46+
static IVSGitExt Create(Func<IVSGitExt> factory) => factory.Invoke();
47+
48+
[Export]
49+
public IVSGitExt VSGitExt { get; }
4850
}
4951
}

0 commit comments

Comments
 (0)