Skip to content

Commit

Permalink
feature: Added Splat.SimpleInjector (#235)
Browse files Browse the repository at this point in the history
- Added a SimpleInjectorDependencyResolver adapter
- Added unit tests
- Added Splat.SimpleInjector to build.cake
  • Loading branch information
RLittlesII authored Jan 20, 2019
1 parent 032c736 commit b97eb95
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 4 deletions.
4 changes: 3 additions & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ var packageWhitelist = new[]
{
"Splat",
"Splat.Autofac",
"Splat.SimpleInjector",
};

var packageTestWhitelist = new[]
{
"Splat.Tests",
"Splat.Tests",
"Splat.Autofac.Tests",
"Splat.SimpleInjector.Tests",
};

var testFrameworks = new[] { "netcoreapp2.1", "net472" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Splat.Autofac
/// <summary>
/// Extension methods for the Autofac adapter.
/// </summary>
public static class SplatAutofacExtension
public static class SplatAutofacExtensions
{
/// <summary>
/// Initializes an instance of <see cref="AutofacDependencyResolver"/> that overrides the default <see cref="Locator"/>.
Expand Down
67 changes: 67 additions & 0 deletions src/Splat.SimpleInjector.Tests/DependencyResolverTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 ReactiveUI;
using Shouldly;
using SimpleInjector;
using Splat.SimpleInjector;
using Xunit;

namespace Splat.Simplnjector
{
/// <summary>
/// Tests to show the <see cref="SimpleInjectorDependencyResolver"/> works correctly.
/// </summary>
public class DependencyResolverTests
{
/// <summary>
/// Simples the injector dependency resolver should resolve a view model.
/// </summary>
[Fact]
public void SimpleInjectorDependencyResolver_Should_Resolve_View_Model()
{
var container = new Container();
container.Register<ViewModel>();
container.UseSimpleInjectorDependencyResolver();

var viewModel = Locator.Current.GetService(typeof(ViewModel));

viewModel.ShouldNotBeNull();
viewModel.ShouldBeOfType<ViewModel>();
}

/// <summary>
/// Simples the injector dependency resolver should resolve a view.
/// </summary>
[Fact]
public void SimpleInjectorDependencyResolver_Should_Resolve_View()
{
var container = new Container();
container.Register<IViewFor<ViewModel>, View>();
container.UseSimpleInjectorDependencyResolver();

var view = Locator.Current.GetService(typeof(IViewFor<ViewModel>));

view.ShouldNotBeNull();
view.ShouldBeOfType<View>();
}

/// <summary>
/// Simples the injector dependency resolver should resolve the screen.
/// </summary>
[Fact]
public void SimpleInjectorDependencyResolver_Should_Resolve_Screen()
{
var container = new Container();
container.RegisterSingleton<IScreen, MockScreen>();
container.UseSimpleInjectorDependencyResolver();

var screen = Locator.Current.GetService(typeof(IScreen));

screen.ShouldNotBeNull();
screen.ShouldBeOfType<MockScreen>();
}
}
}
14 changes: 14 additions & 0 deletions src/Splat.SimpleInjector.Tests/MockScreen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using ReactiveUI;

namespace Splat.Simplnjector
{
/// <summary>
/// Mock screen.
/// </summary>
/// <seealso cref="ReactiveUI.IScreen" />
public class MockScreen : IScreen
{
/// <inheritdoc />
public RoutingState Router { get; }
}
}
25 changes: 25 additions & 0 deletions src/Splat.SimpleInjector.Tests/Splat.SimpleInjector.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net472</TargetFrameworks>

<IsPackable>false</IsPackable>
<NoWarn>$(NoWarn);1591;CA1707;SA1633</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SimpleInjector" Version="4.4.3" />
<PackageReference Include="ReactiveUI" Version="9.8.15" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Splat.SimpleInjector\Splat.SimpleInjector.csproj" />
<ProjectReference Include="..\Splat\Splat.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
26 changes: 26 additions & 0 deletions src/Splat.SimpleInjector.Tests/View.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 ReactiveUI;

namespace Splat.Simplnjector
{
/// <summary>
/// View.
/// </summary>
/// <seealso cref="ReactiveUI.IViewFor{Splat.Simplnjector.ViewModel}" />
public class View : IViewFor<ViewModel>
{
/// <inheritdoc />
object IViewFor.ViewModel
{
get => ViewModel;
set => ViewModel = (ViewModel)value;
}

/// <inheritdoc />
public ViewModel ViewModel { get; set; }
}
}
14 changes: 14 additions & 0 deletions src/Splat.SimpleInjector.Tests/ViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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.

namespace Splat.Simplnjector
{
/// <summary>
/// View Model.
/// </summary>
public class ViewModel
{
}
}
3 changes: 3 additions & 0 deletions src/Splat.SimpleInjector.Tests/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"shadowCopy": false
}
78 changes: 78 additions & 0 deletions src/Splat.SimpleInjector/SimpleInjectorDependencyResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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 SimpleInjector;

namespace Splat.SimpleInjector
{
/// <summary>
/// Simple Injector implementation for <see cref="IMutableDependencyResolver"/>.
/// </summary>
/// <seealso cref="Splat.IMutableDependencyResolver" />
public class SimpleInjectorDependencyResolver : IMutableDependencyResolver
{
private Container _container;

/// <summary>
/// Initializes a new instance of the <see cref="SimpleInjectorDependencyResolver"/> class.
/// </summary>
/// <param name="container">The container.</param>
public SimpleInjectorDependencyResolver(Container container)
{
_container = container;
}

/// <inheritdoc />
public object GetService(Type serviceType, string contract = null) => _container.GetInstance(serviceType);

/// <inheritdoc />
public IEnumerable<object> GetServices(Type serviceType, string contract = null) =>
_container.GetAllInstances(serviceType);

/// <inheritdoc />
public void Register(Func<object> factory, Type serviceType, string contract = null) =>
_container.Register(serviceType, factory);

/// <inheritdoc />
public void UnregisterCurrent(Type serviceType, string contract = null)
{
throw new NotImplementedException();
}

/// <inheritdoc />
public void UnregisterAll(Type serviceType, string contract = null)
{
throw new NotImplementedException();
}

/// <inheritdoc />
public IDisposable ServiceRegistrationCallback(Type serviceType, string contract, Action<IDisposable> callback)
{
throw new NotImplementedException();
}

/// <inheritdoc />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Disposes of the instance.
/// </summary>
/// <param name="disposing">Whether or not the instance is disposing.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_container?.Dispose();
_container = null;
}
}
}
}
15 changes: 15 additions & 0 deletions src/Splat.SimpleInjector/Splat.SimpleInjector.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SimpleInjector" Version="4.4.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Splat\Splat.csproj" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions src/Splat.SimpleInjector/SplatSimpleInjectorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 SimpleInjector;

