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

Commit f3865b1

Browse files
committed
Use a delegate for GetServiceAsync
Rather than extending IGitHubServiceProvider, pass a delegate for GetServiceAsync.
1 parent 738b046 commit f3865b1

File tree

5 files changed

+23
-44
lines changed

5 files changed

+23
-44
lines changed

src/GitHub.Exports/Services/IGitHubServiceProvider.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Threading.Tasks;
3-
using System.Diagnostics.CodeAnalysis;
42
using System.ComponentModel.Composition.Hosting;
53
using System.Runtime.InteropServices;
64
using GitHub.VisualStudio;
@@ -30,20 +28,5 @@ Ret GetService<T, Ret>() where T : class
3028
/// <param name="owner">The owner, which either has to match what was passed to AddService,
3129
/// or if it's null, the service will be removed without checking for ownership</param>
3230
void RemoveService(Type t, object owner);
33-
34-
/// <summary>
35-
/// Gets a Visual Studio service asynchronously.
36-
/// </summary>
37-
/// <typeparam name="T">The type of the service.</typeparam>
38-
Task<T> GetServiceAsync<T>() where T : class;
39-
40-
/// <summary>
41-
/// Gets a Visual Studio service asynchronously.
42-
/// </summary>
43-
/// <typeparam name="T">The type of the service.</typeparam>
44-
/// <typeparam name="Ret">The type of the service instance to return.</typeparam>
45-
/// <returns></returns>
46-
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
47-
Task<Ret> GetServiceAsync<T, Ret>() where T : class where Ret : class;
4831
}
4932
}

