Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add Prism DI extensions #431

Merged
merged 2 commits into from Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.cake
Expand Up @@ -16,6 +16,8 @@ var packageWhitelist = new[]
MakeAbsolute(File("./src/Splat.Serilog/Splat.Serilog.csproj")),
MakeAbsolute(File("./src/Splat.SimpleInjector/Splat.SimpleInjector.csproj")),
MakeAbsolute(File("./src/Splat.Drawing/Splat.Drawing.csproj")),
MakeAbsolute(File("./src/Splat.Prism/Splat.Prism.csproj")),
MakeAbsolute(File("./src/Splat.Prism.Forms/Splat.Prism.Forms.csproj")),
};

var packageTestWhitelist = new[]
Expand All @@ -26,6 +28,7 @@ var packageTestWhitelist = new[]
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")),
MakeAbsolute(File("./src/Splat.Prism.Tests/Splat.Prism.Tests.csproj")),
};

BuildParameters.SetParameters(context: Context,
Expand Down
2 changes: 1 addition & 1 deletion build.config
@@ -1,2 +1,2 @@
#!/usr/bin/env bash
CAKE_VERSION=0.34.1
CAKE_VERSION=0.35.0
6 changes: 3 additions & 3 deletions src/Directory.build.props
Expand Up @@ -62,12 +62,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Nerdbank.GitVersioning" Version="3.0.26" PrivateAssets="all" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.0.28" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="stylecop.analyzers" Version="1.1.118" PrivateAssets="all" />
<PackageReference Include="Roslynator.Analyzers" Version="2.1.0" PrivateAssets="All" />
<PackageReference Include="stylecop.analyzers" Version="1.2.0-beta.113" PrivateAssets="all" />
<PackageReference Include="Roslynator.Analyzers" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.7" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
Expand Down
Expand Up @@ -26,7 +26,7 @@ public static async Task<T> RunTaskAsync<T>(this CoreDispatcher dispatcher, Func
taskCompletionSource.SetException(ex);
}
});
return await taskCompletionSource.Task;
return await taskCompletionSource.Task.ConfigureAwait(false);
}

// There is no TaskCompletionSource<void> so we use a bool that we throw away.
Expand Down
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<NoWarn>$(NoWarn);1591;CA1707;SA1633;CA2000</NoWarn>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Expand Up @@ -9,7 +9,7 @@
<PackageId>Splat.Microsoft.Extensions.Logging</PackageId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="System.Collections.Immutable" Version="1.6.0" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Splat.NLog/Splat.NLog.csproj
Expand Up @@ -9,7 +9,7 @@
<PackageId>Splat.NLog</PackageId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="4.6.7" />
<PackageReference Include="NLog" Version="4.6.8" />
<PackageReference Include="System.Collections.Immutable" Version="1.6.0" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Splat.Ninject.Tests/Splat.Ninject.Tests.csproj
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<NoWarn>$(NoWarn);1591;CA1707;SA1633;CA2000</NoWarn>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
34 changes: 34 additions & 0 deletions src/Splat.Prism.Forms/PrismApplication.cs
@@ -0,0 +1,34 @@
// 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.Text;
using Prism;
using Prism.Ioc;

namespace Splat.Prism.Forms
{
/// <summary>
/// A application instance which supports Prism types.
/// </summary>
public abstract class PrismApplication : PrismApplicationBase
{
/// <summary>
/// Initializes a new instance of the <see cref="PrismApplication"/> class.
/// </summary>
/// <param name="initializer">An initializer for initializing the platform.</param>
public PrismApplication(IPlatformInitializer initializer = null)
: base(initializer)
{
}

/// <inheritdoc/>
protected override IContainerExtension CreateContainerExtension()
{
return new SplatContainerExtension();
}
}
}
15 changes: 15 additions & 0 deletions src/Splat.Prism.Forms/Splat.Prism.Forms.csproj
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>Prism adapter for Splat including Xamarin Forms adapters.</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Prism.Forms" Version="7.2.0.1367" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Splat.Prism\Splat.Prism.csproj" />
</ItemGroup>
</Project>
185 changes: 185 additions & 0 deletions src/Splat.Prism.Tests/DependencyResolverTests.cs
@@ -0,0 +1,185 @@
// 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 Shouldly;
using Splat.Common.Test;
using Splat.Prism;
using Xunit;