namespace Splat.SimpleInjector
{
/// <summary>
/// Extension methods for the Autofac adapter.
/// </summary>
public static class SplatSimpleInjectorExtensions
{
/// <summary>
/// Initializes an instance of <see cref="SimpleInjectorDependencyResolver"/> that overrides the default <see cref="Locator"/>.
/// </summary>
/// <param name="container">Simple Injector container.</param>
public static void UseSimpleInjectorDependencyResolver(this Container container) =>
Locator.Current = new SimpleInjectorDependencyResolver(container);
}
}
16 changes: 14 additions & 2 deletions src/Splat.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Splat.Tests", "Splat.Tests\Splat.Tests.csproj", "{6CAD2584-AA69-4A36-8AD4-A90D040003CA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Splat.Autofac", "Splat.Autofac\Splat.Autofac.csproj", "{8447DF8A-882C-4CA8-A7FB-85D66F12D378}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Splat.Autofac", "Splat.Autofac\Splat.Autofac.csproj", "{8447DF8A-882C-4CA8-A7FB-85D66F12D378}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Splat.Autofac.Tests", "Splat.Autofac.Tests\Splat.Autofac.Tests.csproj", "{1D8068E4-7F85-4322-BC06-3D901F392CF1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Splat.Autofac.Tests", "Splat.Autofac.Tests\Splat.Autofac.Tests.csproj", "{1D8068E4-7F85-4322-BC06-3D901F392CF1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Splat.SimpleInjector", "Splat.SimpleInjector\Splat.SimpleInjector.csproj", "{5A21B576-374D-439E-9303-02A5B0256B26}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Splat.SimpleInjector.Tests", "Splat.SimpleInjector.Tests\Splat.SimpleInjector.Tests.csproj", "{E85B3A4E-6ECA-4171-8605-55792187FAB4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -39,6 +43,14 @@ Global
{1D8068E4-7F85-4322-BC06-3D901F392CF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D8068E4-7F85-4322-BC06-3D901F392CF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D8068E4-7F85-4322-BC06-3D901F392CF1}.Release|Any CPU.Build.0 = Release|Any CPU
{5A21B576-374D-439E-9303-02A5B0256B26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A21B576-374D-439E-9303-02A5B0256B26}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A21B576-374D-439E-9303-02A5B0256B26}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A21B576-374D-439E-9303-02A5B0256B26}.Release|Any CPU.Build.0 = Release|Any CPU
{E85B3A4E-6ECA-4171-8605-55792187FAB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E85B3A4E-6ECA-4171-8605-55792187FAB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E85B3A4E-6ECA-4171-8605-55792187FAB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E85B3A4E-6ECA-4171-8605-55792187FAB4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit b97eb95

Please sign in to comment.