-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Microsoft dependency injection implementation (#370)
- Loading branch information
1 parent
7bb2f67
commit e48eec3
Showing
10 changed files
with
664 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/Splat.Microsoft.Extensions.DependencyInjection.Tests/ContainerWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Splat.Microsoft.Extensions.DependencyInjection.Tests | ||
{ | ||
internal class ContainerWrapper | ||
{ | ||
private IServiceProvider _serviceProvider; | ||
|
||
public ContainerWrapper() | ||
{ | ||
ServiceCollection.UseMicrosoftDependencyResolver(); | ||
} | ||
|
||
public IServiceCollection ServiceCollection { get; } = new ServiceCollection(); | ||
|
||
public IServiceProvider ServiceProvider | ||
{ | ||
get | ||
{ | ||
if (_serviceProvider == null) | ||
{ | ||
_serviceProvider = ServiceCollection.BuildServiceProvider(); | ||
} | ||
|
||
return _serviceProvider; | ||
} | ||
} | ||
|
||
public void BuildAndUse() => ServiceProvider.UseMicrosoftDependencyResolver(); | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
src/Splat.Microsoft.Extensions.DependencyInjection.Tests/DependencyResolverTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Shouldly; | ||
using Splat.Common.Test; | ||
using Xunit; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Splat.Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Splat.Microsoft.Extensions.DependencyInjection.Tests | ||
{ | ||
/// <summary> | ||
/// Tests to show the <see cref="MicrosoftDependencyResolver"/> works correctly. | ||
/// </summary> | ||
public class DependencyResolverTests | ||
{ | ||
/// <summary> | ||
/// Should resolve views. | ||
/// </summary> | ||
[Fact] | ||
public void MicrosoftDependencyResolver_Should_Resolve_Views() | ||
{ | ||
var wrapper = new ContainerWrapper(); | ||
var services = wrapper.ServiceCollection; | ||
services.AddTransient<IViewFor<ViewModelOne>, ViewOne>(); | ||
services.AddTransient<IViewFor<ViewModelTwo>, ViewTwo>(); | ||
|
||
wrapper.BuildAndUse(); | ||
|
||
var viewOne = Locator.Current.GetService(typeof(IViewFor<ViewModelOne>)); | ||
var viewTwo = Locator.Current.GetService(typeof(IViewFor<ViewModelTwo>)); | ||
|
||
viewOne.ShouldNotBeNull(); | ||
viewOne.ShouldBeOfType<ViewOne>(); | ||
viewTwo.ShouldNotBeNull(); | ||
viewTwo.ShouldBeOfType<ViewTwo>(); | ||
} | ||
|
||
/// <summary> | ||
/// Should resolve views. | ||
/// </summary> | ||
[Fact] | ||
public void MicrosoftDependencyResolver_Should_Resolve_Named_View() | ||
{ | ||
var wrapper = new ContainerWrapper(); | ||
var services = wrapper.ServiceCollection; | ||
services.AddTransient<IViewFor<ViewModelTwo>, ViewTwo>(); | ||
|
||
wrapper.BuildAndUse(); | ||
|
||
var viewTwo = Locator.Current.GetService(typeof(IViewFor<ViewModelTwo>)); | ||
|
||
viewTwo.ShouldNotBeNull(); | ||
viewTwo.ShouldBeOfType<ViewTwo>(); | ||
} | ||
|
||
/// <summary> | ||
/// Should resolve view models. | ||
/// </summary> | ||
[Fact] | ||
public void MicrosoftDependencyResolver_Should_Resolve_View_Models() | ||
{ | ||
var wrapper = new ContainerWrapper(); | ||
var services = wrapper.ServiceCollection; | ||
services.AddTransient<ViewModelOne>(); | ||
services.AddTransient<ViewModelTwo>(); | ||
|
||
wrapper.BuildAndUse(); | ||
|
||
var vmOne = Locator.Current.GetService<ViewModelOne>(); | ||
var vmTwo = Locator.Current.GetService<ViewModelTwo>(); | ||
|
||
vmOne.ShouldNotBeNull(); | ||
vmTwo.ShouldNotBeNull(); | ||
} | ||
|
||
/// <summary> | ||
/// Should resolve screen. | ||
/// </summary> | ||
[Fact] | ||
public void MicrosoftDependencyResolver_Should_Resolve_Screen() | ||
{ | ||
var wrapper = new ContainerWrapper(); | ||
var services = wrapper.ServiceCollection; | ||
services.AddSingleton<IScreen>(new MockScreen()); | ||
|
||
wrapper.BuildAndUse(); | ||
|
||
var screen = Locator.Current.GetService<IScreen>(); | ||
|
||
screen.ShouldNotBeNull(); | ||
screen.ShouldBeOfType<MockScreen>(); | ||
} | ||
|
||
/// <summary> | ||
/// Should unregister all. | ||
/// </summary> | ||
[Fact] | ||
public void MicrosoftDependencyResolver_Should_UnregisterAll() | ||
{ | ||
var wrapper = new ContainerWrapper(); | ||
var services = wrapper.ServiceCollection; | ||
|
||
services.AddSingleton<IScreen>(new MockScreen()); | ||
|
||
Locator.CurrentMutable.HasRegistration(typeof(IScreen)) | ||
.ShouldBeTrue(); | ||
|
||
Locator.CurrentMutable.UnregisterAll(typeof(IScreen)); | ||
|
||
var result = Locator.Current.GetService<IScreen>(); | ||
result.ShouldBeNull(); | ||
} | ||
|
||
/// <summary> | ||
/// Should throw an exception if service registration call back called. | ||
/// </summary> | ||
[Fact] | ||
public void MicrosoftDependencyResolver_Should_Throw_If_ServiceRegistionCallback_Called() | ||
{ | ||
var wrapper = new ContainerWrapper(); | ||
wrapper.BuildAndUse(); | ||
|
||
var result = Record.Exception(() => | ||
Locator.CurrentMutable.ServiceRegistrationCallback(typeof(IScreen), disposable => { })); | ||
|
||
result.ShouldBeOfType<NotImplementedException>(); | ||
} | ||
|
||
/// <summary> | ||
/// Should throw an exception if trying to register services when the container is registered as immutable. | ||
/// </summary> | ||
public void MicrosoftDependencyResolver_Should_Throw_If_Attempt_Registration_After_Build() | ||
{ | ||
var wrapper = new ContainerWrapper(); | ||
|
||
wrapper.BuildAndUse(); | ||
|
||
var result = Record.Exception(() => Locator.CurrentMutable.Register(() => new ViewOne())); | ||
|
||
result.ShouldBeOfType<InvalidOperationException>(); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Splat.Microsoft.Extensions.DependencyInjection.Tests/MicrosoftDependencyResolverTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Splat.Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace Splat.Tests.ServiceLocation | ||
{ | ||
/// <summary> | ||
/// Unit Tests for the Modern Dependency Resolver. | ||
/// </summary> | ||
public sealed class MicrosoftDependencyResolverTests : BaseDependencyResolverTests<MicrosoftDependencyResolver> | ||
{ | ||
/// <summary> | ||
/// Test to ensure container allows registration with null service type. | ||
/// </summary> | ||
[Fact] | ||
public void Can_Register_And_Resolve_Null_Types() | ||
{ | ||
var resolver = GetDependencyResolver(); | ||
var foo = 5; | ||
resolver.Register(() => foo, null); | ||
|
||
var value = resolver.GetService(null); | ||
Assert.Equal(foo, value); | ||
|
||
var bar = 4; | ||
var contract = "foo"; | ||
resolver.Register(() => bar, null, contract); | ||
|
||
value = resolver.GetService(null, contract); | ||
Assert.Equal(bar, value); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override MicrosoftDependencyResolver GetDependencyResolver() => new MicrosoftDependencyResolver(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ons.DependencyInjection.Tests/Splat.Microsoft.Extensions.DependencyInjection.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.2</TargetFramework> | ||
<NoWarn>$(NoWarn);1591;CA1707;SA1633</NoWarn> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\Splat.Tests\ServiceLocation\BaseDependencyResolverTests.cs" Link="BaseDependencyResolverTests.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Splat.Common.Test\Splat.Common.Test.csproj" /> | ||
<ProjectReference Include="..\Splat.Microsoft.Extensions.DependencyInjection\Splat.Microsoft.Extensions.DependencyInjection.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
|
||
</Project> |
Oops, something went wrong.