-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Added Splat.SimpleInjector (#235)
- Added a SimpleInjectorDependencyResolver adapter - Added unit tests - Added Splat.SimpleInjector to build.cake
- Loading branch information
1 parent
032c736
commit b97eb95
Showing
12 changed files
with
282 additions
and
4 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
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
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,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>(); | ||
} | ||
} | ||
} |
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,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
25
src/Splat.SimpleInjector.Tests/Splat.SimpleInjector.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,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> |
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,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; } | ||
} | ||
} |
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,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 | ||
{ | ||
} | ||
} |
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,3 @@ | ||
{ | ||
"shadowCopy": false | ||
} |
78 changes: 78 additions & 0 deletions
78
src/Splat.SimpleInjector/SimpleInjectorDependencyResolver.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,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; | ||
} | ||
} | ||
} | ||
} |
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,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> |
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,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); | ||
} | ||
} |
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