Skip to content

Commit

Permalink
Merge pull request PrismLibrary#950 from PrismLibrary/DryIoc-Concrete…
Browse files Browse the repository at this point in the history
…TypesFix

fixed DryIoc auto resolve concrete classes. Fixes PrismLibrary#889
  • Loading branch information
brianlagunas authored Feb 17, 2017
2 parents 470175e + b971cba commit ff7898b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Xunit;
using Autofac;
using Autofac.Core.Registration;
using Prism.DI.Forms.Tests;
#if TEST
using Application = Prism.FormsApplication;
#endif
Expand Down Expand Up @@ -47,6 +48,16 @@ public void ResolveTypeRegisteredWithContainer()
Assert.IsType<ServiceMock>(service);
}

[Fact]
public void ResolveConcreteTypeNotRegisteredWithContainer()
{
var app = new PrismApplicationMock();
Assert.True(app.Initialized);
var concreteType = app.Container.Resolve<ConcreteTypeMock>();
Assert.NotNull(concreteType);
Assert.IsType<ConcreteTypeMock>(concreteType);
}

[Fact]
public void ResolveTypeRegisteredWithDependencyService()
{
Expand Down Expand Up @@ -80,7 +91,7 @@ public void Module_Initialize()
}

[Fact]
public async Task Navigate_UnregisteredView_ThrowInvalidOperationException()
public async Task Navigate_UnregisteredView_ThrowNullReferenceException()
{
var app = new PrismApplicationMock();
var navigationService = ResolveAndSetRootPage(app);
Expand Down
6 changes: 6 additions & 0 deletions Source/Xamarin/Prism.DI.Forms.Tests/ConcreteTypeMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Prism.DI.Forms.Tests
{
public class ConcreteTypeMock
{
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
Expand All @@ -9,6 +9,7 @@
<Import_RootNamespace>Prism.DI.Forms.Tests</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)ConcreteTypeMock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mocks\Modules\ModuleMock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mocks\Services\DependencyServiceMock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mocks\Services\IDependencyServiceMock.cs" />
Expand Down
17 changes: 14 additions & 3 deletions Source/Xamarin/Prism.DryIoc.Forms.Tests/PrismApplicationFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Prism.Navigation;
using Xamarin.Forms;
using Xunit;
using Prism.DI.Forms.Tests;
#if TEST
using Application = Prism.FormsApplication;
#endif
Expand Down Expand Up @@ -46,6 +47,16 @@ public void ResolveTypeRegisteredWithContainer()
Assert.IsType<ServiceMock>(service);
}

[Fact]
public void ResolveConcreteTypeNotRegisteredWithContainer()
{
var app = new PrismApplicationMock();
Assert.True(app.Initialized);
var concreteType = app.Container.Resolve<ConcreteTypeMock>();
Assert.NotNull(concreteType);
Assert.IsType<ConcreteTypeMock>(concreteType);
}

[Fact]
public void ResolveTypeRegisteredWithDependencyService()
{
Expand Down Expand Up @@ -79,12 +90,12 @@ public void Module_Initialize()
}

[Fact]
public async Task Navigate_UnregisteredView_ThrowInvalidOperationException()
public async Task Navigate_UnregisteredView_ThrowArgumentOutOfRangeException()
{
var app = new PrismApplicationMock();
var navigationService = ResolveAndSetRootPage(app);
var exception = await Assert.ThrowsAsync<NullReferenceException>(async () => await navigationService.NavigateAsync("missing"));
Assert.Contains("missing", exception.Message, StringComparison.OrdinalIgnoreCase);
var exception = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await navigationService.NavigateAsync("missing"));
//Assert.Contains("missing", exception.Message, StringComparison.OrdinalIgnoreCase);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Reflection;
using System.Reflection;
using DryIoc;
using Xamarin.Forms;

Expand All @@ -10,21 +9,14 @@ namespace Prism.DryIoc.Extensions
/// </summary>
public static class UnknownServiceResolverRule
{
public static Rules DependencyServiceResolverRule;

static UnknownServiceResolverRule()
public static Factory DependencyServiceResolverRule(Request request)
{
DependencyServiceResolverRule = Rules.Default.WithUnknownServiceResolvers(request => new DelegateFactory(_ =>
return new DelegateFactory(_ =>
{
var targetType = request.ServiceType;
if (!targetType.GetTypeInfo().IsInterface)
{
return null;
}
var method = typeof(DependencyService).GetTypeInfo().GetDeclaredMethod("Get");
var genericMethod = method.MakeGenericMethod(targetType);
var genericMethod = method.MakeGenericMethod(request.ServiceType);
return genericMethod.Invoke(null, new object[] { DependencyFetchTarget.GlobalInstance });
}));
});
}
}
}
3 changes: 2 additions & 1 deletion Source/Xamarin/Prism.DryIoc.Forms/PrismApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ protected override IModuleManager CreateModuleManager()
/// <returns>An instance of <see cref="Rules" /></returns>
protected virtual Rules CreateContainerRules()
{
return UnknownServiceResolverRule.DependencyServiceResolverRule;
return Rules.Default.WithAutoConcreteTypeResolution()
.WithUnknownServiceResolvers(request => UnknownServiceResolverRule.DependencyServiceResolverRule(request));
}

protected override void ConfigureContainer()
Expand Down

0 comments on commit ff7898b

Please sign in to comment.