Skip to content

Commit

Permalink
feature: Microsoft dependency injection implementation (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzhandler authored and glennawatson committed Aug 5, 2019
1 parent 7bb2f67 commit e48eec3
Show file tree
Hide file tree
Showing 10 changed files with 664 additions and 6 deletions.
8 changes: 5 additions & 3 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ var packageWhitelist = new[]
MakeAbsolute(File("./src/Splat/Splat.csproj")),
MakeAbsolute(File("./src/Splat.Autofac/Splat.Autofac.csproj")),
MakeAbsolute(File("./src/Splat.DryIoc/Splat.DryIoc.csproj")),
MakeAbsolute(File("./src/Splat.Ninject/Splat.Ninject.csproj")),
MakeAbsolute(File("./src/Splat.SimpleInjector/Splat.SimpleInjector.csproj")),
MakeAbsolute(File("./src/Splat.Log4Net/Splat.Log4Net.csproj")),
MakeAbsolute(File("./src/Splat.Microsoft.Extensions.DependencyInjection/Splat.Microsoft.Extensions.DependencyInjection.csproj")),
MakeAbsolute(File("./src/Splat.Microsoft.Extensions.Logging/Splat.Microsoft.Extensions.Logging.csproj")),
MakeAbsolute(File("./src/Splat.Ninject/Splat.Ninject.csproj")),
MakeAbsolute(File("./src/Splat.NLog/Splat.NLog.csproj")),
MakeAbsolute(File("./src/Splat.Serilog/Splat.Serilog.csproj")),
MakeAbsolute(File("./src/Splat.Microsoft.Extensions.Logging/Splat.Microsoft.Extensions.Logging.csproj")),
MakeAbsolute(File("./src/Splat.SimpleInjector/Splat.SimpleInjector.csproj")),
};

var packageTestWhitelist = new[]
{
MakeAbsolute(File("./src/Splat.Tests/Splat.Tests.csproj")),
MakeAbsolute(File("./src/Splat.Autofac.Tests/Splat.Autofac.Tests.csproj")),
MakeAbsolute(File("./src/Splat.DryIoc.Tests/Splat.DryIoc.Tests.csproj")),
MakeAbsolute(File("./src/Splat.Microsoft.Extensions.DependencyInjection.Tests/Splat.Microsoft.Extensions.DependencyInjection.Tests.csproj")),
MakeAbsolute(File("./src/Splat.Ninject.Tests/Splat.Ninject.Tests.csproj")),
MakeAbsolute(File("./src/Splat.SimpleInjector.Tests/Splat.SimpleInjector.Tests.csproj")),
};
Expand Down
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();
}
}
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>();
}
}
}
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();
}
}
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>
Loading

0 comments on commit e48eec3

Please sign in to comment.