namespace Splat.Prism.Tests
{
/// <summary>
/// Tests to show the <see cref="PrismDependencyResolver"/> works correctly.
/// </summary>
public class DependencyResolverTests
{
/// <summary>
/// Should resolve the views.
/// </summary>
[Fact]
public void PrismDependencyResolver_Should_Resolve_Views()
{
var container = new SplatContainerExtension();
container.Register(typeof(IViewFor<ViewModelOne>), typeof(ViewOne));
container.Register(typeof(IViewFor<ViewModelTwo>), typeof(ViewTwo));

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 the views.
/// </summary>
[Fact]
public void PrismDependencyResolver_Should_Resolve_Named_View()
{
var container = new SplatContainerExtension();
container.Register(typeof(IViewFor<ViewModelTwo>), typeof(ViewTwo), "Other");

var viewTwo = Locator.Current.GetService(typeof(IViewFor<ViewModelTwo>), "Other");

viewTwo.ShouldNotBeNull();
viewTwo.ShouldBeOfType<ViewTwo>();
}

/// <summary>
/// Should resolve the view models.
/// </summary>
[Fact]
public void PrismDependencyResolver_Should_Resolve_View_Models()
{
var container = new SplatContainerExtension();

container.Register(typeof(ViewModelOne), typeof(ViewModelOne));
container.Register(typeof(ViewModelTwo), typeof(ViewModelTwo));

var vmOne = Locator.Current.GetService<ViewModelOne>();
var vmTwo = Locator.Current.GetService<ViewModelTwo>();

vmOne.ShouldNotBeNull();
vmTwo.ShouldNotBeNull();
}

/// <summary>
/// Should resolve the screen.
/// </summary>
[Fact]
public void PrismDependencyResolver_Should_Resolve_Screen()
{
var builder = new SplatContainerExtension();
builder.RegisterSingleton(typeof(IScreen), typeof(MockScreen));

var screen = Locator.Current.GetService<IScreen>();

screen.ShouldNotBeNull();
screen.ShouldBeOfType<MockScreen>();
}

/// <summary>
/// Should unregister the screen.
/// </summary>
[Fact]
public void PrismDependencyResolver_Should_UnregisterCurrent_Screen()
{
var builder = new SplatContainerExtension();
builder.RegisterSingleton(typeof(IScreen), typeof(MockScreen));

Locator.Current.GetService<IScreen>().ShouldNotBeNull();

Locator.CurrentMutable.UnregisterCurrent(typeof(IScreen));

Locator.Current.GetService<IScreen>().ShouldBeNull();
}

/// <summary>
/// Should unregister the screen.
/// </summary>
[Fact]
public void PrismDependencyResolver_Should_UnregisterCurrent_Screen_With_Contract()
{
var builder = new SplatContainerExtension();
builder.RegisterSingleton(typeof(IScreen), typeof(MockScreen), nameof(MockScreen));

Locator.Current.GetService<IScreen>(nameof(MockScreen)).ShouldNotBeNull();

Locator.CurrentMutable.UnregisterCurrent(typeof(IScreen), nameof(MockScreen));

Locator.Current.GetService<IScreen>(nameof(MockScreen)).ShouldBeNull();
}

/// <summary>
/// Should unregister the screen.
/// </summary>
[Fact]
public void PrismDependencyResolver_Should_UnregisterAll_Screen()
{
var builder = new SplatContainerExtension();
builder.RegisterSingleton(typeof(IScreen), typeof(MockScreen));

Locator.Current.GetService<IScreen>().ShouldNotBeNull();

Locator.CurrentMutable.UnregisterAll(typeof(IScreen));

Locator.Current.GetService<IScreen>().ShouldBeNull();
}

/// <summary>
/// Should unregister the screen.
/// </summary>
[Fact]
public void PrismDependencyResolver_Should_UnregisterAll_Screen_With_Contract()
{
var builder = new SplatContainerExtension();
builder.RegisterSingleton(typeof(IScreen), typeof(MockScreen), nameof(MockScreen));

Locator.Current.GetService<IScreen>(nameof(MockScreen)).ShouldNotBeNull();

Locator.CurrentMutable.UnregisterAll(typeof(IScreen), nameof(MockScreen));

Locator.Current.GetService<IScreen>(nameof(MockScreen)).ShouldBeNull();
}

/// <summary>
/// Check to ensure the correct logger is returned.
/// </summary>
/// <remarks>
/// Introduced for Splat #331.
/// </remarks>
[Fact]
public void PrismDependencyResolver_Should_ReturnRegisteredLogger()
{
var c = new SplatContainerExtension();
c.Register(typeof(ILogger), typeof(ConsoleLogger));
Locator.CurrentMutable.RegisterConstant(
new FuncLogManager(type => new WrappingFullLogger(new ConsoleLogger())),
typeof(ILogManager));

var d = Splat.Locator.Current.GetService<ILogManager>();
Assert.IsType<FuncLogManager>(d);
}

/// <summary>
/// Test that a pre-init logger isn't overriden.
/// </summary>
/// <remarks>
/// Introduced for Splat #331.
/// </remarks>
[Fact]
public void PrismDependencyResolver_PreInit_Should_ReturnRegisteredLogger()
{
var c = new SplatContainerExtension();
c.RegisterInstance(typeof(ILogManager), new FuncLogManager(type => new WrappingFullLogger(new ConsoleLogger())));

var d = Splat.Locator.Current.GetService<ILogManager>();
Assert.IsType<FuncLogManager>(d);
}
}
}
13 changes: 13 additions & 0 deletions src/Splat.Prism.Tests/Splat.Prism.Tests.csproj
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<NoWarn>$(NoWarn);CA1707;CS1574</NoWarn>
</PropertyGroup>

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

</Project>
15 changes: 15 additions & 0 deletions src/Splat.Prism/Splat.Prism.csproj
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>Prism adapter for Splat</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Prism.Core" Version="7.2.0.1367" />
</ItemGroup>

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