src/GitHub.TeamFoundation.14/Services/VSGitExt.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ public class VSGitExt : IVSGitExt
2323
{
2424
static readonly ILogger log = LogManager.ForContext<VSGitExt>();
2525

26-
readonly IGitHubServiceProvider serviceProvider;
26+
readonly Func<Type, Task<object>> getServiceAsync;
2727
readonly IVSUIContext context;
2828
readonly ILocalRepositoryModelFactory repositoryFactory;
2929

3030
IGitExt gitService;
3131
IReadOnlyList<ILocalRepositoryModel> activeRepositories;
3232

3333
[ImportingConstructor]
34-
public VSGitExt(IGitHubServiceProvider serviceProvider)
35-
: this(serviceProvider, new VSUIContextFactory(), new LocalRepositoryModelFactory())
34+
public VSGitExt(Func<Type, Task<object>> getServiceAsync)
35+
: this(getServiceAsync, new VSUIContextFactory(), new LocalRepositoryModelFactory())
3636
{
3737
}
3838

39-
public VSGitExt(IGitHubServiceProvider serviceProvider, IVSUIContextFactory factory, ILocalRepositoryModelFactory repositoryFactory)
39+
public VSGitExt(Func<Type, Task<object>> getServiceAsync, IVSUIContextFactory factory, ILocalRepositoryModelFactory repositoryFactory)
4040
{
41-
this.serviceProvider = serviceProvider;
41+
this.getServiceAsync = getServiceAsync;
4242
this.repositoryFactory = repositoryFactory;
4343

4444
// The IGitExt service isn't available when a TFS based solution is opened directly.
@@ -96,7 +96,7 @@ async Task ContextChangedAsync()
9696

9797
async Task<bool> TryInitialize()
9898
{
99-
gitService = await serviceProvider.GetServiceAsync<IGitExt>();
99+
gitService = (IGitExt)await getServiceAsync(typeof(IGitExt));
100100
if (gitService != null)
101101
{
102102
gitService.PropertyChanged += (s, e) =>

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,7 @@ async Task<object> CreateService(IAsyncServiceContainer container, CancellationT
283283
else if (serviceType == typeof(IVSGitExt))
284284
{
285285
var dte = await GetServiceAsync(typeof(DTE)) as DTE;
286-
var sp = await GetServiceAsync(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider;
287-
return CreateVSGitExt(dte.Version, sp);
286+
return CreateVSGitExt(dte.Version);
288287
}
289288
else if (serviceType == typeof(IGitHubToolWindowManager))
290289
{
@@ -298,15 +297,15 @@ async Task<object> CreateService(IAsyncServiceContainer container, CancellationT
298297
}
299298
}
300299

301-
IVSGitExt CreateVSGitExt(string dteVersion, IGitHubServiceProvider sp)
300+
IVSGitExt CreateVSGitExt(string dteVersion)
302301
{
303302
// DTE.Version always ends with ".0" even for later minor versions.
304303
switch (dteVersion)
305304
{
306305
case "14.0":
307-
return new Lazy<IVSGitExt>(() => new VSGitExt14(sp)).Value;
306+
return new Lazy<IVSGitExt>(() => new VSGitExt14(GetServiceAsync)).Value;
308307
case "15.0":
309-
return new Lazy<IVSGitExt>(() => new VSGitExt15(sp)).Value;
308+
return new Lazy<IVSGitExt>(() => new VSGitExt15(GetServiceAsync)).Value;
310309
default:
311310
log.Error("There is no IVSGitExt implementation for DTE version {Version}", dteVersion);
312311
return null;

src/GitHub.VisualStudio/Services/GitHubServiceProvider.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ public IServiceProvider GitServiceProvider
7171
public object TryGetService(Type t) => theRealProvider.TryGetService(t);
7272

7373
public T TryGetService<T>() where T : class => theRealProvider.TryGetService<T>();
74-
75-
public Task<T> GetServiceAsync<T>() where T : class => theRealProvider.GetServiceAsync<T>();
76-
77-
public Task<Ret> GetServiceAsync<T, Ret>() where T : class where Ret : class => theRealProvider.GetServiceAsync<T, Ret>();
7874
}
7975

8076
/// <summary>
@@ -313,8 +309,5 @@ public void Dispose()
313309
Dispose(true);
314310
GC.SuppressFinalize(this);
315311
}
316-
317-
public async Task<T> GetServiceAsync<T>() where T : class => (T)await asyncServiceProvider.GetServiceAsync(typeof(T));
318-
public async Task<Ret> GetServiceAsync<T, Ret>() where T : class where Ret : class => (Ret)await asyncServiceProvider.GetServiceAsync(typeof(T));
319312
}
320313
}

test/UnitTests/GitHub.TeamFoundation/VSGitExtTests.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@ public class TheConstructor : TestBaseClass
2121
public void GetServiceIGitExt_WhenSccProviderContextIsActive(bool isActive, int expectCalls)
2222
{
2323
var context = CreateVSUIContext(isActive);
24-
var sp = Substitute.For<IGitHubServiceProvider>();
24+
var sp = Substitute.For<IAsyncServiceProvider>();
2525

2626
var target = CreateVSGitExt(context, sp: sp);
2727

28-
sp.Received(expectCalls).GetServiceAsync<IGitExt>();
28+
sp.Received(expectCalls).GetServiceAsync(typeof(IGitExt));
2929
}
3030

3131
[TestCase(true, 1)]
3232
[TestCase(false, 0)]
3333
public void GetServiceIGitExt_WhenUIContextChanged(bool activated, int expectCalls)
3434
{
3535
var context = CreateVSUIContext(false);
36-
var sp = Substitute.For<IGitHubServiceProvider>();
36+
var sp = Substitute.For<IAsyncServiceProvider>();
3737
var target = CreateVSGitExt(context, sp: sp);
3838

3939
var eventArgs = new VSUIContextChangedEventArgs(activated);
4040
context.UIContextChanged += Raise.Event<EventHandler<VSUIContextChangedEventArgs>>(context, eventArgs);
4141

42-
sp.Received(expectCalls).GetServiceAsync<IGitExt>();
42+
sp.Received(expectCalls).GetServiceAsync(typeof(IGitExt));
4343
}
4444

4545
[Test]
@@ -207,23 +207,27 @@ static IReadOnlyList<IGitRepositoryInfo> CreateActiveRepositories(params string[
207207
return repositories.AsReadOnly();
208208
}
209209

210-
static VSGitExt CreateVSGitExt(IVSUIContext context = null, IGitExt gitExt = null, IGitHubServiceProvider sp = null,
210+
static VSGitExt CreateVSGitExt(IVSUIContext context = null, IGitExt gitExt = null, IAsyncServiceProvider sp = null,
211211
ILocalRepositoryModelFactory repoFactory = null)
212212
{
213213
context = context ?? CreateVSUIContext(true);
214214
gitExt = gitExt ?? CreateGitExt();
215-
sp = sp ?? Substitute.For<IGitHubServiceProvider>();
215+
sp = sp ?? Substitute.For<IAsyncServiceProvider>();
216216
repoFactory = repoFactory ?? Substitute.For<ILocalRepositoryModelFactory>();
217217
var factory = Substitute.For<IVSUIContextFactory>();
218218
var contextGuid = new Guid(Guids.GitSccProviderId);
219219
factory.GetUIContext(contextGuid).Returns(context);
220-
sp.GetService<IVSUIContextFactory>().Returns(factory);
221-
sp.GetServiceAsync<IGitExt>().Returns(gitExt);
222-
var vsGitExt = new VSGitExt(sp, factory, repoFactory);
220+
sp.GetServiceAsync(typeof(IGitExt)).Returns(gitExt);
221+
var vsGitExt = new VSGitExt(sp.GetServiceAsync, factory, repoFactory);
223222
vsGitExt.PendingTasks.Wait();
224223
return vsGitExt;
225224
}
226225

226+
public interface IAsyncServiceProvider
227+
{
228+
Task<object> GetServiceAsync(Type serviceType);
229+
}
230+
227231
static IGitExt CreateGitExt(params string[] repositoryPaths)
228232
{
229233
var gitExt = Substitute.For<IGitExt>();

0 commit comments

Comments
 (0)