From b8588934a8518afc1ededdf9b39b6274263acbeb Mon Sep 17 00:00:00 2001 From: RLittlesII Date: Sun, 20 Jan 2019 03:08:19 -0600 Subject: [PATCH] feature: Added Splat.SimpleInjector - Added a SimpleInjectorDependencyResolver adapter - Added unit tests - Added Splat.SimpleInjector to build.cake --- build.cake | 4 +- ...Extension.cs => SplatAutofacExtensions.cs} | 2 +- .../DependencyResolverTests.cs | 67 ++++++++++++++++ src/Splat.SimpleInjector.Tests/MockScreen.cs | 14 ++++ .../Splat.SimpleInjector.Tests.csproj | 25 ++++++ src/Splat.SimpleInjector.Tests/View.cs | 26 +++++++ src/Splat.SimpleInjector.Tests/ViewModel.cs | 14 ++++ .../xunit.runner.json | 3 + .../SimpleInjectorDependencyResolver.cs | 78 +++++++++++++++++++ .../Splat.SimpleInjector.csproj | 15 ++++ .../SplatSimpleInjectorExtensions.cs | 22 ++++++ src/Splat.sln | 16 +++- 12 files changed, 282 insertions(+), 4 deletions(-) rename src/Splat.Autofac/{SplatAutofacExtension.cs => SplatAutofacExtensions.cs} (96%) create mode 100644 src/Splat.SimpleInjector.Tests/DependencyResolverTests.cs create mode 100644 src/Splat.SimpleInjector.Tests/MockScreen.cs create mode 100644 src/Splat.SimpleInjector.Tests/Splat.SimpleInjector.Tests.csproj create mode 100644 src/Splat.SimpleInjector.Tests/View.cs create mode 100644 src/Splat.SimpleInjector.Tests/ViewModel.cs create mode 100644 src/Splat.SimpleInjector.Tests/xunit.runner.json create mode 100644 src/Splat.SimpleInjector/SimpleInjectorDependencyResolver.cs create mode 100644 src/Splat.SimpleInjector/Splat.SimpleInjector.csproj create mode 100644 src/Splat.SimpleInjector/SplatSimpleInjectorExtensions.cs diff --git a/build.cake b/build.cake index a3a839a21..dfcda3596 100644 --- a/build.cake +++ b/build.cake @@ -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" }; diff --git a/src/Splat.Autofac/SplatAutofacExtension.cs b/src/Splat.Autofac/SplatAutofacExtensions.cs similarity index 96% rename from src/Splat.Autofac/SplatAutofacExtension.cs rename to src/Splat.Autofac/SplatAutofacExtensions.cs index bd5a5f1e3..8f3ad78c6 100644 --- a/src/Splat.Autofac/SplatAutofacExtension.cs +++ b/src/Splat.Autofac/SplatAutofacExtensions.cs @@ -10,7 +10,7 @@ namespace Splat.Autofac /// /// Extension methods for the Autofac adapter. /// - public static class SplatAutofacExtension + public static class SplatAutofacExtensions { /// /// Initializes an instance of that overrides the default . diff --git a/src/Splat.SimpleInjector.Tests/DependencyResolverTests.cs b/src/Splat.SimpleInjector.Tests/DependencyResolverTests.cs new file mode 100644 index 000000000..9e88551b0 --- /dev/null +++ b/src/Splat.SimpleInjector.Tests/DependencyResolverTests.cs @@ -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 +{ + /// + /// Tests to show the works correctly. + /// + public class DependencyResolverTests + { + /// + /// Simples the injector dependency resolver should resolve a view model. + /// + [Fact] + public void SimpleInjectorDependencyResolver_Should_Resolve_View_Model() + { + var container = new Container(); + container.Register(); + container.UseSimpleInjectorDependencyResolver(); + + var viewModel = Locator.Current.GetService(typeof(ViewModel)); + + viewModel.ShouldNotBeNull(); + viewModel.ShouldBeOfType(); + } + + /// + /// Simples the injector dependency resolver should resolve a view. + /// + [Fact] + public void SimpleInjectorDependencyResolver_Should_Resolve_View() + { + var container = new Container(); + container.Register, View>(); + container.UseSimpleInjectorDependencyResolver(); + + var view = Locator.Current.GetService(typeof(IViewFor)); + + view.ShouldNotBeNull(); + view.ShouldBeOfType(); + } + + /// + /// Simples the injector dependency resolver should resolve the screen. + /// + [Fact] + public void SimpleInjectorDependencyResolver_Should_Resolve_Screen() + { + var container = new Container(); + container.RegisterSingleton(); + container.UseSimpleInjectorDependencyResolver(); + + var screen = Locator.Current.GetService(typeof(IScreen)); + + screen.ShouldNotBeNull(); + screen.ShouldBeOfType(); + } + } +} diff --git a/src/Splat.SimpleInjector.Tests/MockScreen.cs b/src/Splat.SimpleInjector.Tests/MockScreen.cs new file mode 100644 index 000000000..986b523d1 --- /dev/null +++ b/src/Splat.SimpleInjector.Tests/MockScreen.cs @@ -0,0 +1,14 @@ +using ReactiveUI; + +namespace Splat.Simplnjector +{ + /// + /// Mock screen. + /// + /// + public class MockScreen : IScreen + { + /// + public RoutingState Router { get; } + } +} \ No newline at end of file diff --git a/src/Splat.SimpleInjector.Tests/Splat.SimpleInjector.Tests.csproj b/src/Splat.SimpleInjector.Tests/Splat.SimpleInjector.Tests.csproj new file mode 100644 index 000000000..6ce72c074 --- /dev/null +++ b/src/Splat.SimpleInjector.Tests/Splat.SimpleInjector.Tests.csproj @@ -0,0 +1,25 @@ + + + + netcoreapp2.1;net472 + + false + $(NoWarn);1591;CA1707;SA1633 + + + + + + + + + + + + + + + Always + + + diff --git a/src/Splat.SimpleInjector.Tests/View.cs b/src/Splat.SimpleInjector.Tests/View.cs new file mode 100644 index 000000000..f459dab39 --- /dev/null +++ b/src/Splat.SimpleInjector.Tests/View.cs @@ -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 +{ + /// + /// View. + /// + /// + public class View : IViewFor + { + /// + object IViewFor.ViewModel + { + get => ViewModel; + set => ViewModel = (ViewModel)value; + } + + /// + public ViewModel ViewModel { get; set; } + } +} diff --git a/src/Splat.SimpleInjector.Tests/ViewModel.cs b/src/Splat.SimpleInjector.Tests/ViewModel.cs new file mode 100644 index 000000000..faf70cad5 --- /dev/null +++ b/src/Splat.SimpleInjector.Tests/ViewModel.cs @@ -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 +{ + /// + /// View Model. + /// + public class ViewModel + { + } +} diff --git a/src/Splat.SimpleInjector.Tests/xunit.runner.json b/src/Splat.SimpleInjector.Tests/xunit.runner.json new file mode 100644 index 000000000..42db7ef95 --- /dev/null +++ b/src/Splat.SimpleInjector.Tests/xunit.runner.json @@ -0,0 +1,3 @@ +{ + "shadowCopy": false +} diff --git a/src/Splat.SimpleInjector/SimpleInjectorDependencyResolver.cs b/src/Splat.SimpleInjector/SimpleInjectorDependencyResolver.cs new file mode 100644 index 000000000..a7a7755df --- /dev/null +++ b/src/Splat.SimpleInjector/SimpleInjectorDependencyResolver.cs @@ -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 +{ + /// + /// Simple Injector implementation for . + /// + /// + public class SimpleInjectorDependencyResolver : IMutableDependencyResolver + { + private Container _container; + + /// + /// Initializes a new instance of the class. + /// + /// The container. + public SimpleInjectorDependencyResolver(Container container) + { + _container = container; + } + + /// + public object GetService(Type serviceType, string contract = null) => _container.GetInstance(serviceType); + + /// + public IEnumerable GetServices(Type serviceType, string contract = null) => + _container.GetAllInstances(serviceType); + + /// + public void Register(Func factory, Type serviceType, string contract = null) => + _container.Register(serviceType, factory); + + /// + public void UnregisterCurrent(Type serviceType, string contract = null) + { + throw new NotImplementedException(); + } + + /// + public void UnregisterAll(Type serviceType, string contract = null) + { + throw new NotImplementedException(); + } + + /// + public IDisposable ServiceRegistrationCallback(Type serviceType, string contract, Action callback) + { + throw new NotImplementedException(); + } + + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Disposes of the instance. + /// + /// Whether or not the instance is disposing. + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + _container?.Dispose(); + _container = null; + } + } + } +} diff --git a/src/Splat.SimpleInjector/Splat.SimpleInjector.csproj b/src/Splat.SimpleInjector/Splat.SimpleInjector.csproj new file mode 100644 index 000000000..3148eb27c --- /dev/null +++ b/src/Splat.SimpleInjector/Splat.SimpleInjector.csproj @@ -0,0 +1,15 @@ + + + + netstandard2.0 + + + + + + + + + + + diff --git a/src/Splat.SimpleInjector/SplatSimpleInjectorExtensions.cs b/src/Splat.SimpleInjector/SplatSimpleInjectorExtensions.cs new file mode 100644 index 000000000..d34695f9f --- /dev/null +++ b/src/Splat.SimpleInjector/SplatSimpleInjectorExtensions.cs @@ -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 +{ + /// + /// Extension methods for the Autofac adapter. + /// + public static class SplatSimpleInjectorExtensions + { + /// + /// Initializes an instance of that overrides the default . + /// + /// Simple Injector container. + public static void UseSimpleInjectorDependencyResolver(this Container container) => + Locator.Current = new SimpleInjectorDependencyResolver(container); + } +} diff --git a/src/Splat.sln b/src/Splat.sln index c225ac681..676316ab9 100644 --- a/src/Splat.sln +++ b/src/Splat.sln @@ -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 @@ -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